[英]add roles command not working (discord.js)
I am Coding my own discord bot for my server, I have a issue that the bot says no role doesn't exit even if I mentions the role.我正在为我的服务器编写自己的 discord 机器人,我有一个问题,即机器人说没有角色不会退出,即使我提到了角色。 Here is the code:
这是代码:
const Discord = require('discord.js')
const client = new Discord.Client()
client.on("message", message => {
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase()
if (command === "add") {
if (!message.member.hasPermission("MANAGE_ROLES"))
return message.channel.send("Insufficient permissions")
const member = message.mentions.members.first()
if (!member)
return message.channel.send("No user mentioned")
const add = args.slice(1).join(" ")
if (!add)
return message.channel.send("No role said")
const roleAdd = message.guild.roles.find(role => role.name === add);
if (!roleAdd)
return message.channel.send("Role does not exist")
if (member.roles.has(roleAdd.id)){
return message.channel.send("User already has role")
}
if (member) {
member.addRole(roleAdd).catch((error) =>{
message.channel.send("I cant add...")
}).then((member) => {
message.channel.send(`:thumbsup: ${roleAdd} added to ${member.displayName}`)
})
}
}
})
client.login(config.token)
What did I made a mistake?我犯了什么错误? Thanks.
谢谢。
First make sure you are using discord.js v12.首先确保您使用的是 discord.js v12。 Type
npm i discord.js@latest
in your terminal.在您的终端中键入
npm i discord.js@latest
。 Here you can see all changes in discord.js v12. 在这里您可以看到 discord.js v12 中的所有更改。
If you are using discord.js v12 now, this code should work for you:如果您现在使用 discord.js v12,则此代码应该适合您:
if (!message.member.hasPermission("MANAGE_ROLES"))
return message.channel.send("Insufficient permissions")
const member = message.mentions.members.first()
if (!member)
return message.channel.send("No user mentioned")
const add = args.slice(1).join(" ");
if (!add)
return message.channel.send("No role said")
const roleAdd = message.guild.roles.cache.find(role => role.name === add);
if (!roleAdd)
return message.channel.send("Role does not exist")
if (member.roles.cache.has(roleAdd.id)) {
return message.channel.send("User already has role")
}
if (member) {
member.roles.add(roleAdd).catch((error) => {
message.channel.send("I cant add...")
}).then((member) => {
message.channel.send(`:thumbsup: ${roleAdd} added to ${member.displayName}`)
})
}
Mention the role you want to add won't work because here提及您要添加的角色不起作用,因为这里
const roleAdd = message.guild.roles.cache.find(role => role.name === add);
you just find the role by its name.您只需通过角色名称即可找到该角色。 To fix that you can change that line into:
要解决此问题,您可以将该行更改为:
const roleAdd = message.guild.roles.cache.find(role => role.name === add) || message.mentions.roles.first();
This will allow a user to mention the role as well.这也将允许用户提及角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.