Defining Commands¶
PySpigot allows you to define, register, and unregister commands from within scripts. These commands function in the same way as any plugin-defined command would in-game.
For instructions on importing the command manager into your script, visit the General Information page.
Info
This is not a comprehensive guide to commands in Bukkit. For a more complete guide to commands, see Spigot's tutorial on commands here.
Command Manager Usage¶
The command manager makes several functions available to you to register and unregister commands, including:
registerCommand(command_function, name)
: The most basic way to register a command.registerCommand(command_function, tab_function, name)
registerCommand(command_function, name, description, usage)
registerCommand(command_function, tab_function, name, description, usage)
registerCommand(command_function, name, description, usage, aliases)
registerCommand(command_function, tab_function, name, description, usage, aliases)
registerCommand(command_function, tab_function, name, description, usage, aliases, permission, permission_message)
: The most comprehensive way to register a command.unregisterCommand(name)
: Allows you to unregister a command from your script using its name.unregisterCommand(command)
: Allows you to unregister a command from your script. Accepts theScriptCommand
object returned from any of theregisterCommand
functions.
See the below section for a description of the arguments for the registerCommand
functions.
All the above registerCommand
functions return a ScriptCommand
object, which can be used to unregister the command later, if desired.
Tip
You do not need to unregister your commands when your script is stopped/unloaded. PySpigot will handle this for you.
Function Arguments¶
The arguments of the above functions are described below:
command_function
: The function that should be called when the command is run in-game or in the server console. Command functions should always returnTrue
orFalse
. ReturnTrue
if the command was used correctly. ReturnFalse
if it was not. This function should also accept three arguments:sender
, which represents the sender that executed the command (either a player or console),label
, which represents the label that was typed (either the command name or one of its aliases), andargs
, a list of arguments passed along with the command.- For more information, see the Spigot tutorial on commands.
tab_function
: A function that should be called to generate a list of tab-completable items when typing the command.name
: The name of the command being registereddescription
: A description for the commandusage
: A message printed to the person executing the command if the usage is incorrect. This is controlled programatically by returning eitherTrue
orFalse
from the command function.aliases
: A list of aliases for the command.permission
: A permission node required to execute the command.permission_message
: A message to display if the command sender does not have permission to execute the command.
Code Example¶
Let's look at the following code that defines and registers a command:
-
Here, we import PySpigot as
ps
to utilize the command manager (command
). -
Here, we define a function called
kick_command
that takes three arguments,sender
,label
, andargs
.sender
is who/what executed the command,label
is exactly the command that was typed (if the command sender typed an alias, then this argument would be the alias that the command sender used).args
is a list ofstr
containing each argument thatsender
typed after the base command. -
Here, we return a boolean value from the function. Command functions must always return either
True
orFalse
. Whether or not you should returnTrue
orFalse
is explained above. -
Here, we define a function called
tab_kick_command
that takes the same parameters askick_command
. This function serves as the tab completer for the command. On line 9, we return a list ofstr
from this function, which serves as the options that can be tab completed. Tab complete functions must return a list ofstr
. -
Here, we return a list from the function, representing the list of tab-completable arguments for the command. Tab completion functions must always return a list of
str
if there are tab-completable items or, if there are no tab-completable items,None
or an empty list. -
Here, we register the command with
registerCommand(command_function, tab_function, name)
. We also assign the returned value ofregisterCommand
toregistered_command
. This is aScriptCommand
object, which represents the command that was registered. This can be used to unregister the command if you would like to do so later.
All commands must be registered with PySpigot's command manager to work. Commands can be registered in multiple ways. In the code above, the registerCommand(function, name)
function is used, which takes two arguments:
- The first argument accepts the function that should be called when the command is executed.
- The second argument is the name of the command, a
str
.
Unregistering a Command¶
Continuing the above code example:
- Here, we unregister the command by passing the
ScriptCommand
object we assigned earlier when registering the command. You can also unregister a command by passing its name if you don't want to store theScriptCommand
object.
Tip
For more functional examples of commands, check out some of the example scripts.
Summary¶
- Commands are defined as functions in your script. Command functions must take three parameters: a sender, label, and args (the names of these parameters can be whatever you like). The function is called when the command is executed.
- Use a tab function to return tab-completable items when typing the command.
- All commands must be registered with PySpigot's command manager using
command.registerCommand(function, name)
.