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