繁体   English   中英

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

[英]TypeError: Cannot read property 'id' of undefined (also 'startWith')

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

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)

这是我正在使用的代码:

module.exports = message => {
// Define client
const Discord = require("discord.js");
const client = message.client;
// Check who, and the prefix being used

if (message.author.id === "") return;
if (!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没有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位置处的代码,并尝试调试为什么它没有发送预期的字段。

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

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

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

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

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

希望对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM