I am having trouble with getting the right information from message.mentions.roles.first() and message.mentions.members.first()
This is my command handler in Index.js
bot.on('message', async (message, guild, helpList, roles) => {
if (!message.content.startsWith(prefix) || message.author.bot || !message.member.hasPermission('MANAGE_ROLES')) return;
const args = message.content.slice(prefix.length).split(' ') ;
const command = args.shift().toLowerCase();
const aRole = message.mentions.roles.first();
const Role = aRole.id
const name = args.join(' ');
const User = message.mentions.members.first();
if (!Role && command !== 'create' && command !== 'give' && command !== 'take' && command !== 'help' && command !== 'list') {return message.channel.send('Sorry, that role was not found.')}
if (!User && (command == 'give' || command == 'take') ) {return message.reply ("That name doesnt match a user.");}
try{
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
} catch(e){
console.log(e)
}
});
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
} catch(e){
console.log(e)
}
});
Here is my take.js, a command to remove a role from the specified member.
module.exports = {
name: 'take',
description: '',
execute(message, Role, User, roles){
try{
User.roles.remove(Role);
return message.channel.send (`${Role} has been removed from ${User}'s list of rolls.`);
}catch(e){
console.log(e);
console.log(Role)
console.log(User)
}}}
This is the error I get.
TypeError: Cannot read property 'remove' of undefined
at Object.execute (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/commands/take.js:6:16)
at Client.<anonymous> (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/index.js:34:33)
at Client.emit (events.js:310:20)
at MessageCreateAction.handle (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
at WebSocketShard.onPacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:437:22)
at WebSocketShard.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:294:10)
at WebSocket.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:310:20)
711442214043254854
<@&711442214043254854> <@!271438275619586062>
Why is Role not working? Any help is greatly appreciated.
Actually in your code when you call execute
function here
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
The execute
function in take.js
will wait for these parameters :
In fact, in the order you give parameters to the execute
function, you are giving the variable name
and not User
. So instead of User
being a GuildMember it's a String and of course String.roles
is undefined
, that's why the error says cannot read property 'remove' of undefined
.
To fix your error you should modify
bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
to
bot.commands.get(command).execute(message, Role, User, name, guild, helpList);
(Look closely, I inverted name
parameter with User
parameter)
But wait, it's not finished
Another issue in your code is that you are declaring too much parameters.
bot.on("message", callback(Message))
will only call callback()
with one parameter (see docs here).
guild, helpList, roles
are always undefined
as parameters here.
So you should remove unnecessary parameters like that :
bot.on("message", (message) => {
...
}
And same for execute
function call, remove unused parameters :
bot.commands.get(command).execute(message, Role, User);
I think that's it. I hope I've been understandable :D