等待仅在bulkDelete的异步函数中有效

时间:2019-12-24 20:49:44

标签: javascript async-await discord

我正在使用Discord.js对Discord机器人进行编码,并且试图创建.clear命令来清除消息。问题是我无法删除邮件,因为尝试使用bulkDelete时收到await is only valid in async function。我在bot.on('message', msg => {部分进行了编码。这是我的代码:

if (msg.content.startsWith('.clear')) {
    if(msg.member.hasPermission('MANAGE_MESSAGES')) {
      const args = msg.content.split(' ').slice(1);
      const amount = args.join(' ');
      if(!amount) {
        const noNumbers = new Discord.RichEmbed()
          .setColor('#0099ff')
          .setDescription(':no_entry: Vous n\'avez pas précisé combien de messages devraient être supprimés !')
        msg.channel.send(noNumbers)
      }
      if(isNaN(amount)) {
        const notNumber = new Discord.RichEmbed()
          .setColor('#0099ff')
          .setDescription(':no_entry: Ce paramètre n\'est pas un nombre !')
        msg.channel.send(notNumber)
      }
      if(amount > 100) {
        const tooMuch = new Discord.RichEmbed()
          .setColor('#0099ff')
          .setDescription(':no_entry: Vous ne pouvez pas supprimer plus de 100 messages à la fois !')
        msg.channel.send(tooMuch)
      }
      if(amount < 1) {
        const tooLess = new Discord.RichEmbed()
          .setColor('#0099ff')
          .setDescription(':no_entry: Vous ne pouvez pas supprimer moins d\'un message !')
        msg.channel.send(tooLess)
      }
      else {
          await msg.channel.messages.fetch({limit: amount}).then(messages => {
            msg.channel.bulkDelete(messages)        
          });
        }
      }
    }

谢谢! (不要介意嵌入的描述,我是法国人)

1 个答案:

答案 0 :(得分:2)

尝试:

bot.on('message', async (msg) => {
  // your code
}
相关问题