[英]Discord Bot error comes up trying to add a role
嘗試制作“靜音命令”時出現 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);
},
};
如果有問題,這里是“主文件”
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);
這將不起作用,因為 discord 用戶和公會成員都是完全不同的方法。 您不能在任何公會中為 discord 用戶添加角色(除非您的機器人在上述公會中),您只能向公會成員添加角色。
代替:
message.mentions.users.first()
和:
message.mentions.members.first()
學習更多關於:
Discord 用戶
公會成員
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.