commands.register
Registers a chat command. When a player types /name and some text, your handler runs with the player and the text after the name.
Signature
Section titled “Signature”commands.register(name: string, handler: (player: PlayerSnapshot, args: string, context: HandlerContext) => unknown, options?: CommandOptions): voidParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
name |
string |
1 to 32 letters, digits or _, without the slash. Case does not matter. |
handler |
(player: PlayerSnapshot, args: string, context: HandlerContext) => unknown |
Runs the command. A player’s commands and events run one after another, in order; context.signal tells you when the player has left (see async handlers). |
options.authorize |
(player: PlayerSnapshot, args: string, context: HandlerContext) => boolean | Promise<boolean> |
Optional. Decides first whether the player may use the command; when the player leaves meanwhile, nothing more happens. |
options.denyMessage |
string |
Optional. Sent to the player when they are denied or the command fails. |
Returns
Section titled “Returns”Nothing. Throws a TypeError for an invalid name and an Error for a name that is already registered.
Example
Section titled “Example”An admin command.
import { chat, commands } from "nativemp";
commands.register("announce", (player, args) => { chat.sendServerMessage(`[Announcement] ${args}`);}, { authorize: (player) => player.id === 1, // Replace with the mode's role lookup. denyMessage: "Admins only.",});Related
Section titled “Related”chat.onPlayerMessage: lines that aren’t commands