简体   繁体   English

Discord.js 多反应收集器

[英]Discord.js multiple reaction collector

How do I make this code work with multiple reactions?如何使此代码适用于多个反应? I would like the embed to have multiple reactions, and based on the selected reaction, it would give a different response Here's the code:我希望嵌入有多个反应,并根据选定的反应,它会给出不同的反应这是代码:

module.exports = {
    name: 'test',
    description: "ping command",
    async execute(message, args, Discord){
        
        var newEmbed = new Discord.MessageEmbed()
        .setColor('#A5775C')
        .setTitle('Reactions')
        .setDescription('*React to this!')
        
const MAX_REACTIONS = 1;
        
      const sentMessage = await message.channel.send({embeds: [newEmbed]});

      await sentMessage.react('🐸');

      const filter = (reaction, user) => reaction.emoji.name === '🐸' && !user.bot;

      const collector = sentMessage.createReactionCollector({
        filter,
        max: MAX_REACTIONS,
      });

      collector.on('end', (collected, reason) => {

        if (reason === 'limit')
          return message.channel.send(`You reacted with 🐸!`);
      });
    }
  }```

First you need to add all the needed reactions to the message:首先,您需要将所有需要的反应添加到消息中:

await sentMessage.react('emoji');
await sentMessage.react('emoji_2');
await sentMessage.react('emoji_3');
// ... //

Now, you need to edit the filter(reaction, user) function.现在,您需要编辑filter(reaction, user)函数。 reaction.emoji.name === '🐸' means that the collector will only respond to one emoji - 🐸. reaction.emoji.name === '🐸'表示收集器只会响应一个表情符号 - 🐸。 If you want the collector to respond to different emojis, you can just delete this expression.如果您希望收集器响应不同的表情符号,您可以删除此表情。 In this case, the collector will respond to any emoji.在这种情况下,收集器将响应任何表情符号。 But, you can also make the collector respond to a specific list of emoji:但是,您也可以让收集器响应特定的表情符号列表:

const filter = (reaction, user) => ["emoji1", "emoji2", "emoji3" /* ... */].includes(reaction.emoji.name) && !user.bot
// the collector will now respond to all the emoji in the array

And finally, to display the user-selected emoji instead of 🐸, replace the string in message.channel.send() :最后,要显示用户选择的表情符号而不是🐸,请替换message.channel.send()中的字符串:

message.channel.send(`You reacted with ${collected.first().emoji.name}!`);

Also, I can offer you 2 more optional things to improve the code:此外,我可以为您提供另外 2 个可选的东西来改进代码:

  1. Since the code is written specifically to collect one reaction, the MAX_REACTIONS constant probably won't change.由于代码是专门为收集一个反应而编写的,因此MAX_REACTIONS常量可能不会改变。 So you can get rid of it and use 1 when creating the collector.所以你可以去掉它,在创建收集器时使用1
  2. Because you do not pass the time property when you create the collector, the collector will last indefinitely if the author of the command does not choose a reaction.因为您在创建收集器时没有传递time属性,所以如果命令的作者没有选择反应,则收集器将无限期地持续下去。 Therefore, you can pass the idle property and specify the time in milliseconds when creating the collector.因此,您可以在创建收集器时传递idle属性并以毫秒为单位指定时间。 After the specified number of milliseconds of inactivity, the collector will stop.在指定的不活动毫秒数后,收集器将停止。 For example:例如:
const collector = sentMessage.createReactionCollector({
    filter,
    max: 1,
    idle: 10000 // the collector will stop after 10 seconds of inactivity
});

If the collector stops due to inactivity, reason will be "idle" , inside collector.on("end", ...) :如果收集器由于不活动而停止, reason将是"idle" ,在collector.on("end", ...)内部:

collector.on('end', (collected, reason) => {
    if (reason === "limit") {
        return message.channel.send(`You reacted with ${collected.first().emoji.name}!`);
    } else if (reason === "idle") {
        // ... //
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM