【发布时间】:2020-04-06 07:45:51
【问题描述】:
我正在尝试制作一个 Discord 机器人,它会根据反应给出一个角色。目前,我正在测试原始事件是否会触发。
我不知道为什么,但是 channels.fetchMessage 不会触发 .then,我尝试手动输入仍然不会响应的频道 ID。
我在 Discord.js ^12.1.1 上运行
这不是完整的代码示例,因为我在最后取出了客户端登录
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("ready", async () => {
client.user.setActivity('Redacted');
});
client.on('raw', packet => {
// We don't want this to run on unrelated packets
if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) return;
// Grab the channel to check the message from
const channel = client.channels.cache.get(packet.d.channel_id);
// There's no need to emit if the message is cached, because the event will fire anyway for that
//if (channel.messages.has(packet.d.message_id)) return;
// Since we have confirmed the message is not cached, let's fetch it
console.log("Passed")
channel.fetchMessage(packet.d.message_id).then(message => {
console.log("Passed Again")
// Emojis can have identifiers of name:id format, so we have to account for that case as well
const emoji = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;
// This gives us the reaction we need to emit the event properly, in top of the message object
const reaction = message.reactions.get(emoji);
// Adds the currently reacting user to the reaction's users collection.
if (reaction) reaction.users.set(packet.d.user_id, client.users.get(packet.d.user_id));
// Check which type of event it is before emitting
if (packet.t === 'MESSAGE_REACTION_ADD') {
console.log("yes")
}
if (packet.t === 'MESSAGE_REACTION_REMOVE') {
console.log("no")
}
});
});
【问题讨论】:
标签: discord.js