Skip to content

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.

commands.register(name: string, handler: (player: PlayerSnapshot, args: string, context: HandlerContext) => unknown, options?: CommandOptions): void
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.

Nothing. Throws a TypeError for an invalid name and an Error for a name that is already registered.

An admin command.

gamemodes/admin.ts
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.",
});