Discord.js如何制作反应收集器 [英] discord.js How do you make a reaction collector

查看:0
本文介绍了Discord.js如何制作反应收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样做,当你达到一定的反应时,我的机器人会发送一条消息,如果有人能帮助我,这是我的代码

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

推荐答案

除了awaitReactions,您还可以使用createReactionCollector,它的collector.on()监听器可能比awaitReactionsthen()catch()方法更易于使用。

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

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

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

查看以下工作代码:

// 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意图
  • 事件现在为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);
    }
  }
});

结果:

这篇关于Discord.js如何制作反应收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆