TypeError: Невозможно прочитать свойство 'id' из undefined (также 'startWith') - PullRequest
0 голосов
/ 28 сентября 2018

Ошибка, которую я получаю: (Я также получаю ту же ошибку с '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);

}

};

Ответы [ 2 ]

0 голосов
/ 28 сентября 2018

При проверке поля объекта ВСЕГДА вы должны проверять объект.

Далее приведен пример вашего кода.

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

Выдолжен сделать: if (message.author && message.author.id === "") return;

И если вы не уверены, какое сообщение не является нулевым, то вы должны сделать: if (message && message.author && message.author.id === "") return;

Я надеюсь быть полезным

0 голосов
/ 28 сентября 2018

Ошибка, которую вы видите, состоит в том, что message не имеет поля author или content.

Обработайте сценарии, как показано ниже:

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 и пытается отладить, почему он не отправляет ожидаемые поля.

...