const Discord = require('discord.js');
const ALIVE_ROLE = 'Role ID'
const DEAD_ROLE = 'Role ID'
const client = new Discord.Client();
client.on("message", (message) => {
msg = message.content.toLowerCase();
if (message.author.bot) {
return;
}
mention = message.mentions.users.first();
if (msg.startsWith("Give new role")) {
if (mention == null) {
return;
}
message.delete();
mentionMessage = message.content.slice
mention.member.roles.remove(Role 1);//code fails
mention.member.roles.add(Role 2);//code fails
mention.send('you now have a new role');
message.channel.send("now done");
}
});
client.login('token')
我正在制作一个机器人,当您发送一条消息,通知用户@ed角色已更改,但其中提到的那部分却没有任何想法?
答案 0 :(得分:1)
您需要使用message.mentions.members.first()
而不是message.mentions.users.first()
。 User
不具有角色,而Member
(User
中的Guild
)具有角色。
const Discord = require('discord.js');
const ALIVE_ROLE = 'Role ID'
const DEAD_ROLE = 'Role ID'
const client = new Discord.Client();
client.on("message", (message) => {
msg = message.content.toLowerCase();
if (message.author.bot) {
return;
}
mention = message.mentions.members.first();
if (msg.startsWith("Give new role")) {
if (mention == null) {
return;
}
message.delete();
mentionMessage = message.content.slice
mention.member.roles.remove(ALIVE_ROLE);//code fails
mention.member.roles.add(DEAD_ROLE);//code fails
mention.send('you now have a new role');
message.channel.send("now done");
}
});
client.login('token')