Skip to content

Admin commands

Operator commands on the server console, and admins kept by name. For commands typed by players, see the chat example.

admin.ts
import { Server, Color, Weapon } from "@sagl/server";

export function adminCommands(server: Server) {
  const admins = new Set(["Thomas"]);

  const find = (id: string) => {
    const player = server.player(Number(id));
    if (!player) throw new Error(`no player ${id}`);
    return player;
  };

  server.console
    .command("admin", "admin <id>", ([id]) => {
      const player = find(id);
      admins.add(player.name);
      player.send("You're an admin now.", Color.Yellow);
    })
    .command("money", "money <id> <amount>", ([id, amount]) => {
      find(id).giveMoney(Number(amount));
    })
    .command("armour", "armour <id>", ([id]) => {
      find(id).armour = 100;
    })
    .command("arm", "arm <id>", ([id]) => {
      const player = find(id);
      player.giveWeapon(Weapon.M4, 500);
      player.giveWeapon(Weapon.Shotgun, 50);
    })
    .command("bring", "bring <id> <toId>", ([id, toId]) => {
      const to = find(toId).position;
      find(id).setPosition({ x: to.x + 1, y: to.y, z: to.z });
    })
    .command("weather", "weather <0-22>", ([w]) => {
      server.weather = Number(w);
    })
    .command("announce", "announce <text>", (_args, text) => {
      server.broadcast(`[Server] ${text}`, Color.Orange);
    });

  return { isAdmin: (name: string) => admins.has(name) };
}
index.ts
const { isAdmin } = adminCommands(server);
server.on("playerExplosion", (player, e) => {
  if (e.weapon === Weapon.RocketLauncher && !isAdmin(player.name)) return false;
});