Commands
addCommand: chat commands with parsed arguments, ACE permissions, chat suggestions and a row in ai-core's admin panel.
import { addCommand } from "@aicore-fivem/lib/server";addCommand
Registers a chat command (and console command). Arguments are parsed and checked before your handler runs, the permission is checked with ACE, chat suggestions go only to players allowed to use the command, and ai-core's admin panel lists it in its Commands tab. Use it for every server command instead of a bare RegisterCommand.
Side: Server · Waits: no
addCommand<P extends readonly CommandParam[]>(
name: string | string[],
options: CommandOptions<P>,
handler: (source: number, args: CommandArgs<P>, raw: string) => unknown,
): voidaiLib.addCommand(name, options, handler) -- handler(source, args, raw)Parameters
Prop
Type
CommandOptions
Prop
Type
CommandParam
Prop
Type
What the player sees on wrong input
Before the handler runs, the lib answers with an ai-core notification (or a console line for source 0) using ai-core's texts:
| Case | Text (English) |
|---|---|
| No permission | You are not allowed to use this command. |
| A required argument is missing | <name> is missing. |
number / playerId is not a number | <name> must be a number. |
playerId is not an online player | No player is online with id <value>. |
| After any argument error | Usage: /name <required> [optional] |
Returns
void. Throws in TypeScript when name is an empty list.
Example
import { addCommand, medical, notify, createLocale } from "@aicore-fivem/lib/server";
const Locale = createLocale();
addCommand(
["heal", "h"],
{
help: Locale("CMD_HEAL"),
params: [{ name: "id", type: "playerId", help: Locale("CMD_P_ID"), optional: true }],
restricted: true,
},
(source, args) => {
const target = args.id ?? source; // args.id is number | undefined
if (target === 0) return; // the console must name a player
if (medical.heal(target) && source > 0) notify(source, Locale("HEALED"), "success");
},
);
addCommand(
"announce",
{
help: "Sends a message to everyone",
params: [{ name: "text", type: "longString" }],
restricted: ["group.admin", "ai.admin"],
},
(_source, { text }) => {
for (const player of getPlayers()) notify(Number(player), text, "info", 8000);
},
);Never check permissions by hand inside the handler: restricted does it, and chat suggestions follow it. For a list of principals, ai-core grants command.<name> to each of them, which needs add_ace resource.ai-core command.add_ace allow and add_ace resource.ai-core command.remove_ace allow in permission.cfg. Job and gang grades are principals (ai.job.police), so suggestions update when a player's job or gang changes.
In TypeScript, args is typed from params: args.id is number, or number | undefined when optional. A param list shared by several commands must be declared as const, or the names and types widen and args loses them.