Javascript Discord.js Bot在启动时抛出错误。我怎样才能修好它?

Javascript Discord.js Bot在启动时抛出错误。我怎样才能修好它?,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,每当我启动bot时,它都会抛出一个错误: client.commands.get('avatar').execute(message,args);^'执行(未定义的) const{MessageEmbed}=require(“discord.js”); module.exports={ 名称:'阿凡达', 描述:'显示您或某人的化身', 执行:(消息,参数)=>{ 如果(message.channel.type==='dm'){ 返回message.channel.send('您不能在DMs中执

每当我启动bot时,它都会抛出一个错误:

client.commands.get('avatar').execute(message,args);^'执行(未定义的)

const{MessageEmbed}=require(“discord.js”);
module.exports={
名称:'阿凡达',
描述:'显示您或某人的化身',
执行:(消息,参数)=>{
如果(message.channel.type==='dm'){
返回message.channel.send('您不能在DMs中执行命令!');
}
让member=message.indications.users.first()| | message.author;
让avatar=member.displayAvatarURL({dynamic:true,size:1024})
const embed=new MessageEmbed()
.setAuthor(`${member.tag}`、`${member.avatarURL()}`)
.setTitle(`${member.username}'Avatar:`)
.setImage(`${avatar}`)
message.channel.send(嵌入)
}
}

有几件事需要检查

确保已针对bot设置了命令。下面是一个示例,我从一个文件导入所有命令,并针对bot设置它们。commands文件只包含我的所有命令的数组,但是您可以只为您拥有的一个命令运行它

import { Client, Collection } from 'discord.js';
import { botCommands } from './commands';

const bot = new Client();
bot.commands = new Collection();

Object.values(botCommands).map(command => bot.commands.set(command.name, command));
您还可以通过检查bot.commands.has(command)的结果来检查bot在运行之前是否设置了命令

bot.on('message',(msg:any)=>{
常量args=msg.content.split(+/);
const命令:string=args.shift().toLowerCase();
info(`Called command:${command}`);
如果(!bot.commands.has(command))返回;
试一试{
const botCommand=bot.commands.get(命令);
if(botCommand)botCommand.execute(msg,args);
}捕获(错误){
控制台错误(error);
msg.reply('尝试执行该命令时出错!');
}
});

抛出错误的代码在哪里?您只提供了
执行
本身的代码。这是否回答了您的问题?