Search code examples
node.jsif-statementdiscord.js

Ways to include custom error messages in shorthand if-else sanity checks Discord JS


I am a beginner and probably, this is a stupid question. I am writing a command handler for a discord.js bot. Every time, a user sends a message starting with the correct command prefix, I check whether the invoke is in an Enmap of possible commands. Currently, it looks like this:

const command = client.commands.get(invoke);
if(!command) return;
...

I would like to keep this shorthand way of writing those sanity checks, but I would like to inform the user that there is no command with this name.


Solution

  • I wouldn't recommend telling the user if a command is invalid, but here's how you can do it:

    const command = client.commands.get(command);
    if (!command) return message.channel.send("Invalid Command.");