2
votes

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:\Users\alant\Desktop\Informatique - Développement\Shadow Bot\shadow_bot.js:54:32)
at emitOne (events.js:121:20)
at Client.emit (events.js:211:7)
at MessageCreateHandler.handle (C:\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:330:35)
at WebSocketConnection.onMessage (C:\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:293:17)
at WebSocket.onMessage (C:\node_modules\ws\lib\EventTarget.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 ! ")
    }
})
2
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
Okay, no problem thanksAlan 'MrSmooth'
@Alan is the message sent on a Server or a Direct Message?André
@André which message ? The "Vous êtes dorénavant .... " Must be sent on the channel, but the error message on the consoleAlan 'MrSmooth'
I meant the message that you're sending the command. If you're sending on a Direct Message, the message wouldn't have a guildAndré

2 Answers

3
votes

try this:

let myRole = message.guild.roles.find(x => x.name === "Moderators");

Edit: If you are in discord.js v12 use this:

let role = message.guild.roles.cache.find("480091776468647936");
1
votes

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);