无法读取null的属性“ hasRole”

时间:2018-09-08 12:37:43

标签: javascript node.js discord.js

我试图听dm,但是您一定不能担任选定的角色。

if (!message.member.hasRole('447778989914718218')) {
  if (message.channel.type == "dm") {
    if (message.content == "1") {

      client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778997766586380')
      client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('478930168875057152')
      client.guilds.get('447726259246727168').members.get(message.author.id).addRole('447778989914718218')
      //message.delete();

    } else if (message.content == "2") {

      //var server = client.guilds.get("447726259246727168");
      //server.channels.find(channels => channels.name === "bot-test").send(`nice `)
      client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778989914718218')
      client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('478930168875057152')
      client.guilds.get('447726259246727168').members.get(message.author.id).addRole('447778997766586380')
      message.channel.delete();

    } else if (message.content == "3") {

      client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778989914718218')
      client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778997766586380')
      client.guilds.get('447726259246727168').members.get(message.author.id).addRole('478930168875057152')

    }
  }
}

但是没有错误:

(node:15292) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'hasRole' of null
 (C:\Users\Megaport\Desktop\bot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\Megaport\Desktop\bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\Megaport\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\Megaport\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\Megaport\Desktop\bot\node_modules\ws\lib\event-target.js:120:16)
    at emitOne (events.js:116:13)
    at WebSocket.emit (events.js:211:7)
    at Receiver._receiver.onmessage (C:\Users\Megaport\Desktop\bot\node_modules\ws\lib\websocket.js:137:47)
    at Receiver.dataMessage (C:\Users\Megaport\Desktop\bot\node_modules\ws\lib\receiver.js:409:14)
    at Receiver.getData (C:\Users\Megaport\Desktop\bot\node_modules\ws\lib\receiver.js:347:12)
    at Receiver.startLoop (C:\Users\Megaport\Desktop\bot\node_modules\ws\lib\receiver.js:143:16)
    at Receiver.add (C:\Users\Megaport\Desktop\bot\node_modules\ws\lib\receiver.js:117:10)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at TLSSocket.Readable.push (_stream_readable.js:208:10)
    at TLSWrap.onread (net.js:597:20)
(node:15292) 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(). (rejection id: 1)
(node:15292) [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.

2 个答案:

答案 0 :(得分:0)

通常.hasRole不存在,如果您没有在任何地方定义它。

尝试使用以下代码:

if (!message.member.roles.has('447778989914718218')) {    
    if (message.channel.type == "dm") {

        if (message.content == "1") {


            client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778997766586380')
            client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('478930168875057152')
            client.guilds.get('447726259246727168').members.get(message.author.id).addRole('447778989914718218')
            //message.delete();
        } else
        if (message.content == "2") {

            //var server = client.guilds.get("447726259246727168");
            //server.channels.find(channels => channels.name === "bot-test").send(`nice `)
            client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778989914718218')
            client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('478930168875057152')
            client.guilds.get('447726259246727168').members.get(message.author.id).addRole('447778997766586380')
            message.channel.delete();
        } else  
        if (message.content == "3") {
            client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778989914718218')
            client.guilds.get('447726259246727168').members.get(message.author.id).removeRole('447778997766586380')

            client.guilds.get('447726259246727168').members.get(message.author.id).addRole('478930168875057152')

        }
    }
}

答案 1 :(得分:0)

message.member在以下情况下可以为空

  • 没有公会存在(消息以DM发送)
  • GuildMember不会被破坏(当您的机器人被很多人使用时的常见问题),您可以使用Guild#fetchMember(或master上的Guild.members#fetch)进行修复,这将在您解决诺言时返回成员Object < / li>
  • 该消息是每个Webhook发送的