Discord.js: examples/permissions/adminbot.js broken

Created on 11 Aug 2016  路  10Comments  路  Source: discordjs/discord.js

$ node test.js
Ready to begin! Serving in 13 channels
E:\plasma!\plasma-chan\discord\test.js:31
                var role = msg.server.roles.get("name", "good people");
                                     ^

TypeError: Cannot read property 'roles' of undefined
    at Client.<anonymous> (E:\plasma!\plasma-chan\discord\test.js:31:24)
    at emitOne (events.js:96:13)
    at Client.emit (events.js:188:7)
    at WebSocket.websocket.onmessage (E:\plasma!\plasma-chan\discord\node_modules\discord.js\lib\Client\InternalClient.js:90:15)
    at WebSocket.onMessage (E:\plasma!\plasma-chan\discord\node_modules\ws\lib\WebSocket.js:442:14)
    at emitTwo (events.js:106:13)
    at WebSocket.emit (events.js:191:7)
    at Receiver.ontext (E:\plasma!\plasma-chan\discord\node_modules\ws\lib\WebSocket.js:841:10)
    at E:\plasma!\plasma-chan\discord\node_modules\ws\lib\Receiver.js:536:18
    at Receiver.applyExtensions (E:\plasma!\plasma-chan\discord\node_modules\ws\lib\Receiver.js:371:5)
bug

Most helpful comment

Although the example code should have that check prepared

All 10 comments

Your bot probably got DM'd, hence no server.
To avoid this check for a server and return if the command is reliant on one, for example like this
if(!msg.server) return;

Although the example code should have that check prepared

@GexoXYZ how do i fix it exactly?

@SloppierKitty7 check if the message from a Direct Message using that line of code he gave in your on message handler.

@SloppierKitty7 Is the issue resolved or not yet? Just wondering so, I can maybe help yah.

@developerCodex i still have the issue. @briantanner "message handler"?

bot.on('message', function (msg) { this is what I was referring to

@briantanner nope still not working. could you send me a whole example of a bot that has a command to create a role and give role?

@SloppierKitty7 in case you still need it, this is just the minimal code for the message event handler. The rest of the code would just be initializing the bot and logging in. This handler will create the role and add the user to it at the same time. You can split it out to two separate commands if you want. I'm not sure what happens with duplicate roles or something though.

bot.on('message', msg => {
  if (msg.content.toLowerCase() === 'addme' && msg.server) {
    msg.server.createRole({
      name: 'Random Role',
      permissions: [
        'attachFiles',
        'sendMessages',
      ],
      mentionable: true,
    }).then((role) => {
      role.addMember(msg.author);
    }).catch((error) => {
      // do some error handling
    });
  }
});

Fixed in 9.0.2 (by that I mean it's removed at the moment but will be added soon)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smchase picture smchase  路  3Comments

iCrawl picture iCrawl  路  3Comments

xCuzImPro picture xCuzImPro  路  3Comments

Lombra picture Lombra  路  3Comments

DatMayo picture DatMayo  路  3Comments