Moderation
Before this: Player Control, Every Player
A chat command answers whoever types it. A command for running the server
has to check who that is first. This lesson builds an /a channel that only
admins can use and read, and a /reset that only trusted builders can run.
Needs admin to load: it uses Chat Command Event and Fill Array From Players.
Contents
Code
import { escape, sanitize } from "sanitize"
const ROLE = "Admin"
const ADMIN_COLOR = "ff8040"
const MAX_LEN = 200
var players: controller[]
on ChatCommand("a", "Message every admin: /a message") -> (who, arguments) {
if !who.HasRole(ROLE) {
who.ShowChatMessage("Only admins can use /a")
return
}
let name = escape(who.GetDisplayName())
let line = '<color="${ADMIN_COLOR}">[admin] ${name}:</> ${sanitize(arguments, MAX_LEN)}'
players.fillFromPlayers()
for p in players {
if p.HasRole(ROLE) {
p.ShowChatMessage(line)
}
}
}
who.HasRole(ROLE)is true when the player has the server role namedAdmin. Anyone else is told no, and the handler stops.- The line is built once: the sender’s escaped name, then their message,
cleaned with
sanitize, in the colors from Rich Text. - The loop from Every Player sends it to every player who has the role, the sender included.
A player without the role types /a hello. What do they see, and does any
admin see it? An admin types /a with nothing after it. What do the admins
get?
Run
- Check the name of your server’s admin role in its role settings, and set
ROLEto it. - Compile the chip and paste it in-game. Nothing needs wiring.
- Type
/a testing. You see the line in orange with your name. A friend without the role gets “Only admins can use /a”, and nobody sees their message.
Under the Hood
Open the chip. The chain starts at Chat Command Event and goes straight
into Has Role, whose Player State comes from the event and whose role
name is the constant Admin. A NOT of its Has Role output decides
the Branch: one side answers no, the other reads the name, calls
sanitize, then runs Fill Array From Players and the loop. Inside the loop, a second Has Role checks each
player.
Has Role itself is not admin. Any chip can check a role, so a circuit anyone can paste can still treat admins differently. This visitor counter leaves admins out of the count:
@left in door: zone
var visits: int = 0
@right out sign = "Visitors: ${visits.Value}"
on ZoneEntered(zone = door) -> (character) {
if character.HasRole("Admin") {
return
}
visits += 1
}
Who may do what
There are three checks on a player, and any chip can use them.
HasRole(name): the player has a role on this server, by its name.HasPermission(name): the player has one of the game’s gameplay permissions right now, the kind a team’s settings grant andSetTempPermissionoverrides.IsTrusted(): the player is trusted by the owner of the chip’s bricks. Use it for a build’s own controls, so the people the owner builds with can use them too.
This /reset puts the visitor count back to 0, for trusted builders only:
@left in door: zone
var visits: int = 0
@right out sign = "Visitors: ${visits.Value}"
on ZoneEntered(zone = door) {
visits += 1
}
on ChatCommand("reset", "Reset the visitor count") -> (who, arguments) {
if !who.IsTrusted() {
who.ShowChatMessage("Only builders the owner trusts can reset the count")
return
}
visits = 0
who.ShowChatMessage("Visitor count reset")
}
Modify
- Refuse an empty message: if
arguments.Trim()is empty, tell the sender how to use/aand stop. - Let
/resetsay who did it: send every admin a line naming the player who reset the count. - Add
/bench namefor admins: find the player withFindPlayer, stop them respawning withSetCanRespawn(false)as in Player Control, and/unbench nameto let them back.
Make
Make a moderated sign. Any player can set it with /sign text. Only
trusted builders can lock it with /sign lock and unlock it with
/sign unlock. While it is locked, everyone else is told it is locked. Keep
the text short with sanitize.
chip sign -> decal Text
Good to Know
- A role is checked by its name, as typed in the server’s role settings. A misspelled name is not an error: the check is false for everyone.
- Every check runs when the command is typed, so a player who gains or loses a role is treated by their new roles at once.
- A command is there for everyone to type. The check is the only thing that keeps it to some players, so put it first, before the command does anything.
See Player info and ChatCommand in the reference.
Next: pick a lesson from the tree on the Learn page.