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?