繁体   English   中英

Discord.js 多反应收集器

[英]Discord.js multiple reaction collector

如何使此代码适用于多个反应? 我希望嵌入有多个反应,并根据选定的反应,它会给出不同的反应这是代码:

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 🐸!`);
      });
    }
  }```

首先,您需要将所有需要的反应添加到消息中:

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

现在,您需要编辑filter(reaction, user)函数。 reaction.emoji.name === '🐸'表示收集器只会响应一个表情符号 - 🐸。 如果您希望收集器响应不同的表情符号,您可以删除此表情。 在这种情况下,收集器将响应任何表情符号。 但是,您也可以让收集器响应特定的表情符号列表:

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

最后,要显示用户选择的表情符号而不是🐸,请替换message.channel.send()中的字符串:

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

此外,我可以为您提供另外 2 个可选的东西来改进代码:

  1. 由于代码是专门为收集一个反应而编写的,因此MAX_REACTIONS常量可能不会改变。 所以你可以去掉它,在创建收集器时使用1
  2. 因为您在创建收集器时没有传递time属性,所以如果命令的作者没有选择反应,则收集器将无限期地持续下去。 因此,您可以在创建收集器时传递idle属性并以毫秒为单位指定时间。 在指定的不活动毫秒数后,收集器将停止。 例如:
const collector = sentMessage.createReactionCollector({
    filter,
    max: 1,
    idle: 10000 // the collector will stop after 10 seconds of inactivity
});

如果收集器由于不活动而停止, 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