TypeError: Невозможно прочитать свойство 'id' из undefined всякий раз, когда я пишу PM своему боту на разногласиях - PullRequest
0 голосов
/ 15 мая 2019

Когда я только начинаю кодировать своего бота, я неожиданно получаю сообщение об ошибке Cannot read property 'id' of undefined всякий раз, когда я отправляю боту личное сообщение. Код и стек ошибок приведены ниже.

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');

// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';

// Initialize Discord Bot
var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});

bot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(bot.username + ' - (' + bot.id + ')');
});

bot.on('message', function (user, userID, channelID, message, evt) {
    // Our bot needs to know if it will execute a command
    // It will listen for messages that will start with `!`
    if (message.substring(0, 1) == '!') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            // !ping
            case 'ping':
                bot.sendMessage({
                    to: channelID,
                    message: 'Pong!'
                });
            break;
            // Just add any case commands if you want to..
         }
     }
});
TypeError: Cannot read property 'id' of undefined

    at new Channel (C:\Users\PC\Desktop\Discord-Bot\node_modules\discord.io\lib\index.js:2529:25)

    at DiscordClient.handleWSMessage (C:\Users\PC\Desktop\Discord-Bot\node_modules\discord.io\lib\index.js:1889:33)

    at WebSocket.emit (events.js:189:13)

    at Receiver.ontext (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\WebSocket.js:841:10)

    at C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:536:18

    at Receiver.applyExtensions (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:371:5)

    at C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:508:14

    at Receiver.flush (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:347:3)

    at Receiver.finish (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:541:12)

    at Receiver.expectHandler (C:\Users\PC\Desktop\Discord-Bot\node_modules\ws\lib\Receiver.js:499:31)```

1 Ответ

0 голосов
/ 15 мая 2019

Я не имею ничего общего с discord.io, но, глядя на вашу ошибку, кажется, что

var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});

присваивает undefined вашему var bot.По крайней мере, так говорит ваше сообщение об ошибке.

Глядя на пример кода пакета из https://www.npmjs.com/package/discord.io

var Discord = require('discord.io');

var bot = new Discord.Client({
    token: "",
    autorun: true
});

bot.on('ready', function() {
    console.log('Logged in as %s - %s\n', bot.username, bot.id);
});

bot.on('message', function(user, userID, channelID, message, event) {
    if (message === "ping") {
        bot.sendMessage({
            to: channelID,
            message: "pong"
        });
    }
});

... Я не могу найти ничего, что вы бы сделали неправильно ... так что я думаю, что

var Discord = require('discord.io');

получает неправильный путь к discord.io

...