这个问题可能有一些重复,但是那里的代码不同,没有答案对我有用。我是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)
答案 0 :(得分:1)
问题是您在使用Discord.js软件包时正在为Discord.io使用代码。您发送和接收消息的方式大不相同。我建议您切换到discord.js,因为这就是您要编写的代码。