0
votes

So I'm using this command handler to make my discord bot more rugged but I'm completely blanking on how to send messages with the external commands; particularly defining msg. Heres my command handler code:

const Discord = require('discord.js');
const prefix = "%"
const fs = require('fs');

const client = new Discord.Client();
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 connected!")
    client.user.setPresence({
        status: 'online',
        activity: {
            name: 'Online',
            type: 'PLAYING',
            url: 'https://www.google.com/'
        }
    })
})

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

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

    if (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login("<insert my token here>")

And I don't have any 'ping.js' code, as everything I've thought to try and have found online hasn't worked. Thanks for the help!

1
It looks like your index.js file is setup correctly based on the Discord.js guide. Your client on message event handler looks at each message and checks for the message prefix, in your case %. Then the code splits the message and checks if a command name matches the collection. If it finds a match it forwards that message to the appropriate command. Can your provide more info on the error or update your question to include the command file you are trying to call?Lateralus
Sorry, I didn't specify my question very well. In 'ping.js' msg is undefined, and I can't quite figure out how to define it. Can I define it with u function? Any help would be appriciated, thanks!Dx_ed

1 Answers

0
votes

I personally do it like this

fs.readdir("./commands/", (err, files) => {
  if(err) console.error(error)
  let jsfiles = files.filter(f => f.split(".").pop() === "js")
  if (jsfiles.length <= 0) {
    return console.log("No commands to log in FOLDER NAME")
  }
  console.log(`Loading ${jsfiles.length} commands from FOLDER NAME...`)
  jsfiles.forEach((f,i) => {
    let props = require(`./commands/${f}`)
    console.log(`${i + 1}: ${f} loaded!`)
    client.commands.set(f, props)
  })
})

then in the client.on message

let cmd = client.commands.get(command+".js")
    if (cmd) cmd.run(bot, message, args, prefix)

in ping.js try this

const Discord = require("discord.js");
const client = new Discord.Client();
module.exports.run = async (client, message, args, prefix) => {
  // command code here
};
module.exports.help = {
  name: "command name",
  usage: "command usage",
};