Cannot read property 'roles' of undefined Discord.js
Cannot read property 'roles' of undefined Discord.js
I have an issue with my discord bot which will be a board game bot (I hope). So, I try to assign to users their player number with roles like the first player will have the role 'Player 1' etc... But I always have the same issue:
let role = message.guild.roles.find("480091776468647936");
^
TypeError: Cannot read property 'roles' of undefined
at Client. (C:UsersalantDesktopInformatique - DéveloppementShadow Botshadow_bot.js:54:32)
at emitOne (events.js:121:20)
at Client.emit (events.js:211:7)
at MessageCreateHandler.handle (C:node_modulesdiscord.jssrcclientwebsocketpacketshandlersMessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:node_modulesdiscord.jssrcclientwebsocketpacketsWebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:node_modulesdiscord.jssrcclientwebsocketWebSocketConnection.js:330:35)
at WebSocketConnection.onMessage (C:node_modulesdiscord.jssrcclientwebsocketWebSocketConnection.js:293:17)
at WebSocket.onMessage (C:node_moduleswslibEventTarget.js:99:16)
at emitOne (events.js:116:13)
at WebSocket.emit (events.js:211:7)
I already tried so search on Google by myself, but I'm making this bot as a hobby and don't have (yet) a deep knowledge on JavaScript so I expect some help. (Sorry if my English is bad, I'm not from an English speaking country)
bot.on('message', function(message)
var message = message.content;
if (message === "joueur 1")
membre = message.author;
let role = message.guild.role.find("480091776468647936");
membre.addRole(role);
message.channel.send("Vous etas doreavant le joueur 1 ! ")
)
1 Answer
1
First, you have to define myRole,
// get role by ID
let myRole = message.guild.roles.get("264410914592129025");
or
// get role by name
let myRole = message.guild.roles.find("name", "Moderators");
Choose one of these.
if (command === "addroletome")
// Add the role!
message.author.addRole(role).catch(console.error);
message.channel.send("Successfully assigned the role! ")
Bonus : You can easily modify it to remove the role
// Remove a role!
message.author.removeRole(role).catch(console.error);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Images of text should never be used. They aren't searchable, they aren't accessible, and they are hard to read on smaller screen sizes. I've typed it out for you, but in the future you need to copy any code into your question and format it correctly using the built in formatting controls.
– Jason Aller
yesterday