0
votes

Lately i faced the Challenge of Coding my own Discord bot, I wanted to make a command that shows the ping of the bot. If i have the command in my main file it works but if I put it in a seperate file it shows the error. Thats my main File. Quick UPDATE! apparently the problem is only for that specific line because the exact same works for the next command

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

const client = new Discord.Client();
const prefix = '!';

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}`);}
 // Define each file inside /commands/ folder
 for(const file of commandFiles){
   // Set the command equal to the file
    const command = require(`./commands/${file}`);

     // Add the command to the collection
     client.commands.set(command.name, command);}




   client.once('ready', () => {
      console.log('Bor has entered the Bar')
});
        
client.on('message', message =>{
      if(!message.content.startsWith(prefix) || message.author.bot) return;
      
      const args = message.content.slice(prefix.length).split(/ +/);
      const command = args.shift().toLowerCase();


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

      else if(command =='invite'){
         message.channel.send('here is a invite link :) https://discord.gg/yPKDtzBv7s')   //discord invite
      }

      else if(command =='random'){
         message.channel.send('https://www.youtube.com/watch?v=pct1uEhAqBQ')      //random Vid
      }

      else if(command =='rick'){                                                // rick Astley never gonna give u aup
         message.channel.send('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
      }
   })

client.login('MY LOGIN');

And this is the ping code

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.execute = 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) {}

And the error: C:\Users...\Desktop\DiscordBot\main.js:37 client.commands.get('ping').execute(message, args); ^

TypeError: Cannot read property 'execute' of undefined at Client. (C:\Users...\Desktop\DiscordBot\main.js:37:37) at Client.emit (events.js:315:20) at MessageCreateAction.handle (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14) at Object.module.exports [as MESSAGE_CREATE] (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31) at WebSocketShard.onPacket (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) at WebSocketShard.onMessage (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) at WebSocket.onMessage (C:\Users...\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:132:16) at WebSocket.emit (events.js:315:20) at Receiver.receiverOnMessage (C:\Users...\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:825:20)

1

1 Answers

0
votes

Your filter function is incorrect. Your current implementation of endsWith will always return false as you did not pass any parameter. To change this, add ".js" as an argument so it looks for JavaScript files:

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

EDIT:

Another problem is that your module.exports is missing the name property. It should look like this:

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

Now when you do this, it should work:

client.commands.set(command.name, command);

Before, command.name was just undefined.