TypeError: Cannot read property 'execute' of undefined Discord Bot js

My problem is the following: when compiling, I get the error that the property 'execute' is not defined. What I'm trying to do is open a file that is in another folder and dock it in the if, I was guided by the command handling documentation, I don't know if the error is in the other file which is called 'ping.js'. I recently started, so I don't fully understand it. The main code is as follows:

const Discord = require('discord.js');
const { token, default_prefix } = require('./conf.json');
const client = new Discord.Client();
const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}


client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', async message => {
    if (!message.content.startsWith(default_prefix) || message.author.bot) return;

    const args = message.content.slice(default_prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping') {
        client.commands.get('ping').execute(message, args);
    }
});

client.login(token);

While the 'ping.js' code is:

const Discord = require('discord.js');

module.exports = {
    description: "Get the latency of the bot.",
    usage: {},
    examples: {},
    aliases: [ "pong", "latency", "uptime" ],
    permissionRequired: 0,
    checkArgs: (args) => !args.length
  }

module.exports.run = async function (client, message, args, config, gdb, prefix, permissionLevel, db) {
    let botMsg = await message.channel.send("Pinging")
                    botMsg.edit({ 
                    embed: {
                    name: "ping",
                    title: "📶 Ping",
                    color: 0x2ed32e,
                    description: [
                        "**Server**: `" + (message.createdAt - message.createdAt) + "ms`",
                        "**API**: `" + Math.round(client.ws.ping) + "ms`",
                        "**Uptime**: `" + msToTime(client.uptime) + "`"
                    ].join("\n"),
                    footer: { text: "Requested by " + message.author.tag, icon_url: message.author.displayAvatarURL }
                }
            })
        }

        function msToTime(ms) {...
        }

It works, but if I add it directly to the main code, but I don't want that. If you have any ideas or know the solution, I would appreciate it.

Upvotes: 0

Views: 7062

Answers (2)

Florence Winters
Florence Winters

Reputation: 471

It's saying execute is undefined because you didn't define execute it in ping.js.

You can do either:

  • In ping.js change module.exports.run to module.exports.execute
  • OR in your main file, change client.commands.get('ping').execute to client.commands.get('ping').run

The reasoning for the is when calling command.execute() you are attempting to call a function named 'execute' in the command's module. Since you named it run instead of execute, it looks for the wrong function and doesn't find it.

Upvotes: 2

user13429955
user13429955

Reputation:

That's because you named it run and not execute on this line:

module.exports.run = async function ()

change it to execute and it should work fine, if you wanna keep the keyword run, instead of client.commands.get('ping').execute(message, args), use client.commands.get('ping').run(message, args)

Also I should mention you have a lot of parameters:

execute function (client, message, args, config, gdb, prefix, permissionLevel, db) {
   //...
}

Any after args will be undefined as you only pass in messsage and args, here:

client.commands.get('ping').execute(message, args)

Upvotes: 1

Related Questions