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

Discordjs - lastMessageId always null event after fetching

简介:

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.

检查反应的代码是这个:

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; 
    }    
});

如果反应已经在好的频道(LAWYER_CONTACT)或配置频道(CONFIG)中做出,它将调用这个函数,这是一个试图检查权限的函数:

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

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

我对 console.log(用户) 的回应是: Result

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

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

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

感谢您的宝贵时间。

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

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