Javascript wait仅在bot命令内的异步函数错误中有效

Javascript wait仅在bot命令内的异步函数错误中有效,javascript,discord.js,Javascript,Discord.js,我写了这段代码,但我不能运行我的机器人,我不知道为什么 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 |

我写了这段代码,但我不能运行我的机器人,我不知道为什么

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仅在异步函数中有效

正如它所说,wait只能在异步函数中使用。因此,如果此代码位于函数内部,则使该函数异步。例如,如果周围的函数如下所示:

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}`);
  }
}
将其更改为:

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}`);
  }
}

谢谢你,朋友!
(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}`);
  }
})();