如何测试用户对消息反应了哪种表情符号?

问题描述 投票:0回答:1

我正在尝试建立一个系统,使用户可以对消息作出反应,并且该消息将以一些文本进行回复。文字会有所不同,具体取决于他们对哪些表情符号做出了反应。我已经研究了反应收集器,但仍在努力寻找我想做的事的例子。

这是我正在使用的基本代码,是从Discord的集合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
1个回答
1
投票

您的收集器过滤器将仅允许收集🇫表情符号,因此您应该删除该表情符号,以便在添加其他反应时使机器人具有不同的行为。您可以使用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语句中,您可以添加所需的任何内容(发送消​​息,嵌入等)

© www.soinside.com 2019 - 2024. All rights reserved.