因此,我正在构建一个 Bot(专门用于 Discord)并且我对 JavaScript 和一般编码非常陌生。
我正在观看有关从区域命令清除消息的教程(您告诉机器人要删除多少消息,然后它会这样做。)
一切都很顺利,我在这个命令上工作了将近一个小时,在我保存并在我的 CMD 中运行“bot”之后,我收到了这个错误(显示在错误部分和预期结果区域)
基本上:
const fetched = await message.channel.fetchMessages({limit: args[0]});
语法错误:await 仅在异步函数中有效
但我很确定它是一个async
函数,我对编码仍然很陌生,所以我可能已经关闭它或其他东西,不确定。
仅供参考,第 31 行我以为我“打开”了一个async
东西,但也许我关闭了它。
我没有发送完整的代码。
由于我对编码非常陌生,因此我无法做太多事情。我确实加了一个“;” (没有引号)解决了一个错误,但不是我要问的那个。
// Purge messages command.
if (msg.startsWith(prefix + 'PURGE')) { // COmmand checks like "!PING", but ``startWith`` because you'll be adding a number.
// Wrapping in an async because ``awaits`` only work in asyncs.
async function purge() {
message.delete(); // Deletes command trigger, to clean up the chat more fully.
//Now, we want to check if the user has the `Moderator` role.
if (!message.member.roles.find("name", "Moderator")) { // This checks to see if they DONT have that role (the "!" inverts the true/false)
message.channel.send('You need the Moderator role to use this command!');
return; // This returns the code, so the rest doesn't run.
}
}
// Check if the argument is a number.
if (isNaN(args[0])) {
// Posts that the message is NaN (not a number)
message.channel.send('Your argument was not a number. \n Usage: ' +
prefix + 'purge <amount>'); //\n turns into a new line.
// Stops the actions, so it doesn't start deleting messages.
return;
}
const fetched = await message.channel.fetchMessages({limit: args[0]}); // This takes the argument number.
预期结果:机器人打开并删除大量指定消息。
完整的错误信息:
C:\Users\xxxx\OneDrive\Desktop\Xxxxx xxx\xxx.js:49 const fetched = await message.channel.fetchMessages({limit: args[0]}); // 这需要参数编号。
SyntaxError: await 仅在 Object.Module._extensions..js (internal/modules/cjs/loader.js:789) 的 Module._compile (internal/modules/cjs/loader.js:723:723:23) 的异步函数中有效10) 在 Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs) /loader.js:585:3) 在 Function.Module.runMain (internal/modules/cjs/loader.js:831:12) 在启动 (internal/bootstrap/node.js:283:19) 在 bootstrapNodeJSCore (internal/引导程序/node.js:622:3)
我无法理解你的代码。但我认为您应该在该功能的顶部添加“异步”。
async function func() {
await ~
}
像那种风格一样修改你的代码
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句