[英]How to fix cannot read property “send” of undefined
这个问题可能有一些重复,但那里的代码不同,没有一个答案对我有用。 我是 discord.js 的新手,所以这可能只是一个愚蠢的错误。
这是我的代码 -
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 + ')');
});
client.on('message', function(message) {
if (message.startsWith('^')) { //condition is false, even though message starts with ^
if (message.startsWith('^ping')) {
message.mentions.users.forEach((k, v) => {
message.channel.send('Hello,' + v + '!');
});
} else if (message.startsWith('^dice')) {
message.channel.send('You rolled a a' + Math.floor((Math.random() * 100) + 1) + "!");
} else {
message.channel.send("Command not found.");
}
} else {
message.channel.send("debug") //error in this line
}
});
我真的不知道如何解决这个问题,所以我尝试过的所有解决方案都会导致这个错误。 此外,如果您看到此代码的任何其他修复,请指出。 这是错误:
message.channel.send("debug")
^
TypeError: Cannot read property 'send' of undefined
at DiscordClient.<anonymous> (C:\Users\user\Documents\UltraBot\bot.js:32:25)
at DiscordClient.emit (events.js:315:20)
at DiscordClient.handleWSMessage (C:\Users\user\Documents\UltraBot\node_modules\discord.io\lib\index.js:1854:11)
at WebSocket.emit (events.js:315:20)
at Receiver.ontext (C:\Users\user\Documents\UltraBot\node_modules\ws\lib\WebSocket.js:841:10)
at C:\Users\user\Documents\UltraBot\node_modules\ws\lib\Receiver.js:536:18
at Receiver.applyExtensions (C:\Users\user\Documents\UltraBot\node_modules\ws\lib\Receiver.js:371:5)
at C:\Users\user\Documents\UltraBot\node_modules\ws\lib\Receiver.js:508:14
at Receiver.flush (C:\Users\user\Documents\UltraBot\node_modules\ws\lib\Receiver.js:347:3)
at Receiver.finish (C:\Users\user\Documents\UltraBot\node_modules\ws\lib\Receiver.js:541:12)
问题是您使用Discord.js的代码,同时使用Discord.io ZEFE90A8E604A7C6B7D8ZD843 您发送和接收消息的方式大不相同。 我建议您切换到 discord.js,因为这就是您编写代码的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.