Commands
Register server-side commands with typed argument parsing, chat suggestions, console restrictions and Ace-based group restrictions. MSK.RegisterCommand wraps the native RegisterCommand and adds these features.
MSK.RegisterCommand
Registers a server command. The handler runs as callback(source, args, raw). Passing a table of command names registers the same handler for each of them.
If restricted is set in properties, the command is registered as restricted and the matching command.<commandName> Ace is granted to the given group(s) automatically (via MSK.AddAce) unless already allowed.
Every name gets its own copy of properties. When you register aliases like {'ban', 'b'}, all of them keep the restricted setting. Before v4.1.0 only the first name was restricted and every alias after it could be used by anyone.
Parameters
commandName - string or table - The command name, or a list of names sharing one handler.
callback - function - Handler executed as callback(source, args, raw) (or callback(Player, args, raw) when returnPlayer is set).
properties - table - Additional command properties (see below). Optional.
Returns
command - table - The internal command entry that was registered.
Description: properties
- allowConsole -
boolean- Allow execution from the server console (source == 0). Optional, default:true - showSuggestion -
boolean- Show / hide the chat suggestion. Optional, default:true - restricted -
booleanorstringortable- Restrict the command to one or more groups via Ace. When a string or table is given, the correspondingcommand.<commandName>Ace is added for those groups. - returnPlayer -
boolean- Pass the resolved player object as the first handler argument instead ofsource(not available in STANDALONE). - help -
string- Chat suggestion description - params -
table- Typed argument definitions (see below)
Description: params
- name -
string- Name of the argument (becomes the key inargs) - type -
string- One ofnumber,string,longString,playerId,player,any - help -
string- Chat suggestion of the argument - optional -
boolean- Mark the argument optional (must be the last one). Optional, default:false
For type = 'player', the argument is resolved to the full player object. For type = 'playerId', only the numeric server id is returned. In both cases me resolves to the executing player.
type = 'string' only takes a single word. type = 'longString' takes the rest of the line, so /ban 1 2D Using an aimbot gives you Using an aimbot as one value. New in v4.1.0. It swallows every argument after it, so it has to be the last parameter.
MSK.RegisterCommand(commandName, callback, properties)
-- Example 1
MSK.RegisterCommand('testCommand', function(source, args, raw)
local targetId, time, reason = args.playerId, args.time, args.reason
if not reason then
reason = 'Unknown reason'
end
print(('Player %s was banned by %s for %s until %s'):format(targetId, source, reason, time))
end, {
allowConsole = true,
showSuggestion = true,
restricted = {'superadmin', 'admin'},
help = 'This is a Test Command',
params = {
{name = 'playerId', type = 'playerId', help = 'Target players server id'},
{name = 'time', type = 'string', help = 'Ban Time'},
{name = 'reason', type = 'longString', help = 'Ban Reason', optional = true},
}
})
-- Example 2 -> Framework based (not available in STANDALONE)
MSK.RegisterCommand('testCommand', function(Player, args, raw)
local Target = args.player
-- ESX
print(Player.source) -- PlayerId of the Executer
print(Target.source) -- PlayerId of the Arguments playerId
-- QBCore
print(Player.PlayerData.source)
print(Target.PlayerData.source)
end, {
allowConsole = true,
showSuggestion = true,
returnPlayer = true,
restricted = {'superadmin', 'admin'},
help = 'This is a Test Command',
params = {
{name = 'player', type = 'player', help = 'random argument'},
}
})
-- As an Export:
exports.msk_core:RegisterCommand(commandName, callback, properties)