smart-command
is a Node.js command processor that makes your job easy when controlling parameter obligatoriness and responses.
This package was made to be used with Rage:MP, a multi-player modification for Grand Theft Auto V but is not restricted to it.
Take this example as hypothetical.
var playerCommands = Scope.get('player');
playerCommands.addCommnand('ban', {
target: 'Word',
time: 'Number',
reason: { type: 'Phrase', required: false }
}, function(source, parameters) {
var source_player = source.player;
var target_player = mp.players.get(parameters.target);
source.respond(`Admin ${source_player.name} has banned ${target_player.name}!`);
if(parameters.reason) {
source.respond(`Reason ${parameters.reason}`);
}
});
// called when a player enters a command
function onPlayerCommand(player, text) {
playerCommands.parse({
player: player,
respond: text => { player.send(text) }
}, text);
}
/ban WeirdNewbie 10 "Called another player a \"Dum-Dum\""
Admin Candy has banned WeirdNewbie
Reason Called another player a "Dum-Dum"
Parameter types are configured when a command is being defined (Scope.addCommand
). A parameter type must be an object with the keys being the parameter names - later to be pushed to the handler as an argument - and values as:
- string: when a parameter is mandatory, the value must be a string containing the parameter parser name.
-
object of
{ type: String, required: Boolean }
: When the obligatoriness of the parameter must be specified.
Note: A nonrequired parameter must always follow a required one and never the opposite.
Parses a number and trims the result. Will fail if any non-numeric character is input or it contains nothing.
Parses a word, or any piece of text which does not contain whitespace. Will fail if the input text is blank.
Parses a double quote enclosed text and watches out for escaped double quotes. As shown in the example above.
License: MIT