Creating a command handler system for Discord bot

I’m building a Discord bot and need help with creating a better command system. Right now I have a basic setup but it’s getting messy.

module.exports = {
  register: function(cmd, description, access, handler) {
    var cmdObj = {};
    cmdObj['command'] = cmd;
    cmdObj['desc'] = description;
    cmdObj['access'] = access;
    cmdObj['handler'] = handler;

    commandList.push(cmdObj);
  },

  execute: function(msg, botData) {
    var params = msg.split(' ');

    for (var j = 0; j < commandList.length; j++) {
      var cmd = commandList[j];

      if (cmd.command == params[0]) {
        checkPermission(cmd.access, botData, function() {
          console.log("EXECUTED");
          cmd.handler(botData, params);
        });
      }
    }
  },

  list: function() {
    return commandList;
  }
}

Here’s how I use it:

commands.register('echo', 'Echo back a message', null, function(botData, params) {
  if (params[1] == "-r") {
    bot.sendMessage({to: botData.channelID, message: "reversed"});
  } else {
    bot.sendMessage({to: botData.channelID, message: "normal"});
  }
});

The problem is handling flags like !echo -r -v -q. I want something that works like Unix commands but I’m not sure how to parse multiple flags properly.

For permissions, I have this table:

+----+-----------+
| id | role      | 
+----+-----------+
|  1 | user      |
|  2 | trusted   |
|  3 | moderator |
|  4 | admin     |
|  5 | owner     |
+----+-----------+

How can I make admins automatically have moderator permissions without manually adding every role? Should I use numeric levels instead?

What’s the best way to build a Unix-style command parser and handle hierarchical permissions?

for real! numeric levels r the way 2 go! makes stuff easier. and splitting flags with ‘-’ is a good idea. just be careful with unexpected inputs or it’ll get messy. keep it flexible!

Command parsing gets messy fast if you don’t plan ahead. Switch to numeric permissions where higher levels include everything below - just check userPermission >= commandRequiredLevel instead of individual roles. Way cleaner. For flags, use regex to pull them out first, then handle the leftover arguments. I build a parser function that spits out an object with flags and clean args - works great. Honestly though, check out minimist or yargs-parser instead of building your own. They catch edge cases you haven’t hit yet and save tons of time. Keep your command handlers doing actual work, not wrestling with input parsing.

I encountered similar challenges while developing my bot last year. For managing permissions, it’s effective to use numeric levels, allowing higher numbers to inherit all permissions from lower ones. Implement a check with if (userLevel >= requiredLevel) to simplify your hierarchy. Regarding flag parsing, I suggest creating a parser that first splits the input by spaces, treating anything prefixed with a - as a flag. You can merge flags like -rvq or keep them distinct as -r -v -q. It’s essential to construct a flags object that your handler receives along with any remaining arguments. A helpful approach was separating the argument parsing logic from command registration by defining a parseArgs() function, which returns both flags and positional arguments separately, streamlining your execute function and facilitating independent command testing.

I’ve built way too many command parsers from scratch - it’s a nightmare. Everyone suggests manual parsing but there’s a better way.

Skip the regex and custom parsers. Automate everything - command parsing, permission checks, response routing. Keep it out of your bot code entirely.

I set up workflows where Discord messages trigger automated parsing that handles flags, validates permissions against your database, and runs the right handlers. No more messy string splitting or permission cascading in JavaScript.

For permission hierarchy, let automation handle numeric comparison. The workflow checks user level against required permissions and routes from there. Flags like -r -v -q get parsed and passed as clean objects to your handlers.

Best part? You can modify commands, add flags, or change permission logic without redeploying. Configurable workflows handle all the complexity.

Bot code stays clean while automation handles parsing and permissions. Way more maintainable than hand-building parsers.

Check out more at https://latenode.com.

just loop through params and split on anything starting with ‘-’ for flags. for permissions, go numeric but write a helper that maps your role names to numbers - like getPermissionLevel('moderator') returns 3. won’t break existing code and you’ll barely need to refactor.