node.js - 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
,它似乎一切正常,它甚至在收集器结束时删除了消息!我还没有找到这样的东西,我真的很困惑发生了什么。
解决方案
问题似乎与收集事件处理程序有关:
collector.on('collect', (reaction, ReactionCollector) => {
console.log(`Collected ${reaction.emoji.name} from ${reaction.users.last().tag}`);
});
有2个问题:
现在collect事件在Discord.js v12中传递MessageReaction和User对象,而不是像Discord.js v11中的元素和Collector对象。这意味着您需要将参数从 更改为。
(reaction, ReactionCollector)
(reaction, user)
reaction.users
不再返回 Discord.js 集合,而是返回 aReactionUserManager
,您必须访问它的cache
属性才能获得Discord.Collection()
. 这意味着您需要更改reaction.users.last().tag
为reaction.users.cache.last().tag
. 或者,由于现在传递了一个Useruser.tag
对象,因此您可以使用它。
这是修改后的代码:
collector.on('collect', (reaction, user) => {
console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});
推荐阅读
- mysql - mysql比较ip地址
- c# - TypeScript 编译中缺少文件 custom.ts。请通过 'files' 或 'include' 属性确保它在您的 tsconfig 中
- javascript - JDBC 驱动程序从 mysql 返回错误的值
- grafana - Grafana - 如何绘制动态传递的每个变量的指标
- docker - Docker 看不到 main.go
- python - 不能将 queue.put 与 keras 模型一起使用
- r - 控制哪些节点改变大小 igraph
- java - 这是否意味着使用 newSingleThreadExecutor() 将在每个任务的同一 CPU 内核上执行给定的任务?
- postgresql - 欧洲进口陷入困境
- c++ - 我不能在一行中多次调用 rand() 吗?