I am currently designing a Discord bot. I am working on a module to easily make commands.
This is currently my commands module.
module.exports = {
add: function(name, help, permissions, callback) {
var command = {};
command['name'] = name;
command['help'] = help;
command['permissions'] = permissions;
command['callback'] = callback;
list.push(command);
},
parse: function(message, data) {
var args = message.split(' ');
for (var i = 0; i < list.length; i++) {
var command = list[i];
if (command.name == args[0]) {
isAuthorized(command.permissions, data, function() {
console.log("RAN");
command.callback(data, args);
});
}
}
},
get: function() {
return list;
}
}
and here is how I would go about using it.
commands.add('ping', 'Send a ping-pong request.', null, function(data, args) {
if (args[1] == "-p") {
discord.sendMessage({to: data.channelID, message: "pong"});
} else {
discord.sendMessage({to: data.channelID, message: "ding"});
}
});
It all good, however I am worry about the design. I am not liking it too much as it can be buggy and annoying. For instance, the above would simply parse a command by a whitespace. Like so:
!ping -d
It will call the ping function and check the first element of args to see if it contain a "-d" flag. I want this to be as similar to Linux terminal as I can get. The above is simple, but what about a command that takes more than one flag? For example:
!ping -d -help
Which will obviously print out "ding" and if implemented it should also print something that would describe the command and it arguments.
But what if I write something like
!ping -help -d -f -m
How would that work out? I rather not have a bunch of conditional statement.
Permissions
Another concern I thought about is permission levels. I have each role setup in a database.
+----+-----------+
| id | name |
+----+-----------+
| 1 | guest |
| 2 | member |
| 3 | mod |
| 4 | developer |
| 5 | admin |
| 6 | master |
| 8 | owner |
+----+-----------+
These roles were created using this command.
roles.createRole = function(name) {
database.insert('roles', ['name'], ['"' + name + '"']);
}
commands.add('createrole', 'Create a new role.', ['master'], function(data, args) {
var role = args[1];
if (role) {
roles.createRole(role);
}
});
And if they have the permission called "master" they are able to create a role.
This is how I am storing the users information.
-------------------+
| id | username | discord_id | permissions
|
+----+---------------+--------------------+-------------------------------------------------------+
| 1 | kurieita | 000000000 | {"test":true,"master":true,"admin":true,"guest":true} |
Obviously I have the role and can execute the command. But what if I set someone to "admin", I also want them to have moderator powers. I would have to give them moderator powers too.
Ideas
Here are my ideas I came up with (while I was writing this)
-
Add "value" field for database.roles and check if user permission level is greater than the required permission field.
-
I thought about doing a more OOP approach. Something along these lines.
var command = new Command(name, description); command.setCallback(function() {}); command.setFlag(name, function() {});I hadn't really thought much about that or how it would work tho.
-
Another idea I had which is similar to the way I am doing it now is this
commands.add(name, description, permissions, function(data, args, flags) {}
where name and description are string, permission is a index array. Data is an array based wit Discord information (userID, channelID, etc), args is an array with values. For example: !message text "text" would be an arg and flags would contain anything prepended with "-". For instance !ping -d, "d" would be in the flags array.
Questions
- How does Linux parse it commands and flags?
- How does Linux take care of permission levels for each command?
- How would I go about implementing a Linux like parser for my discord bot commands module?
- What would the best way to deal with permission levels be?
Any help is appreciated. Thank you in advanced.
via Kurieita
No comments:
Post a Comment