I'm working on a discord bot with a command handler where I have all commands in their own files which I import into the main file. The issue I'm having with this is that I'm struggling to implement a prefix into the commands so all commands can just be triggered by text.
The way my command handler works is that I have this code in bot.js:
client.on('message', msg => {
if (msg.content.startsWith(prefix)) return;
//Splitting the message from the user
const args = msg.content.split(/ +/);
const command = args.shift().toLowerCase();
console.log(`Called comand: ${command}`);
//See if the commands folder has that command in it
if (!client.commands.has(command)) return;
//Try to execute the command. If we can't, we throw an error instead.
try {
client.commands.get(command).execute(msg, args);
} catch (error) {
console.error(error);
msg.channel.send("I hit an issue trying to issue that command.");
console.log("A Comnmand was issued, but I hit an issue trying to run it.");
}
In the top of that file I also have
const prefix = '!'
Then I have a file called index.js in the commands folder which looks like this:
module.exports = {
about: require('./about'),
help: require('./help'),
nokill: require('./nokill'),
animeme: require('./animeme'),
showmeme: require('./showmeme'),
roadmap: require('./roadmap'),
changelog: require('./changelog'),
wuvu: require('./wuvu'),
debug: require('./debug'),
dailyquote: require('./dailyquotes'),
dance: require('./dance'),
And for the commands, they look something like this:
const GihpyAPIModule = require('./command_modules/fetchGif.js');
module.exports = {
name: 'dance',
decription: 'Sends a dancing GIF',
execute(msg, args) {
msg.channel.send("Here's your dance!");
var searchPromise = GihpyAPIModule.getGif("dance");
searchPromise.then((gif) => {
msg.channel.send(gif);
})
}
After looking at the console the bot runs from, I've noticed that if I send a message with the prefix in it first, like !dance, it doesn't even pick it up. It just ignores it completely.
Here's what I've tried so far:
Implemented the prefix into each command file
Used this in bot.js:
const args = msg.content.slice(prefix.length).split(/ +/);
As the bot just ignores messages with ! in the start, the bot just cut messages down, so help would turn into elp.
Any advice is greatly appreciated!