Reputation: 33
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
Upvotes: 0
Views: 317
Reputation: 1880
guildMember.roles
returns a guildMemberRoleManager
.has()
is a function, provided by a collection
So in order to access that function, you have to use .cache
(because it returns a collection
) in between:
rMember.roles.cache.has()
Upvotes: 3