Building a command handler system for Discord bot with proper flag parsing

Help with Discord Bot Command System Structure

I’m building a Discord bot and need advice on creating a better command handler. My current setup works but has some issues I want to fix.

Current Command Handler

module.exports = {
  register: function(cmd, description, perms, handler) {
    var newCmd = {
      command: cmd,
      desc: description,
      requiredPerms: perms,
      execute: handler
    };
    commandList.push(newCmd);
  },

  process: function(msg, context) {
    var tokens = msg.split(' ');
    
    commandList.forEach(function(cmd) {
      if (cmd.command === tokens[0]) {
        checkPermissions(cmd.requiredPerms, context, function() {
          cmd.execute(context, tokens);
        });
      }
    });
  },

  list: function() {
    return commandList;
  }
}

Example Usage

commands.register('echo', 'Responds with echo message', null, function(context, tokens) {
  if (tokens[1] === "-v") {
    bot.reply({channel: context.channelID, text: "verbose echo"});
  } else {
    bot.reply({channel: context.channelID, text: "simple echo"});
  }
});

Problems I’m Facing

My current parser just splits on spaces which is too basic. Commands like !echo -v -help or !echo -help -v -quiet -force become hard to handle without tons of if statements.

For permissions, I have this role structure in my database but hierarchical permissions are tricky:

Roles: guest(1), user(2), moderator(3), developer(4), admin(5), owner(6)

Users are stored with JSON permission objects, but if someone is admin level, they should automatically get moderator privileges too.

What I Want to Achieve

I want command parsing similar to Linux terminal commands where flags can be in any order and combined. Also need a clean permission system where higher roles inherit lower role permissions automatically.

Anyone have suggestions for better command parsing patterns or permission hierarchy designs?

Every Discord bot dev runs into this parsing nightmare eventually. Skip building custom parsers - just automate the whole command flow instead.

I built workflows that grab Discord messages and parse them automatically. They split flags from arguments, handle stuff like -vhf, and check permissions before running commands.

Permissions are dead simple - just a lookup table mapping user roles to numbers. When commands come in, it checks if the user’s level is high enough. No messy inheritance crap.

Adding new commands is the best part. I throw command names, descriptions, permissions, and templates into a spreadsheet. The automation reads it and registers everything.

Took me 2 hours vs weeks building a parser from scratch. I can change command behavior without touching any code.

This is exactly what Latenode’s built for. Connect Discord webhooks straight to parsing logic and database lookups - no custom handlers needed.

Been wrestling with the same command parsing nightmare for months before I cracked it. Here’s what works: split it into two phases instead of doing everything at once. First pass grabs and normalizes all flags no matter the format. Second pass validates against your command schema. For permissions, I store each user’s highest role number, then commands just say what minimum they need. Processing becomes a simple greater-than check - no special inheritance logic needed. Watch out for quoted args like !echo "hello world" -v though. Your parser has to handle quotes before extracting flags or you’ll hit weird edge cases. Also throw in command aliases early - users always want shortcuts for stuff they use constantly.

Had the same issues building my Discord bot last year. Switching from manual string splitting to structured flag parsing was a game changer. For commands, I built a simple parser that turns tokens into objects. So !echo -v -help message here becomes {flags: ['v', 'help'], args: ['message', 'here']}. Then you just do if (parsed.flags.includes('v')) instead of messy conditionals. Permissions got way easier when I stopped matching exact roles and started using minimum required levels. Set the min level when registering commands, then just compare user level >= required level. Handles inheritance automatically. Watch out for edge cases like empty flags or broken commands - add validation early or they’ll bite you later.

Yeah, splitting on spaces won’t cut it. I built a dedicated argument parser that separates flags from values. Here’s what works: preprocess everything first - scan once for flags like -v and --help, then grab whatever’s left as regular arguments. For combined flags like -vhf, just split them up during preprocessing. The permission stuff gets way easier with numbers. When you register commands, just store the minimum permission level as a number. Then checking permissions is one line: userLevel >= requiredLevel. Admin level 5 beats moderator level 3, so inheritance works automatically. I threw in a helper function that finds the user’s highest role and returns the number. Way cleaner than dealing with nested permission objects.

Use regex for flag parsing instead of splitting. Something like /-(\w+)/g grabs all flags first, then you process what’s left as arguments. For permissions, go with bitwise operators - give each role a power of 2 (guest=1, user=2, mod=4, etc) and check with AND operations. Way faster than level comparisons and handles multiple roles per user without extra work.