Registry
The registry keeps track of all the commands and types that Cmdr knows about.
Types
HookType
type
HookType =
"BeforeRun"
|
"AfterRun"
ArgumentDefinition
interface
ArgumentDefinition {
Type:
string
|
TypeDefinition
--
The argument type (case sensitive), or an inline TypeDefinition object.
Name:
string
--
The argument name, this is displayed to the user as they type.
Description:
string?
--
A description of what the argument is, this is also displayed to the user.
Optional:
boolean?
--
If this is set to true
, then the user can run the command without filling out the value. In which case, the argument will be sent to implementations as nil
.
Default:
any?
--
If present, the argument will be automatically made optional, so if the user doesn't supply a value, implementations will receive whatever the value of Default
is.
}
The table
definition, usually contained in a CommandDefinition, which 'defines' the argument.
CommandDefinition
interface
CommandDefinition {
Name:
string
--
The name of the command
Aliases:
{
string
}
?
--
Aliases which aren't part of auto-complete, but if matched will run this command just the same. For example, m
might be an alias of announce
.
Description:
string?
--
A description of the command, displayed to the user in the help
command and auto-complete menu.
Group:
string?
--
This property is intended to be used in hooks, so that you can categorise commands and decide if you want a specific user to be able to run them or not. But the help
command will use them as headings.
Args:
{
ArgumentDefinition
|
(
CommandContext
)
→
(
ArgumentDefinition
)
}
--
Arguments for the command; this is technically optional but if you have no args, set it to {}
or you may experience some interface weirdness.
Data:
(
...
)
→
any
--
If your command needs to gather some extra data from the client that's only available on the client, then you can define this function. It should accept the command context and tuple of transformed arguments, and return a single value which will be available in the command with CommandContext:GetData.
ClientRun:
(
...
)
→
string?
--
If you want your command to run on the client, you can add this function to the command definition itself. It works exactly like the function that you would return from the Server module. If this function returns a string, the command will run entirely on the client and won't touch the server (which means server-only hooks won't run). If this function doesn't return anything, it will fall back to executing the Server module on the server.
Run:
(
...
)
→
string?
--
An older version of ClientRun. There are very few scenarios where this is preferred to ClientRun (so, in other words, don't use it!). These days, Run
is only used for some dark magic involving server-sided command objects.
}
TypeDefinition
interface
TypeDefinition {
Prefixes:
string?
--
String containing prefixed union types for this type. This property should omit the inital type, so the string should begin with a prefix character, e.g. Prefixes = "# integer ! boolean"
DisplayName:
string?
--
Overrides the user-facing name of this type in the autocomplete menu. Otherwise, the registered name of the type will be used.
Default:
(
(
Player
)
→
string
)
?
--
Should return the "default" value for this type as a string. For example, the default value of the player
type is the name of the player who ran the command.
Listable:
boolean?
--
If true, this will tell Cmdr that comma-separated lists are allowed for this type. Cmdr will automatically split the list and parse each segment through your Transform
, Validate
, Autocomplete
and Parse
functions individually, so you don't have to change the logic of your type at all. The only limitation is that your Parse
function must return a table, the tables from each individual segment's Parse
functions will be merged into one table at the end of the parsing step. The uniqueness of values is ensured upon merging, so even if the user lists the same value several times, it will only appear once in the final table.
Transform:
(
string
,
)
→
T?
--
Transform is an optional function that is passed two values: the raw text, and the player running the command. Then, whatever values this function returns will be passed to all other functions in the type (Validate
, Autocomplete
and Parse
).
Validate:
(
T
)
→
(
boolean
,
string?
)
--
The Validate
function is passed whatever is returned from the Transform
function (or the raw value if there is no Transform
function). If the value is valid for the type, it should return true
. If the value is invalid, it should return two values: false
and a string containing an error message. If this function is omitted, anything will be considered valid.
ValidateOnce:
(
T
)
→
(
boolean
,
string?
)
--
This function works exactly the same as the normal Validate
function, except it only runs once (after the user presses Enter). This should only be used if the validation process is relatively expensive or needs to yield. For example, the playerId
type uses this because it needs to call GetUserIdFromNameAsync
in order to validate. For the vast majority of types, you should just use Validate
instead.
Autocomplete:
(
T
)
→
(
{
string
}
,
{
IsPartial:
boolean?
}
?
)
?
--
Should only be present for types that are possible to be auto-completed. It should return an array of strings that will be displayed in the auto-complete menu. It can also return a second value, containing a dictionary of options (currently, IsPartial
: if true then pressing Tab to auto-complete won't continue onwards to the next argument.)
Parse:
(
T
)
→
any
--
Parse is the only required function in a type definition. It is the final step before the value is considered finalised. This function should return the actual parsed value that will be sent to implementations.
}
The table
definition, contained in an ArgumentDefinition or registered, which 'defines' the argument.
Properties
Cmdr
This item is read only and cannot be modified. Read OnlyA reference to Cmdr. This may either be the server or client version of Cmdr depending on where the code is running.
Functions
RegisterType
Registry:
RegisterType
(
name:
string
,
--
The name of the type, this must be unique, alphanumeric, and start with a lower-case letter or digit.
typeObject:
TypeDefinition
) →
(
)
Registers a type on the current realm (server/client), this function should be called from within the type definition ModuleScript.
RegisterTypePrefix
Registry:
RegisterTypePrefix
(
name:
string
,
--
The name of the type.
union:
string
--
The string should omit the initial type name, so this string should begin with a prefix character, e.g. # integer ! boolean
) →
(
)
Registers a Prefixed Union Type string on the current realm (server/client), this function should be called from within the type definition ModuleScript.
If there are already type prefixes for the given type name, they will be concatenated. This allows you to add prefixes to default types, like players
.
RegisterTypeAlias
Registry:
RegisterTypeAlias
(
name:
string
,
--
The name of the type, this must be unique, alphanumeric, and start with a lower-case letter or digit.
alias:
string
--
The string should include the initial type name, e.g. string # integer ! boolean
) →
(
)
Allows you to register a name which will be expanded into a longer type which can be used as a command argument type.
For example, if you register the alias stringOrNumber
it could be interpreted as string # number
when used.
RegisterTypesIn
This item only works when running on the server. ServerRegisters all types from within a container on both the server and client.
RegisterHooksIn
This item only works when running on the server. ServerRegistry:
RegisterHooksIn
(
) →
(
)
Registers all hooks from within a container on both the server and client. If you want to add a hook only on the server or client – e.g. for logging – then you should use the Register.RegisterHook method instead.
RegisterCommand
This item only works when running on the server. ServerRegistry:
RegisterCommand
(
filter:
(
CommandDefinition
→
boolean
)
?
--
If present, will be passed a command definition which will then only be registered if the function returns true
.
) →
(
)
Registers a command definition and its server equivalent. Handles replicating the definition to the client.
RegisterCommandsIn
This item only works when running on the server. ServerRegistry:
RegisterCommandsIn
(
filter:
(
(
CommandDefinition
)
→
boolean
)
?
--
If present, will be passed a command definition which will then only be registered if the function returns true
.
) →
(
)
Registers all commands from within a container on both the server and client.
Module scripts which include Server
in their name will not be sent to the client.
RegisterDefaultCommands
This item only works when running on the server. ServerRegisters the default commands on both the server and client.
The optional arrayOrFunc
parameter can be provided with:
- an array of strings — this will limit registration to only commands which have their
Group
property set to this - a function which takes in a CommandDefinition and returns a
boolean
— only iftrue
is returned will the command be registered
GetCommand
Returns the CommandDefinition from the given name, or nil if no command is found. Command aliases are also accepted.
GetCommands
Returns an array of all registers commands, not including aliases.
GetCommandNames
Registry:
GetCommandNames
(
) →
{
string
}
Returns an array of containing the names of all registered commands, not including aliases.
GetTypeNames
Registry:
GetTypeNames
(
) →
{
string
}
Returns an array of containing the names of all registered types, not including aliases.
GetType
Returns the type definition from the given name, or nil if no argument is found.
GetTypeName
Returns a type name taking aliases into account. If there is no alias, the name parameter is simply returned as a pass through.
RegisterHook
Registry:
RegisterHook
(
priority:
number
--
If unspecified, the priority will default to 0
.
) →
(
)
Registers a hook on the current realm (server/client). This should probably be ran on the server or in a hook module script, but can also work on the client.
Hooks run in order of priority from lowest (running first) to highest.
GetStore
Registry:
GetStore
(
name:
string
) →
table
Returns a table saved with the given name. Always returns the same table on subsequent calls. Useful for commands that require persistent state, like bind or ban. This is the same as CommandContext.GetStore.
GetCommandsAsStrings
This was deprecated in v1.8.0
This method was renamed to GetCommandNames in v1.8.0. The old name exists for backwards compatibility and should not be used for new work.
Registry:
GetCommandsAsStrings
(
) →
(
)
AddHook
This was deprecated in v1.1.2
This method was renamed to RegisterHook in v1.1.2. The old name exists for backwards compatibility and should not be used for new work.
Registry:
AddHook
(
priority:
number
--
If unspecified, the priority will default to 0
.
) →
(
)