0
votes

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!

2

2 Answers

0
votes

you need to replace your method named:

if (msg.content.startsWith(prefix)) return;

With this:

if (!msg.content.startsWith(prefix)) return;

I think than it should work.

0
votes

You are making your command EXIT if it starts with a prefix. The return; function will terminate a function and make it return a certain value, or it will just stop it and ‘return’ to the main code. What you are doing is that you are making the bot return to the state of listening out for a command, instead of actually executing it.

A simple fix for this would be

if(!msg.content.startsWith(prefix)) return;

The ! mark at the start is very important. It means that if this is not the case, then return to listening out for new messages.