简体   繁体   中英

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. 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. Type npm i discord.js@latest in your terminal. Here you can see all changes in discord.js v12.

If you are using discord.js v12 now, this code should work for you:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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