简体   繁体   中英

Discord.js Bot throwing error on start. How can I fix it?

Whenever I boot up the bot it throws an error:

client.commands.get('avatar').execute(message,args); ^ 'execute' of undefined)

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

There are a few things to check

Make sure you have set your commands up against the bot. Here is an example where I import all of my commands from a file and set them against the bot. The commands file just contains an array of all of my commands but you could just run it for the one command you have.

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

You can also check that the bot has the command set up before running it by checking the result of 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!');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM