简体   繁体   English

Discord 尝试添加角色时出现机器人错误

[英]Discord Bot error comes up trying to add a role

Trying to make a "mute command" comes up with TypeError: Cannot read property 'add' of undefined If you could help me Thanks:)尝试制作“静音命令”时出现 TypeError: Cannot read property 'add' of undefined 如果你能帮助我谢谢:)

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

Here is the "main file" if there is an issue with this如果有问题,这里是“主文件”

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

This will not work because discord user and guild member both are completely different methods.这将不起作用,因为 discord 用户和公会成员都是完全不同的方法。 You cannot add roles to discord user in any guild (unless your bot is in the said guild), you can only add roles to guild member.您不能在任何公会中为 discord 用户添加角色(除非您的机器人在上述公会中),您只能向公会成员添加角色。
replace:代替:

message.mentions.users.first()

with:和:

message.mentions.members.first()

Learn more about:学习更多关于:
Discord User Discord 用户
Guild Member 公会成员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM