【问题标题】:How do I use a reaction handler with a switch statement如何使用带有 switch 语句的反应处理程序
【发布时间】:2021-01-17 14:18:22
【问题描述】:

我正在制作一个 discord.js 机器人,并且我正在创建一个允许用户对给定消息做出反应的函数,然后机器人将根据反应编辑消息。

我所做的工作有效,但是,我希望用户能够在反应结束之前多次按下反应。这是我的代码:

const filter = (reaction, user) =>
 ['????', '????', '????', '????'].includes(reaction.emoji.name) &&
 user.id === message.author.id;
r.awaitReactions(filter, {
 max: 3,
 time: 30000,
 errors: ['time'],
})
 .then((collected) => {
  const reaction = collected.first();
  switch (reaction.emoji.name) {
   case '????':
    message.util.send(cAbility);
    break;
   case '????':
    message.util.send(qAbility);
    break;
   case '????':
    message.util.send(eAbility);
    break;
   case '????':
    message.util.send(ultAbility);
    break;
  }
 })
 .catch((collected) => {
  return console.log();
 });

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您可能想要倾听反应而不是等待它们。

    这是一个例子:

    client.on('messageReactionAdd', async (reaction, user) => {
      if (reaction.partial) {
        // The message the reaction belongs to might have been deleted.
        try {
          await reaction.fetch();
        } catch (error) {
          console.error('Error: ', error);
          return;
        }
      }
      switch (reaction.emoji.name) {
       case '?':
        message.util.send(cAbility);
        break;
       case '?':
        message.util.send(qAbility);
        break;
       case '?':
        message.util.send(eAbility);
        break;
       case '?':
        message.util.send(ultAbility);
        break;
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2015-04-27
      • 2011-06-26
      相关资源
      最近更新 更多