Flux.Command class
Part of Flow
The command system: keeps the registered chat and console commands, completes what players
type and runs the commands for them. Commands are Command objects. Every file in the
commands folder of a plugin or schema gets a CMD table to fill in (name, description,
syntax, argument types, permission, aliases, subcommands and the on_run callback) and is
registered automatically under an ID taken from its file name; see Command:on_run and
Command:subcommand for examples. Players run commands by typing them in the chat with a
command prefix or through the flc console command, which also works from the server
console.
Everything that is typed has to match exactly, ignoring the case: the command ID or alias,
the subcommand word, a word from a list of allowed words, and a player's full name or
SteamID. Flux.Command:complete turns what has been typed so far into the full words: the
chatbox and the console offer its suggestions, and Flux.Command:add_completer lets
plugins suggest the values of their own argument types.
On the server Flux.Command:interpret does the work: it splits the text into arguments,
finds the command by its ID or alias and the subcommand by its word, checks the permission
of the player, the amount of arguments and the words from lists, turns the player argument
into a list of targets (player names or the selectors described under
Flux.Command:str_to_player), checks their immunity, logs the command and calls its
on_run. Plugins can step in through the PlayerCanRunCommand, CommandCheckImmunity,
PlayerRunCommand and TargetFromString hooks. On the client Flux.Command:send asks
the server to run a command for the local player.
Summary
Functions
Flux.Command:complete.Methods
Flux.Command:complete.Flux.Command:complete in place of the token it was made for, in double quotes if it has whitespace in it, and adds a space after it, so that the suggestions for the next token can follow.Flux.Command:tokenize does.player.find_exact), or one of the following target selectors: '@group' (everyone in a user group), '(name)' (everyone with this text in their name), '[name]' (the player with exactly this name, in this case), '^' (yourself), '*' (everyone) and '!radius' (everyone within this distance from you).Functions
Internal
This function is internal. It is not part of the public API and may change without notice.
Completes the flc and flCmd console commands with the suggestions of
Flux.Command:complete.
Parameters
- cmd String name of the console command
- args_text String what has been typed after it
Returns
- List<String> the console lines to offer
Flux.Command.con_command(actor, _cmd, _args, args_text)
shared internal packages/flow/lib/sh_command.lua:1139Internal
This function is internal. It is not part of the public API and may change without notice.
Powers the flc and flCmd console commands. Interprets the command on the server and sends it to the server on the client.
Parameters
- actor Player the player who has run the console command
- _cmd String name of the console command
- _args List<String> arguments of the console command
- args_text String arguments as a single string, the Flux command to run
Methods
Registers the completer of an argument type: the function that lists the values an
argument of that type can have, for the suggestions of Flux.Command:complete. Commands
use the type by naming it in their arg_types. The completer returns every value that
fits, as it has to be typed; the suggestions are narrowed down to what the player has
typed and sorted elsewhere, so the completer may ignore the partial text. Register
completers in a shared file, so that the client has them. Flux registers 'player' itself.
-- plugins/factions/plugin/sh_plugin.lua
Flux.Command:add_completer('faction', function(actor, partial, args, cmd)
local ids = {}
for id in pairs(Factions.all()) do
ids[#ids + 1] = id
end
return ids
end)
Parameters
- type_id String ID of the argument type
- callback Function(actor, partial, args, cmd) that returns a List<String> of values; it gets the player who is typing (nil for the server console), the text typed for the argument so far, a List<String> of the arguments before it and the Command or subcommand the argument belongs to
Puts one of the suggestions of Flux.Command:complete in place of the token it was made
for, in double quotes if it has whitespace in it, and adds a space after it, so that the
suggestions for the next token can follow. Pass the same text that was completed; to cycle
through the suggestions, apply each one to that text in turn.
Parameters
- text String the text that was passed to complete
- result Map the result of complete
- index Number position of the suggestion in result.candidates
Returns
- String the new text, or the same text if there is no such suggestion
Works out what can be typed next in a command, for the suggestions of the chatbox and of
the console. The text is what has been typed after the command prefix, up to the caret.
The token that is completed is the last one, or a new one if the text ends with
whitespace; a double quote that is not closed makes the rest of the text that token. The
suggestions are the commands the player may run, the subcommands of the command that was
typed, or the values of its next argument: the words of a list from arg_types or what
the completer of the argument's type returns. Commands and subcommands the player may not
run are left out. A command or subcommand is suggested only when its ID or one of its
aliases starts with the typed text, ignoring the case: under its ID if that is the one,
else under the alias, which aliases marks. The name that is exactly what was typed comes
first, the others are sorted by name. The values of an argument are kept when they contain
the typed text, ignoring the case, those that start with it first, so that a player can be
found by any part of their name. Apply a suggestion with Flux.Command:apply_completion.
local result = Flux.Command:complete('whitelist ad', PLAYER)
-- result.kind == 'subcommand', result.candidates == { 'add' }, result.start == 11
local text = Flux.Command:apply_completion('whitelist ad', result, 1)
-- text == 'whitelist add '
Parameters
- text String the typed text without the command prefix
- actor Player optional, defaults to
nilthe player who is typing, for the permission checks; nil for the server console
Returns
- Map the result: kind (String 'command', 'subcommand' or 'argument'; nil when nothing can be suggested because the command is unknown or not allowed, or a word that is not one of its subcommands has been typed), partial (String the typed text of the token, without its quote), start (Number position of the first character of the token, its opening quote included, counted in characters), start_byte (Number the same position in bytes), token_index (Number position of the token among all of them, 1 for the command), command (Command the command or subcommand that has been typed, nil while the command itself is typed), arg_index (Number position of the argument that is typed, 1 for the first one after the command or subcommand word, nil while the command is typed), arg_type (String/List<String> its type from
arg_types, nil if it has none), candidates (List<String> the suggestions, as values to be quoted by apply_completion), commands (Map<Number - Command> the command or subcommand of the suggestion at the same position, for the suggestions that are one) and aliases (Map<Number
- Boolean> true at the position of a suggestion that is an alias of its command rather than its ID)
See also
Registers a command along with its aliases, fills in the defaults for missing fields of
the command and its subcommands, and runs the 'OnCommandCreated' hook. Commands defined in
the 'commands' folder of a plugin are registered automatically, see Command#on_run for an
example. When player_arg is not set, the position of the first 'player' entry of
arg_types is used.
Parameters
- id String unique command ID, also the main name it is called by; it is stored converted with
string.to_id - data Command/Map command object or a table with the same fields: name, description, syntax, permission (ID of the role it is allowed to), permission_id, arguments (minimum amount), arg_types, immunity, player_arg, alias or aliases, no_console and the on_run callback; a table is turned into a Command
See also
Splits a command string into arguments, the way Flux.Command:tokenize does.
-- { 'ban', 'John Doe', '60', 'minging' }
local args = Flux.Command:extract_arguments('ban "John Doe" 60 minging')
Parameters
- text String
Returns
- List<String> arguments
- String text of the arguments after the first one, as it was typed
Finds all of the commands that have the search string in their ID or aliases. On the client only the commands the local player has access to are returned.
Parameters
- id String search string
Returns
- List<Command> matching commands
Finds a command by its exact ID or alias, ignoring the case.
Parameters
- id String command ID or alias
Returns
- Command the command, or nil if there is no such command
Returns the completer of an argument type.
Parameters
- type_id String ID of the argument type
Returns
- Function the completer, or nil if the type has none
See also
Parses a command string and runs the command on behalf of the specified player.
Finds the command by its ID or alias and then its subcommand by the next word, both
matched exactly in any case. Checks that the player has access to the command or
subcommand, that enough arguments were given and that the arguments with a list of words
in arg_types are one of them, resolves the targets and their immunity, logs the command
and runs it. A trailing 'text' argument is passed on exactly as it was typed, see
Command:get_text_index. The player is notified of any failure (it is printed to the
console for the server console). Serverside only.
Parameters
- actor Player the player who runs the command, an invalid entity for server console
- text String command ID followed by its subcommand word, if any, and its arguments
- from_console Boolean optional, defaults to
nilwhether it was run as a console command, passed to the 'PlayerCanRunCommand' hook
Returns a value the way it has to be typed as one argument: in double quotes if it is empty or has whitespace in it.
Parameters
- value String
Returns
- String
Flux.Command:run(actor, cmd_table, arguments, raw_args)
server packages/flow/lib/sh_command.lua:1100Calls the on_run callback of a command in protected mode. Assumes that the command is valid and that all of the permission checks have already been done. Serverside only.
Parameters
- actor Player the player who runs the command, an invalid entity for server console
- cmd_table Command the command or subcommand
- arguments List arguments to pass to on_run after the player
- raw_args String optional, defaults to
nilraw text of the arguments, available to the callback as self.raw_args
Asks the server to run a command on behalf of the local player. Clientside only.
Flux.Command:send('getup')
Parameters
- command String command ID followed by its arguments
Finds the players targeted by a command argument: the player with exactly this
SteamID, character name or Steam name (see player.find_exact), or one of the following
target selectors: '@group' (everyone in a user group), '(name)' (everyone with this text
in their name), '[name]' (the player with exactly this name, in this case), '^'
(yourself), '*' (everyone) and '!radius' (everyone within this distance from you).
Plugins can add more by returning a parser function from the 'TargetFromString' hook. A
name that several players have targets nobody. Serverside only.
Parameters
- actor Player the player who is running the command
- str String player name, SteamID or target selector
Returns
- List<Player> the targets, or false if nobody was found; String the selector character if one was used, or 'ambiguous' if several players have this name
Splits a command string into tokens. Tokens are separated by whitespace; text in double
quotes that start a token is one token, and the quotes are not part of its value. A double
quote that is never closed is an ordinary character, unless open_end is set: the rest of
the text is then the last token, as it is while a quoted argument is being typed.
-- 'ban', 'John Doe' (quoted, closed) and '60'
local tokens = Flux.Command:tokenize('ban "John Doe" 60')
Parameters
- text String
- open_end Boolean optional, defaults to
falseread an unclosed quote as a token that is being typed
Returns
- List<Map> tokens: value (String), start and finish (Number positions of the first and the last byte of the token in the text, quotes included), quoted (Boolean true if it starts with a quote) and closed (Boolean whether the quote is closed, false only for an unclosed quote read with open_end)