Discord.js TypeError:无法读取未定义的属性“ add”,该怎么办?

时间:2020-07-22 14:42:07

标签: javascript node.js discord.js

我想制作/mute command,但在添加角色上遇到一个问题。在我的行await member.roles.add(muterole).catch(console.error);上写错误:cannot read property 'add' of undefined。我不知道该怎么办。请帮我。我的完整代码:

const Discord = require('discord.js');
const Bot = new Discord.Client();

Bot.on("ready", () => {
    console.log(`Bot joined by ${Bot.user.tag}`);
});

Bot.on("message", async msg => {
    if (msg.author.bot) return;
    if (msg.channel.type === "dm") return;

    let Prefix = "/";

    if (msg.content.startsWith(Prefix)) {
        let massive = msg.content.split(" ");
        let cmd = massive[0];
        let args = massive.slice(1);

        if (msg.content.startsWith(`${Prefix}mute`)) {
            let member = msg.mentions.users.first();
            if (member) {
                //let member = msg.guild.member(user);
                let muterole = msg.guild.roles.cache.find(role => role.name === "Muted");

                await member.roles.add(muterole).catch(console.error);
            }
        }
    }
})

Bot.login('my token been hidden :)');

1 个答案:

答案 0 :(得分:0)

根据discord.js实体在这里表示的User docsmsg.mentions.users.first()类,没有属性“角色”

upd: 您可能正在寻找msg.mentions.members.first()的实体GuildMember