javascript - Discordjs - 获取后 lastMessageId 始终为空事件

标签 javascript node.js discord.js fetch bots

简介:

I'm pretty new at creating discord BOT and I'm currently trying to make like a ticket bot. In order to do that, I need to check if the user that add the reaction is allow to do it or not.

代码:

I already checked this (successfully) when the user is using a command thanks to this (isAllow is a custom function that works when passing member object in params) :

if (Common.isAllow(message.member)) {/* code ... */}

But I also want to check for the roles of the user that is reacting to a message, in order to create a new channel if the user is allowed to do it.

The problem is that when I get the user object, the lastMessageId is null, so I cannot get member object and check its roles.

检查 react 的代码是这个:

client.on("messageReactionAdd", async (reaction, user) => {
    // When a reaction is received, check if the structure is partial
    if (reaction.partial) {
        // If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
        try {
            await reaction.fetch();
        } catch (error) {
            console.error('Something went wrong when fetching the message: ', error);
            // Return as `reaction.message.author` may be undefined/null
            return;
        }
    }
    
    switch (reaction.message.channel.id) {
        // Check if the message has been sent into the lawyer contact channel
        case Config.CHANNELS.LAWYER_CONTACT:
        case Config.CHANNELS.CONFIG:
            checkForLawyerChannelReaction(reaction, user);
            break;
        default:
            break; 
    }    
});

如果在好的 channel (LAWYER_CONTACT)或配置 channel (CONFIG)中做出了 react ,它将调用这个函数,这是一个试图检查权限的函数:

function checkForLawyerChannelReaction(reaction, user) {
    console.log('checkForLawyerChanelReaction');
    console.log(user);

    if (Common.isAllow( /* member object */ ) { /* code ... */ }

我对 console.log(user) 得到的响应是: Result

我已经尝试过像这样获取用户数据,但它并没有改变任何事情: console.log(await client.users.fetch('123456789123456789')); (当然ID换了)

请注意,BOT 昨天以管理员权限加入(再次),并且从昨天开始用户已发送消息。

是否有人对此有解决方案或我可能还没有的信息?

感谢您的宝贵时间。

最佳答案

您可以通过将 user 作为参数传递给 Guild.member(user) 来获取 GuildMember 对象方法。

const guildMember = reaction.message.guild.member(user);

关于javascript - Discordjs - 获取后 lastMessageId 始终为空事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68428952/

相关文章:

javascript - jquery 不隐藏类并在单击时设置动画

javascript - Angular 4 以及 Node-rdkafka 和 kafka-node 的加载问题

javascript - 获取语​​音 channel 中的用户数

javascript - 有没有办法使用 setTimeout 为我的代码添加冷却时间,这样人们就不会向命令发送垃圾邮件?

javascript - Auth0 与 Socket.IO 强制认证

javascript - Angular 和 SEO 索引

javascript - Node JS 同步读取一行文本

javascript - 不和谐嵌入中的行分隔符/中断

Javascript 只触发一次

node.js - 了解 Sequelize 数据库迁移和种子