Discord 反应收集器不收集反应

Discord reactions collector doesn't collect reactions

我已经阅读了 discord.js 文档,并一直在尝试我一直在阅读的内容,但其中 none 似乎有效。我遇到的主要问题是收集器声明它没有收集任何东西。

if (message.content === 'poll') {
  let embedPoll = new Discord.MessageEmbed()
    .setTitle(' DaSquad ')
    .setColor('YELLOW')
    .addField('1:', 'name')
    .addField('2:', 'name2')
    .addField('3:', 'name3')
    .addField('4:', 'name4')
    .addField('5:', 'name5')
    .addField('6:', 'name6')
    .addField('7:', 'name7')
    .addField('8:', 'name8');
  let msgEmbed = await message.channel.send({ embeds: [embedPoll] });
  await msgEmbed.react('1️⃣');
  await msgEmbed.react('2️⃣');
  await msgEmbed.react('3️⃣');
  await msgEmbed.react('4️⃣');
  await msgEmbed.react('5️⃣');
  await msgEmbed.react('6️⃣');
  await msgEmbed.react('7️⃣');
  await msgEmbed.react('8️⃣');

  const filter = (reaction) =>
    reaction.emoji.name === '1️⃣' ||
    reaction.emoji.name === '2️⃣' ||
    reaction.emoji.name === '3️⃣' ||
    reaction.emoji.name === '4️⃣' ||
    reaction.emoji.name === '5️⃣' ||
    reaction.emoji.name === '6️⃣' ||
    reaction.emoji.name === '7️⃣' ||
    reaction.emoji.name === '8️⃣';

  const collector = new Discord.ReactionCollector(msgEmbed, filter);

  console.log(collector.collected);
}

您可以使用已发送的消息对象上可用的 awaitReactionscreateReactionCollector 代替 ReactionCollector class (msgEmbed ).

如果您使用的是 discord.js v13,则 awaitReactions and createReactionCollector methods accept a single parameter and the filter is part of the options object now. (See Changes in v13。)因此,您需要对其进行更新;使用 filter.

传递单个对象

另一个错误是您在实例化收集器后立即尝试使用简单的 console.log 读取收集的反应 (collector.collected)。您应该改为设置事件侦听器并订阅 collect(可能还有 stop)事件。

确保添加 GUILD_MESSAGE_REACTIONS 意图。你可以阅读 .

还有一件事,您可以创建一个数组,其中包含您想要与之做出反应的所有表情符号,让您的生活更轻松一些。

查看下面的代码:

if (message.content === 'poll') {
  let embedPoll = new Discord.MessageEmbed()
    .setTitle(' DaSquad ')
    .setColor('YELLOW')
    .addField('1:', 'name')
    .addField('2:', 'name2')
    .addField('3:', 'name3')
    .addField('4:', 'name4')
    .addField('5:', 'name5')
    .addField('6:', 'name6')
    .addField('7:', 'name7')
    .addField('8:', 'name8');

  let reactions = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣'];
  let msgEmbed = await message.channel.send({ embeds: [embedPoll] });

  reactions.forEach((reaction) => msgEmbed.react(reaction));

  const filter = (reaction) => reactions.includes(reaction.emoji.name);
  const collector = msgEmbed.createReactionCollector({ filter });

  // code inside this runs every time someone reacts with those emojis
  collector.on('collect', (reaction, user) => {
    console.log(`Just collected a ${reaction.emoji.name} reaction from ${user.username}`);
  });
}