簡體   English   中英

類型錯誤:每當我在不和諧的情況下向我的機器人輸入 PM 時,都無法讀取未定義的屬性“id”

[英]TypeError: Cannot read property 'id' of undefined whenever i type a PM to my bot on discord

剛開始編寫我的機器人時,我突然收到錯誤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)```

我真的與 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路徑錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM