0

I'm trying to make a discord bot using discord.js and repl.it, but for the ping command, I keep getting this error that says "Cannot read property 'execute' of undefined". I've looked at others with the same problem and tried their solutions, but none worked. Here is the code for my index.js file:

const Discord = require('discord.js');
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.on('ready', () => {
    console.log(`Bot is ready.`)

    client.user.setActivity("b!help", {type: "LISTENING"});
});

client.on('message', msg => {
    if (!msg.content.startsWith(process.env.PREFIX) || msg.author.bot) return;

    const args = msg.content.slice(process.env.PREFIX.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (command == 'ping') {
        client.commands.get('ping').execute(client, msg, args);
    } else if (command == 'avatar') {
        client.commands.get('avatar').execute(client, msg, args);
    } 
    // do the same for the rest of the commands...
});

client.login(process.env.TOKEN)

and here is the code for my ping.js file:

const client = require('../index.js');
module.exports = {
    name: 'ping',
    description: 'Ping pong',
    execute(client, msg, args) {
        msg.channel.send(`Latency is ${Date.now() - msg.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
  },
};

Any help is appreciated! I'm still kinda new to coding and everything o.o

Olive
  • 1
  • What version of Discord.js are you using? These `client.commands` methods don't seem to exist in the stable version. – kdau May 29 '21 at 23:40
  • @kdau I am using version 12.5.3 – Olive May 29 '21 at 23:57
  • 1
    Ah, I mistook your code for referencing a new `ClientApplication#commands` object coming in v13. I see now that this is a collection you added to the `Client` instance yourself. To see what commands you actually loaded from files, put the following after the `for` loop and check the output: `console.log(Array.from(client.commands.keys()));` – kdau May 30 '21 at 00:12
  • Just out of curiosity, why did you require your index.js in the ping module? – user15517071 May 30 '21 at 00:16
  • Adding on to the comment above me, `client` should already be accessible through the passed parameter of the execute function, or `message.client`. That client variable is just the entire index.js file – Elitezen May 30 '21 at 01:56
  • Oh! I just deleted the require index.js part and it's worked now! I think I had it before because I only had msg and args as my parameters, but forgot to delete it when I added client. Thanks for all the help!! – Olive May 30 '21 at 02:55

1 Answers1

0

quite an easy fix

module.exports = {
    name: 'ping',
    description: 'Ping pong',
    execute: (client, msg, args) { // the colons are very important in objects
        msg.channel.send(`Latency is ${Date.now() - msg.createdTimestamp}ms. API Latency is ${Math.round(client.ws.ping)}ms`);
  },
};

see you're exporting an object yes? {}, An javascript object is simply a name: value pair, the error with your code is you forgot the : between execute and (client, msg, args) the function parameters.

moreover you cannot use exported modules from your index.js because it would throw circular dependency error, you already passed in client as a function parameter in index.js so you really don't need to define client again in your command file

If you don't understand what command handling is, make sure to read up Discord.js command handlers , its very easy :)

hope this helped, goodluck

AnanthDev
  • 1,605
  • 1
  • 4
  • 14