[英]await is only valid in async function error inside a bot command
I wrote this code and I can't run my bot, I don't know why. 我写了这段代码,但我无法运行我的机器人,不知道为什么。
if (command === 'await') {
let msg = await message.channel.send("Vote!");
await msg.react(agree);
await msg.react(disagree);
const reactions = await msg.awaitReactions(reaction => reaction.emoji.name === agree || reaction.emoji.name === disagree, {
time: 15000
});
message.channel.send(`Voting complete! \n\n${agree}: ${reactions.get(agree).count-1}\n${disagree}: ${reactions.get(disagree).count-1}`);
}
SyntaxError: await is only valid in async function
As it says, await can only be used inside an async function. 就像说的那样,await只能在异步函数中使用。 So if this code is inside a function, make that function async. 因此,如果此代码在函数内部,请使该函数异步。 For example, if the surrounding function looks like this: 例如,如果周围的函数如下所示:
function doStuff() {
if(command === 'await'){
let msg = await message.channel.send("Vote!");
await msg.react(agree);
await msg.react(disagree);
const reactions = await msg.awaitReactions(reaction => reaction.emoji.name === agree || reaction.emoji.name === disagree, {time:15000});
message.channel.send(`Voting complete! \n\n${agree}: ${reactions.get(agree).count-1}\n${disagree}: ${reactions.get(disagree).count-1}`);
}
}
Change it to this: 更改为此:
async function doStuff() { // <--- added async
if(command === 'await'){
let msg = await message.channel.send("Vote!");
await msg.react(agree);
await msg.react(disagree);
const reactions = await msg.awaitReactions(reaction => reaction.emoji.name === agree || reaction.emoji.name === disagree, {time:15000});
message.channel.send(`Voting complete! \n\n${agree}: ${reactions.get(agree).count-1}\n${disagree}: ${reactions.get(disagree).count-1}`);
}
}
If this code is not in a function (ie, it's at the topmost scope a script), then you'll need to put it in one. 如果此代码不在函数中(即,它在脚本的最高作用域中),则需要将其放在一个代码中。 It could be an immediately invoked function if desired 如果需要,它可以是立即调用的函数
(async function () {
if (command === 'await') {
const msg = await message.channel.send('Vote!');
await msg.react(agree);
await msg.react(disagree);
const reactions = await msg.awaitReactions(reaction => reaction.emoji.name === agree || reaction.emoji.name === disagree, { time: 15000 });
message.channel.send(`Voting complete! \n\n${agree}: ${reactions.get(agree).count - 1}\n${disagree}: ${reactions.get(disagree).count - 1}`);
}
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.