TypeError:无法读取未定义的属性“ id”(也为“ startWith”)

时间:2018-09-28 13:32:48

标签: javascript discord discord.js

我得到的错误:(我也收到与'startWith'相同的错误)。我不太确定自己在做什么错,我并不是一个真正的专家,所以我需要一些帮助。谢谢

THE COMMAND: *mycommand1 '    hello word*

这是我正在使用的代码:

TypeError: Cannot read property 'id' of undefined
at module.exports.message (/root/dvstin.xyz/events/message.js:6:21)
at Client.client.on.args (/root/dvstin.xyz/druggy2.js:14:39)
at Client.emit (events.js:187:15)
at MessageCreateHandler.handle (/root/dvstin.xyz/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
at WebSocketPacketManager.handle (/root/dvstin.xyz/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (/root/dvstin.xyz/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (/root/dvstin.xyz/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
at WebSocket.onMessage (/root/dvstin.xyz/node_modules/ws/lib/event-target.js:120:16)
at WebSocket.emit (events.js:182:13)
at Receiver._receiver.onmessage (/root/dvstin.xyz/node_modules/ws/lib/websocket.js:137:47)

2 个答案:

答案 0 :(得分:1)

您看到的错误是因为message没有authorcontent字段。

处理以下情况:

module.exports = message => {
   // Define client
   const Discord = require("discord.js");
   const client = message.client;

   console.log(message); // <--- Check this console. 
   // Check who, and the prefix being used

    if (message && message.author && message.author.id === "") return;
        if (message && message.content && !message.content.startsWith(client.settings.prefix)) return;
            // Define command
            const command = message.content
                .split(" ")[0]
                .slice(client.settings.prefix.length)
                .toLowerCase();
            // Define command paramaters
            const params = message.content.split(" ").slice(1);
            let cmd;
             if (client.commands.has(command)) {
            cmd = client.commands.get(command);
        }
        // If command, run that command
        if (cmd) {

            cmd.run(client, message, params);
        }

    };

查看从其发送message的代码,并尝试调试为什么它没有发送预期的字段。

答案 1 :(得分:1)

验证对象的字段时,始终应检查该对象。

接下来,是您的代码示例。

在:if (message.author.id === "") return;

您应该这样做:if (message.author && message.author.id === "") return;

如果不确定哪个消息不为null,则必须执行以下操作: if (message && message.author && message.author.id === "") return;

希望能对您有所帮助