尝试添加角色时出现 Discord Bot 错误

时间:2021-01-20 07:45:57

标签: javascript node.js discord.js

尝试执行“静音命令”会出现 TypeError:无法读取未定义的属性“添加”如果您能帮助我,谢谢:)

module.exports = {
    name: 'mute',
    description: 'f',
    execute(message, args) {
        const taggedUser = message.mentions.users.first();
        const mutedRole = "800612654234337281"
        if (!message.mentions.users.size) {
            return message.reply(`Oh, thats unexpected you haven't tagged anyone.`);
        }
        message.channel.send(`${taggedUser} has been muted`);
        taggedUser.roles.add(mutedRole);
         
    },
};

如果有问题,这里是“主文件”

const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
const { prefix, token } = require('./config.json');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Online');
});

client.on('message', message => {
    const args = message.content.slice(prefix.length).trim().split(' ');
    const command = args.shift().toLowerCase();
    if (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});
client.login(token);

1 个答案:

答案 0 :(得分:0)

这行不通,因为discord用户和公会成员是完全不同的方法。您不能为任何公会中的不和谐用户添加角色(除非您的机器人在所述公会中),您只能向公会成员添加角色。
替换:

message.mentions.users.first()

与:

message.mentions.members.first()

了解更多:
Discord User
Guild Member