[英]TypeError: Cannot read property 'id' of undefined whenever i type a PM to my bot on discord
While just starting out coding my bot, I suddenly receive the error Cannot read property 'id' of undefined
whenever I private message the bot.刚开始编写我的机器人时,我突然收到错误
Cannot read property 'id' of undefined
每当我给机器人发私信时。 The code and error stack are below.代码和错误堆栈如下。
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)```
I dont really have anything to do with discord.io, but looking at your error, it seems, that我真的与 discord.io 没有任何关系,但看着你的错误,似乎
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
assigns undefined
to your var bot
.将
undefined
分配给您的var bot
。 At least thats what your error message tells you.至少那是你的错误信息告诉你的。
Looking at the package's example code from from https://www.npmjs.com/package/discord.io从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"
});
}
});
...I cant really find anything that you would've done wrong... so my guess is, that ......我真的找不到你会做错的任何事情......所以我的猜测是,那
var Discord = require('discord.io');
is getting the wrong path to discord.io
进入
discord.io
路径错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.