【问题标题】:discord.js How do you make a reaction collectordiscord.js 你如何做一个反应收集器
【发布时间】:2021-08-05 04:23:30
【问题描述】:

我正在努力做到这一点,所以当你得到一定数量的反应时,我的机器人会发送一条消息,如果有人可以帮助这里是我的代码

.then(function(sentMessage) {
            sentMessage.react('????').catch(() => console.error('emoji failed to react.')).message.reactions.cache.get('????').count;
            const filter = (reaction, user) => {
    return reaction.emoji.name === '????' && user.id === message.author.id;
};

message.awaitReactions(filter, { max: 2, time: 00, errors: ['time'] })
    .then(collected => console.log(collected.size))
    .catch(collected => {
        console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
    });
        });
})

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    除了awaitReactions,您还可以使用createReactionCollector,这可能更易于使用,并且它的collector.on() 侦听器比awaitReactionsthen()catch() 方法更具可读性。

    您无需使用message.reactions.cache.get('?').count 来检查反应数量,因为当您达到最大值时会触发end 事件,您可以在其中发送消息。

    此外,在您的filter 中,您无需检查是否为user.id === message.author.id,因为您希望接受其他用户的反应。但是,您可以检查是否!user.bot 以确保您不会计算机器人的反应。如果您不想限制机器人收集反应的时间,也可以删除 time 选项。

    另一个错误是您在message 本身而不是sentMessage 上调用了.awaitReactions()

    查看下面的工作代码:

    // v12
    client.on('message', async (message) => {
      if (message.author.bot || !message.content.startsWith(prefix)) return;
    
      const args = message.content.slice(prefix.length).split(/ +/);
      const command = args.shift().toLowerCase();
      const MAX_REACTIONS = 2;
    
      if (command === 'react') {
        try {
          // send a message and wait for it to be sent
          const sentMessage = await message.channel.send('React to this!');
    
          // react to the sent message
          await sentMessage.react('?');
    
          // set up a filter to only collect reactions with the ? emoji
          // and don't count the bot's reaction
          const filter = (reaction, user) => reaction.emoji.name === '?' && !user.bot;
    
          // set up the collecrtor with the MAX_REACTIONS
          const collector = sentMessage.createReactionCollector(filter, {
            max: MAX_REACTIONS,
          });
    
          collector.on('collect', (reaction) => {
            // in case you want to do something when someone reacts with ?
            console.log(`Collected a new ${reaction.emoji.name} reaction`);
          });
    
          // fires when the time limit or the max is reached
          collector.on('end', (collected, reason) => {
            // reactions are no longer collected
            // if the ? emoji is clicked the MAX_REACTIONS times
            if (reason === 'limit')
              return message.channel.send(`We've just reached the maximum of ${MAX_REACTIONS} reactions.`);
          });
        } catch (error) {
          // "handle" errors
          console.log(error);
        }
      }
    });
    

    如果您使用的是 discord.js v13,有几个变化:

    • 您需要添加 GUILD_MESSAGE_REACTIONS 意图
    • message 事件现在是 messageCreate
    • 收集器的filteroptions 对象内
    // v13
    const { Client, Intents } = require('discord.js');
    
    const client = new Client({
      intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
      ],
    });
    const prefix = '!';
    
    client.on('messageCreate', async (message) => {
      if (message.author.bot || !message.content.startsWith(prefix)) return;
    
      const args = message.content.slice(prefix.length).split(/ +/);
      const command = args.shift().toLowerCase();
      const MAX_REACTIONS = 2;
    
      if (command === 'react') {
        try {
          // send a message and wait for it to be sent
          const sentMessage = await message.channel.send('React to this!');
    
          // react to the sent message
          await sentMessage.react('?');
    
          // set up a filter to only collect reactions with the ? emoji
          // and don't count the bot's reaction
          const filter = (reaction, user) => reaction.emoji.name === '?' && !user.bot;
    
          // set up the collecrtor with the MAX_REACTIONS
          const collector = sentMessage.createReactionCollector({
            filter,
            max: MAX_REACTIONS,
          });
    
          collector.on('collect', (reaction) => {
            // in case you want to do something when someone reacts with ?
            console.log(`Collected a new ${reaction.emoji.name} reaction`);
          });
    
          // fires when the time limit or the max is reached
          collector.on('end', (collected, reason) => {
            // reactions are no longer collected
            // if the ? emoji is clicked the MAX_REACTIONS times
            if (reason === 'limit')
              return message.channel.send(`We've just reached the maximum of ${MAX_REACTIONS} reactions.`);
          });
        } catch (error) {
          // "handle" errors
          console.log(error);
        }
      }
    });
    

    结果:

    【讨论】:

      【解决方案2】:

      您想使用if 语句检查collected.size,如下所示:

      let amount = 4; // any integer
      if (collected.size /* returns an integer */ === amount) {
      console.log(`Got ${amount} reactions`)
      }
      

      希望我的问题是正确的。

      【讨论】:

        【解决方案3】:

        如果我理解正确,那么您可以更改awaitMessage 方法的参数。您可以删除 time: 00, errors: ['time'] 参数,因为它们是可选的,并保留 max: 2。这样,函数只会在有 2 反应(在这种情况下)时完成。

        我建议从过滤器中删除 user.id === message.author.id;,因为您似乎希望多个用户对消息做出反应。

        更多信息,您可以查看discord.js guidedocumentation for awaitReactions

        代码:

        message.channel.send("Message reacting to.").then(function (sentMessage) {
            sentMessage.react('?').catch(() => console.error('emoji failed to react.'));
            const filter = (reaction, user) => {
                return reaction.emoji.name === '?';
            };
            message.awaitReactions(filter, { max: 2 })
                .then(collected => console.log(collected.size))
        });
        

        【讨论】:

          猜你喜欢
          • 2023-03-18
          • 2020-12-31
          • 2021-06-16
          • 2020-11-30
          • 1970-01-01
          • 2020-05-09
          • 2020-07-06
          • 2021-05-11
          • 2020-08-15
          相关资源
          最近更新 更多