首页 > 解决方案 > 为什么等待反应不起作用 Discord.js V12

问题描述

我试图通过等待该用户的反应来建立一个确认系统,由于某种原因,我无法让它工作。

这是代码:

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.');
  });
}

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

标签: javascriptnode.jsdiscorddiscord.js

解决方案


我正在查看您的代码,我想我已经修复了它,因为我已经尝试过它并且它按预期工作。我所做的解释在代码中(第 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.');
    });
  });
}

推荐阅读