簡體   English   中英

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM