【问题标题】:Discord.js Bot throwing error on start. How can I fix it?Discord.js Bot 在启动时抛出错误。我该如何解决?
【发布时间】:2021-03-11 10:18:42
【问题描述】:

每当我启动机器人时,它都会引发错误:

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)
    }
}

【问题讨论】:

标签: javascript node.js discord.js


【解决方案1】:

有几件事需要检查

确保您已针对机器人设置命令。这是一个示例,我从文件中导入所有命令并将它们设置为针对机器人。命令文件只包含我所有命令的数组,但您可以只为您拥有的一个命令运行它。

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!');
    }
});

【讨论】:

    猜你喜欢
    • 2022-07-12
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2020-03-31
    • 2021-11-21
    • 2021-12-17
    • 2019-06-28
    相关资源
    最近更新 更多