0

I am making a discord bot using discord.js v12. I made an addrole command. Here is the code of that file:

const errors = require("../../../utils/errors"); 
const second = require("../../../utils/othererrors.js"); 

module.exports = {
    config: {
        name: "addrole",
        aliases: ["roleadd"],
        usage: "$addrole <user>",
        description: "Add a role to someone",
        permissions: "manage roles"
    },
    run: async (bot, message, args) => {
        if (message.channel.type == "dm") return message.channel.send("This command only works in a server!");

        if(!message.member.hasPermission(["MANAGE_ROLES", "ADMINISTRATOR"])) return errors.noPerms(message, "MANAGE_ROLES");
        if(!message.guild.me.hasPermission(["MANAGE_ROLES", "ADMINISTRATOR"])) return errors.lack(message.channel, "MANAGE_ROLES");

        let cmd = message.content.split(" ")[0]; //used because of command aliases
        if (args[0] == "help") return message.channel.send(`Command Syntax: ${cmd} (user) <role-name>`);

        let rMember = message.mentions.members.first() || message.guild.members.find(m => m.user.tag === args[0]) || message.guild.members.get(args[0]);
        if(!rMember) return errors.cantfindUser(message.channel);

        if (rMember.id === bot.user.id) return errors.botuser(message, "add a role to");

        let role = message.guild.roles.cache.find(r => r.name == args[1]) || message.guild.roles.cache.find(r => r.id == args[1]) || message.mentions.roles.first();
        if(!role) return errors.noRole(message.channel);

        if(rMember.roles.has(role.id)) {
            return message.channel.send(`**${rMember.displayName} already has that role!**`)
        } else {
            try {
                await rMember.addRole(role.id);
                message.channel.send(`**The role, ${role.name}, has been added to ${rMember.displayName}.**`); //if successful this message
            } catch(e) {
                let id = second.getError(e.message);
                message.channel.send(`Unfortunately an error occurred. Error ID: ${id}`);
            }
        }
    }
}

Now when I start the bot and use the command, it returns this error:

(node:10904) UnhandledPromiseRejectionWarning: TypeError: rMember.roles.has is not a function
    at Object.run (F:\codes\DISCORDBOTS\discordBotJS-Final\src\commands\moderation\addrole.js:29:26)
    at module.exports (F:\codes\DISCORDBOTS\discordBotJS-Final\events\guild\message.js:36:21)
    at Client.emit (events.js:376:20)
    at MessageCreateAction.handle (F:\codes\DISCORDBOTS\discordBotJS-Final\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)       
    at Object.module.exports [as MESSAGE_CREATE] (F:\codes\DISCORDBOTS\discordBotJS-Final\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (F:\codes\DISCORDBOTS\discordBotJS-Final\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (F:\codes\DISCORDBOTS\discordBotJS-Final\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)      
    at WebSocketShard.onMessage (F:\codes\DISCORDBOTS\discordBotJS-Final\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)     
    at WebSocket.onMessage (F:\codes\DISCORDBOTS\discordBotJS-Final\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:376:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:10904) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10904) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

Any help will be appreciated. Thanks

2
  • not being familiar with the discord API but being very familiar with JS/Node, have you console.log'd rMember.roles to see what its outputting? Commented May 25, 2021 at 5:10
  • yes i have..... Commented May 25, 2021 at 13:38

1 Answer 1

3

So in order to access that function, you have to use .cache (because it returns a collection) in between:

rMember.roles.cache.has()
Sign up to request clarification or add additional context in comments.

2 Comments

its working now, thanks. seems like i was using d.js v11 code
Glad I could help :) (Would be cool if you'd mark the answer as accepted ^^ )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.