为什么等待反应不起作用Discord.js V12

时间:2020-09-02 18:43:29

标签: javascript node.js discord discord.js

我试图通过等待该用户的反应来建立确认系统,出于某种原因,我无法使其正常工作。

代码如下:

if (command === 'reset') {
 if (!msg.member.hasPermission('MANAGE_SERVER'))
  msg.reply('You need `Mannage server` permission to delete the progress.');
 //checking if author has mangage server permissions.

 msg.channel
  .send('Are you sure you want to delete all your progress?')
  .then((message) => {
   message.react('✅').then(() => message.react('❌'));
  });
 //confirming if author wants to delete channel.

 const filter = (reaction, user) => {
  return (
   ['✅', '❌'].includes(reaction.emoji.name) && user.id === msg.author.id
  );
 };

 const fetchedChannel = msg.guild.channels.cache.find(
  (channel) => channel.name === 'counting'
 );
 //getting the channel

 msg
  .awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
  .then((collected) => {
   const reaction = collected.first();

   if (reaction.emoji.name === '✅') {
    fetchedChannel.delete();

    msg.reply('Deleted all progress. to start over, run ".init"');
   } else {
    msg.reply('Aborting missing.');
    return;
   }
  })
  .catch((collected) => {
   msg.reply('No response given.');
  });
}

如果有人可以帮助,那就太好了! 谢谢。

1 个答案:

答案 0 :(得分:0)

我正在查看您的代码,并且由于我已经尝试过并且可以按预期工作,因此我认为已对其进行了修复。我所做的解释在代码中(第19行)。如果您对代码有任何疑问或仍然有疑问,我们将很乐意为您提供帮助。编码愉快

if (command === 'reset') {
 if (!msg.member.hasPermission('MANAGE_SERVER'))
  return msg.reply(
   'You need `Mannage server` permission to delete the progress.'
  ); // You forgot to add a return to prevent the command from people without enough permissions

 msg.channel
  .send('Are you sure you want to delete all your progress?')
  .then((message) => {
   message.react('✅');
   message.react('❌'); // I removed a .then(...)

   //confirming if author wants to delete channel.

   const filter = (reaction, user) => {
    return (
     ['✅', '❌'].includes(reaction.emoji.name) && user.id === msg.author.id
    );
   };

   const fetchedChannel = msg.guild.channels.cache.find(
    (channel) => channel.name === 'counting'
   );
   //getting the channel

   message
    .awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] }) // The problem was in this line, you used "msg" instead of "message", it means the bot wasn't awaiting reactions of its own message, it was awaiting reactions from the author's message.
    .then((collected) => {
     const reaction = collected.first();

     if (reaction.emoji.name === '✅') {
      fetchedChannel.delete();

      msg.reply('Deleted all progress. to start over, run ".init"');
     } else {
      msg.reply('Aborting missing.');
      return;
     }
    })
    .catch((collected) => {
     msg.reply('No response given.');
    });
  });
}