首页 > 解决方案 > Discord.js 反应收集器不听收集器.on('collect')

问题描述

我正在尝试使用 Discord.js 制作具有 AFK 功能的 Discord 机器人。当用户发送消息时,它将检查他们是否处于 AFK 状态,并在他们做出反应时发送 DM 提议以关闭 AFK。我正在尝试更新到 v12。它在更新之前工作,现在它似乎拒绝听取任何反应。它会对消息做出反应,但是任何时候我做出反应都不会将其记录在控制台中。

message.author.send(noLongerAFKMessage).then(async function(msg) {
  try {
    await msg.react('✅');
    await msg.react('❌');
    const reactionFilter = (reaction, user) => {
      return reaction.emoji.name === '✅' || reaction.emoji.name === '❌';
    };
    // Use reaction filter to remove to remove the user from the database rather than an event
    const collector = msg.createReactionCollector(reactionFilter, {
      time: 15000
    });
    collector.on('collect', (reaction, ReactionCollector) => {
      console.log(`Collected ${reaction.emoji.name} from ${reaction.users.last().tag}`);
    });
    collector.on('end', _ => {
      msg.delete().catch(() => console.log('Tried deleting afk message that was already deleted'));
    });
  } catch (err) {
    console.error(err);
  }
});

它不会抛出任何错误,它会在设置反应收集器后运行代码,我在 上使用了 console.log collector,它似乎一切正常,它甚至在收集器结束时删除了消息!我还没有找到这样的东西,我真的很困惑发生了什么。

标签: node.jsdiscorddiscord.js

解决方案


问题似乎与收集事件处理程序有关:

collector.on('collect', (reaction, ReactionCollector) => {
  console.log(`Collected ${reaction.emoji.name} from ${reaction.users.last().tag}`);
});

有2个问题:

  1. 现在collect事件在Discord.js v12中传递MessageReactionUser对象,而不是像Discord.js v11中的元素和Collector对象。这意味着您需要将参数从 更改为。(reaction, ReactionCollector)(reaction, user)

  2. reaction.users不再返回 Discord.js 集合,而是返回 a ReactionUserManager,您必须访问它的cache属性才能获得Discord.Collection(). 这意味着您需要更改reaction.users.last().tagreaction.users.cache.last().tag. 或者,由于现在传递了一个Useruser.tag对象,因此您可以使用它。

这是修改后的代码:

collector.on('collect', (reaction, user) => {
  console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});

推荐阅读