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!
index.js
file is setup correctly based on theDiscord.js
guide. Your clienton 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