【问题标题】:channels.fetchMessage not firing then in a raw eventchannels.fetchMessage 在原始事件中未触发
【发布时间】: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


    【解决方案1】:

    您获取频道的方式不正确,您需要获取 GuildChannel。

    您的解决方案是:

    const channel = client.guilds.cache.get(guildID).channels.cache.get(channelID);
    
    channel.messages.fetch(messageID);
    

    【讨论】:

    • 谢谢,但由于某种原因,这导致 addRole 不是函数 let user = message.guild.members.cache.get(packet.d.user_id) let role = message.guild.roles。 cache.find(x => x.name === "会员"); user.addRole(角色)
    • addRole 确实不是从 discord.js v12 开始的函数,您需要使用 roles.add 代替,这里是文档:discord.js.org/#/docs/main/stable/class/…
    • 哇!自从我使用 Discord.js 以来,它发生了很多变化。感谢您的帮助。
    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多