> For the complete documentation index, see [llms.txt](https://openjs.wiki.gd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openjs.wiki.gd/openjs-components/in-build-classes/custom-commands.md).

# Custom Commands

### Creating Custom Commands

Allows you to register and handle custom commands in scripts

***

#### Method

```js
addCommand(commandName: String, commandHandler: Object, permission?: String)
```

**Parameters:**

* **`commandName`** (`String`)\
  The name of the command (without `/`)
* **`commandHandler`** (`Object`)\
  Contains methods like `onCommand` and optionally `onTabComplete`
* **`permission`** *(optional)* (`String`)\
  The permission string required to execute or tab-complete this command.\
  If there is no permission given, the command is available to all players (and ops by default)

***

#### `commandHandler` Methods

* **`onCommand(sender, args)`** *(required)*\
  Called when the command is executed.
  * `sender`: The player or console who executed the command.
  * `args`: A Java array of arguments.
* **`onTabComplete(sender, args)`** *(optional)*\
  Called to provide tab-completion suggestions.\
  Must return a **Java List**.

***

{% code title="Example Without Permissions" %}

```js
addCommand("hello", {
    onCommand: function(sender, args) {
        args = toArray(args); // Convert Java array to JS array
        sender.sendMessage("Hello, " + sender.getName() + "!");
    },
    onTabComplete: function(sender, args) {
        args = toArray(args);
        if (args.length === 1) {
            return toJavaList(["world", "everyone"]);
        }
        return toJavaList([]);
    }
});
```

{% endcode %}

***

{% code title="Example With Permissions" %}

```js
addCommand("adminhello", {
    onCommand: function(sender, args) {
        sender.sendMessage("Hello from the admin command!");
    },
    onTabComplete: function(sender, args) {
        return toJavaList(["secret", "adminzone"]);
    }
}, "openjs.command.adminhello"); // <-- Optional permission
```

{% endcode %}

> Players without `openjs.command.adminhello` will **not see the command** in suggestions or tab-complete.\
> Ops **do have** all permission by default, unless restricted by permission plugins.

***

{% hint style="info" %}

#### Notes

* You can manage permissions via `/lp editor` in [**LuckPerms**](https://luckperms.net/) or other permission systems
* Permissions must be valid strings (e.g., `pluginname.command.commandname`)
* If `onTabComplete` is missing, tab completion is disabled for this command
  {% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://openjs.wiki.gd/openjs-components/in-build-classes/custom-commands.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
