【问题标题】:Discord reactions collector doesn't collect reactions不和谐反应收集器不收集反应
【发布时间】:2022-02-22 00:02:35
【问题描述】:

我已经阅读了 discord.js 文档,并且一直在尝试我一直在阅读的这些东西,但它们似乎都不起作用。我遇到的主要问题是收集器说它什么也没收集。

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

【问题讨论】:

  • 你的 discord.js 版本是多少?
  • v14.16.1 @ZsoltMeszaros
  • 这听起来像是你的 node.js 的版本。
  • 抱歉版本是discord.js@13.6.0

标签: javascript node.js discord discord.js


【解决方案1】:

您可以使用awaitReactionscreateReactionCollector,而不是ReactionCollector 类,它们在您发送的消息对象(msgEmbed) 上可用。

如果您使用 discord.js v13,awaitReactionscreateReactionCollector 方法接受单个参数,filter 现在是 options 对象的一部分。 (见Changes in v13。)所以,你需要更新它;使用filter 传递单个对象。

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

确保添加 GUILD_MESSAGE_REACTIONS 意图。你可以阅读more about reaction collectors here

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

查看下面的代码:

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

【讨论】:

  • 所以我已经尝试了您发送过来的代码,但似乎仍然没有收集任何内容,因为控制台中没有显示任何内容
  • 可能缺少GUILD_MESSAGE_REACTIONS 意图? stackoverflow.com/questions/67543726/…
猜你喜欢
  • 2021-11-03
  • 1970-01-01
  • 2023-03-18
  • 2021-11-15
  • 2021-10-18
  • 2020-08-12
  • 1970-01-01
  • 2020-11-30
  • 2021-05-26
相关资源
最近更新 更多