Discordjs - 获取

时间:2021-07-18 12:10:08

标签: javascript node.js discord.js fetch bots

简介:

<块引用>

我对创建不和谐 BOT 还很陌生,我目前正在尝试像票务机器人一样制作。 为了做到这一点,我需要检查添加反应的用户是否允许这样做。

代码:

<块引用>

当用户使用命令时,我已经(成功)检查了这一点(isAllow 是一个自定义函数,在 params 中传递成员对象时起作用):

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

<块引用>

但我还想检查对消息做出反应的用户的角色,以便在允许用户执行此操作的情况下创建新频道。

<块引用>

问题是,当我获取用户对象时,lastMessageId为空,所以我无法获取成员对象并检查其角色。

检查反应的代码是这样的:

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(user) 的响应是: Result

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

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

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

感谢您抽出宝贵时间。

1 个答案:

答案 0 :(得分:2)

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

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