首页 > 解决方案 > 如何测试用户对消息做出反应的表情符号?

问题描述

我正在尝试制作一个系统,用户可以在其中对消息做出反应,并且会回复一些文本。文本将根据他们对哪个表情符号做出反应而有所不同。我研究过反应收集器,但我仍在努力寻找一个我想做的例子。

这是我正在使用的基本代码,我从 Discord's guide to collections here获得。

message.react('');

const filter = (reaction, user) => {
  return reaction.emoji.name === '';
};

const collector = message.createReactionCollector(filter, { max: 100 });

collector.on('collect', (reaction, user) => {
  message.channel.send('Collecting...')
});

collector.on('end', collected => {
  message.channel.send('Done');
});

此代码有效,但是collector.on('collect'...无论对哪个表情符号做出反应,它都会执行代码。我希望能够执行不同的代码,例如当用户对不同的表情符号做出反应时发送不同的嵌入。谢谢!

标签: discord.js

解决方案


您的收集器过滤器将只允许收集表情符号,因此您应该删除它以使机器人在添加其他反应时表现不同。您可以使用reactionuser参数来确定要执行的操作:

// This will make it collect every reaction, without checking the emoji
const collector = message.createReactionCollector(() => true, { max: 100 })

collector.on('collect', (reaction, user) => {
  if (reaction.emoji.name == '') {
    // The user has reacted with the  emoji
  } else {
    // The user has reacted with a different emoji
  }
})

collector.on('end', collected => {
  // The bot has finished collecting reaction, because either the max number 
  // has been reached or the time has finished
})

在这些if/else语句中,您可以添加任何您想要的内容(发送消​​息、嵌入,...)


推荐阅读