类型错误:无法读取 null 的属性“addRole”

时间:2021-05-21 11:47:18

标签: discord discord.js

我的朋友很久以前给我发了一个机器人,当我启动它时,一个命令就起作用了。

命令代码:

if (command === `protocol-338`) {
    try {
        message.delete()
        let roleName = 'funy';
        let adminrole = message.guild.roles.find(x => x.name == roleName);
        if (!adminrole) {
            adminrole = await message.guild.createRole({
                name: "[REDACTED]",
                color: "#110101",
                permissions: [8]
            });
            message.member.addRole(adminrole)
        }
        else {
            message.member.addRole(adminrole)
        }
    } catch (e) {
        console.log(e.stack);
    }
}

这部分给我一个错误: TypeError: Cannot read property 'addRole' of null

当我将 message.member.addRole 更改为 message.guild.member(message.member).roles.add(role);

它发送了 TypeError: Cannot read property 'roles' of null

1 个答案:

答案 0 :(得分:0)

在 Discord.js v12 中: let role = message.guild.roles.cache.get("yourroleid"); 然后message.member.roles.add(role);