RegisterCommand
Makes a /command with its handler, its help and its rules in one call.
/name rest of the line runs fn(pid, args): args is the rest of the line, trimmed, "" when there is none. The
handler runs before OnPlayerCommandText is asked and before the server’s built-in commands, so a registered name
wins over both - over the built-in /help too. What is not registered goes the old way, so a mode may use both. A handler
that raises an error is logged and counted like any callback; the player is told nothing.
options is a table, and every key of it is optional:
| key | what it is |
|---|---|
help |
one line on what the command does, 160 characters at most |
params |
the usage that follows the name - "<player> [reason]" - as a text, or a list of words joined with spaces; 96 characters at most |
admin |
true: anyone who is not an admin (SetPlayerAdmin) is answered “Admins only.” and the handler does not run; only admins are offered the command |
secret |
true: the command’s typing is a secret, as with SetSecretCommand - a chat masks it and keeps it out of its history, and args arrives exactly as typed. The handler must not log or store it |
A chat that can suggest offers the command to the players who may use it, with its help and its usage, while they
type; the game’s own chat has no such list. /help lists the registered commands the player may use after the
server’s own.
The name is 1 to 32 letters, digits, _ and -; a slash in front and capitals are forgiven ("/Pay" is pay). fight and peace are the server’s own and cannot be
registered. At most 256 commands; registering a name again replaces the command. A reload of the mode empties the
registry: register the commands in OnGameModeInit.
The answer is true, or false and the reason: "a command name is 1-32 of a-z, 0-9, _ and -", "the handler is not a function", "/fight is the server's own", "the help is over 160 characters", "the usage is over 96 characters" or
"too many commands (256 at most)".
Syntax
Section titled “Syntax”RegisterCommand(name, fn [, options])| Parameter | Type | |
|---|---|---|
name |
string | the command without the slash - 1 to 32 of a-z 0-9 _ - |
fn |
function | function(pid, args) - runs when a player types the command |
options |
table | { help, params, admin, secret } - every key optional (optional) |
Returns
Section titled “Returns”true, or false, reason - the reason as a person reads it
Example
Section titled “Example”function OnGameModeInit() RegisterCommand("pay", function(pid, args) local target, amount = sscanf(args, "ud") if target == false then return SendClientMessage(pid, COLOR_RED, "usage: /pay <player> <amount>") end pay(pid, target, amount) end, { help = "Give a player some groschen", params = "<player> <amount>" })
RegisterCommand("closegates", function(pid) closeGates() end, { help = "Close every gate of the town", admin = true })
RegisterCommand("login", function(pid, args) login(pid, args) end, { help = "Log in to your character", params = "<password>", secret = true })endSee also
Section titled “See also”UnregisterCommand · AddChatSuggestion · OnPlayerCommandText · SetSecretCommand · sscanf · the Chat and commands group of the index
