![](/img/trans.png)
[英]Is there any way i can fix my discord bot, which i`m creating discord.js
[英]Discord.js Bot throwing error on start. How can I fix it?
每当我启动机器人时,它都会引发错误:
client.commands.get('avatar').execute(message,args); ^ 未定义的“执行”)
const { MessageEmbed } = require("discord.js");
module.exports = {
name: 'avatar',
description:'displays your or someones avatar',
execute: (message, args) => {
if(message.channel.type === 'dm') {
return message.channel.send('You cant execute commands in DMs!');
}
let member = message.mentions.users.first() || message.author;
let 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(embed)
}
}
有几件事要检查
确保您已针对机器人设置命令。 这是一个示例,我从文件中导入所有命令并将它们设置为针对机器人。 命令文件只包含我所有命令的数组,但您可以只为您拥有的一个命令运行它。
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.on('message', (msg: any) => {
const args = msg.content.split(/ +/);
const command: string = args.shift().toLowerCase();
console.info(`Called command: ${command}`);
if (!bot.commands.has(command)) return;
try {
const botCommand = bot.commands.get(command);
if (botCommand) botCommand.execute(msg, args);
} catch (error) {
console.error(error);
msg.reply('There was an error trying to execute that command!');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.