0

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)
    }
}
NullDev
  • 6,739
  • 4
  • 30
  • 54
  • Where is the code that's throwing the error? You only provided the code for the `execute` itself. – NullDev Mar 11 '21 at 10:22
  • Does this answer your question? [JavaScript "cannot read property "bar" of undefined](https://stackoverflow.com/questions/8004617/javascript-cannot-read-property-bar-of-undefined) – Patrick Hollweck Mar 11 '21 at 10:23

1 Answers1

1

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!');
    }
});
Dharman
  • 30,962
  • 25
  • 85
  • 135