Commands

How to create custom commands.

To create commands, use thecommand.create method. There are 2 commonly used syntaxes for command.create, which is directly dependent on if you would like to pass any options to the command, such as a prefix.

Creating a command with no options: command.create(name, invokeFunction)

Creating a command with options: command.create(name, options, invokeFunction)

Valid options

The following are properties that can be declared in the options parameter.

{
    prefix: string, // A prefix for the command. For example, `prefix: "cool"` on a command named test will register /test and /cool:test
    description: string, // A description for the command, which will appear in the Bukkit /help command.
    usage: string, // A usage for the command, which will appear in the Bukkit /help command.
    permission: string | boolean, // If set to true, the command will require OP to execute. If set as a string, the command will require the given permission node to execute.
    permissionMessage: string, // If declared, this is the message that will be sent to players that don't have permission to execute this command.
    aliases: string[] // A set of aliases for this command. For example, a message command could have aliases ["msg", "w", "whisper"]
}

Examples

Let's start off by creating a command without any options, that just greets the command sender.

command.create("hello", (sender, args) => {
    sender.sendMessage("Hello there! Are you enjoying learning how to use Drupi?");
});

Now, let's try making a command that's a little more useful, like a /fly command. We'll create a command here that requires OP to execute.

However, this code is prone to error. Specifically, if the console executes this command, cast.asPlayer(sender) will throw an error, because the server console cannot be turned into a player. So, let's fix that.

Different behavior for the console

The command variable provides 2 additional methods we can use to achieve this, command.isConsoleSender and command.isPlayerSender. I'll opt to show isConsoleSender because it's more simple for this example, however isPlayerSender can also be used.

Now, this command is safe to use, without any errors being thrown if the console executes the command.

Last updated

Was this helpful?