我正在使用 discord.js 开发一个不和谐的机器人,但我遇到了这个我不知道如何解决的问题 我做了一个命令来测试机器人是否在线,这是它的代码 就在我展示任何代码之前,我想说我对 javascript 有点陌生,所以这个问题让我很困惑
module.exports = {
name : "onstatus",
description : "",
execute(message, args) {
message.channel.send("online");
}
}
所以当我输入 !-onstatus 我期望发生的是机器人响应。但它没有,它在 vs 代码中显示了这个错误
TypeError: Cannot read property 'channel' of undefined
我有另一个有效的命令,但由于某种原因,这不是有效的代码
module.exports = {
name : 'ping',
description : '',
execute(message, args){
message.channel.send('pong');
}
}
当我运行这个并输入 !-ping 时它会起作用
如果你想要整个机器人的代码,你去
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = "!-";
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('leobot is currently online ');
});
client.on('message' , message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command == 'ping') {
client.commands.get('ping').execute(message, args);
} else if (command == 'developerofleobot'){
client.commands.get('developerofleobot').execute(message.args);
} else if (command == 'onstatus'){
client.commands.get('onstatus').execute(message.args);
}
});
client.login('######');
答案 0 :(得分:0)
您的代码
newArr
逗号将是消息而不是点之后的下一个
newArr