HellBoundDeadman
HellBoundDeadman

Reputation: 25

Trying to get my Bot to detect an Inavlid Command

I'm trying to get my bot to detect when a command is not valid (in the list) and return a message saying to the user that it's inavalid, I've tried a heap of methods and It keeps saying the Invalid Command message for Every message, even the Valid commands.

    if (message.content.startsWith(`${prefix}`)) {
        let commands = ["help", "mod", "ping", "serverinfo", "8ball", "suggest", "coinflip", "botinfo", "avatar", "info", "serverlogo", "ssu", "close"];

        if (!message.content.includes(commands)) {
            return message.channel.send(`Sorry it looks like you have entered an invalid command, Please use ${prefix}help to get a list of useable commands`)
        }
    }

Upvotes: 0

Views: 170

Answers (2)

tonyfarney
tonyfarney

Reputation: 300

You need to remove the prefix from the message.content and you are checking in the wrong way:

if (message.content.startsWith(`${prefix}`)) {
    let commands = ["help", "mod", "ping", "serverinfo", "8ball", "suggest", "coinflip", "botinfo", "avatar", "info", "serverlogo", "ssu", "close"];

    const command = message.content.replace(prefix, "");
    if (!commands.includes(command)) {
        return message.channel.send(`Sorry it looks like you have entered an invalid command, Please use ${prefix}help to get a list of useable commands`)
    }
}

Upvotes: 0

Samathingamajig
Samathingamajig

Reputation: 13283

You're testing the wrong way around. You should be testing if the array includes the message string, not if the message string includes the array.

if (!commands.includes(message.content)) { // This doesn't work exactly

Another thing you need to do is remove the prefix from the message content and every subsequent argument.

if (!commands.includes(message.content.replace(prefix, "").split(' ')[0])) { // This first removes the first instance of the prefix, then gets the first argument, which is the command

Upvotes: 1

Related Questions