我正在用Java编写乱码机器人,对于其中一个命令(!server,显示服务器名称和成员总数),错误不断弹出。我当前的代码是;
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.on('message', message =>{;
if(message.author.bot) return;
if (message.content.startsWith("!")){
const prefix = ("!")
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command == 'help'){
client.commands.get('!help').execute(message, args);
}
if(command == 'server'){
client.commands.get('!server').execute(message, args);
}
if(command == 'blacklist'){
client.commands.get('!blacklist').execute(message, args);
}
if(command == 'safe'){
client.commands.get('!safe').execute(message, args);
}
};
错误消息是
client.commands.get('!server').execute(message, args);
^
TypeError: Cannot read property 'execute' of undefined
// !server.js File
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: '!server',
description: "!server",
execute(message, args){
message.react('?')
const serverEmbed = new Discord.MessageEmbed()
serverEmbed.setTitle(`Server Stats`)
serverEmbed.addField("Server Name", message.guild.name);
serverEmbed.addField("Total Members", message.guild.memberCount);
serverEmbed.addField("Online Members", message.guild.members.cache.filter(member => member.presence.status !== "offline").size);
serverEmbed.addField("Offline Members", message.guild.members.cache.filter(member => member.presence.status == "offline").size);
message.channel.send(serverEmbed);
}
}
该代码适用于其他3个命令!有帮助吗?