Staff roles with permissions, warnings, in-game staff commands

Moderators were one flag on the account. Now an account can hold a staff
role (lib/roles.js): helper, moderator or admin, each a list of named
permissions, with a higher role holding everything below it. An account
only marked `moderator` still counts as a moderator.

Every staff action checks the permission it needs, and staff never act
on staff of their own rank or above:

- warnings: kept on the account (who, when, the reason, a note for
  staff), and shown to the player with the game's own warning popup for
  the reason ("fw": inappropriate behavior, bullying, personal
  information, scamming), which this server's strings already word as the
  Burrow Rules.
- moderator panel: each action is checked against the role; new actions
  "warn" (with a reason and note), "summon" (bring a player to the room you
  are in) and "goto"; the player's page shows their warnings; GET
  /mod/api/me returns your role and permissions, and the page hides the
  buttons yours does not allow.
- in the game: !staff, !summon, !goto, !kick, !silence (HOURS, 0, or
  off), !warn PLAYER [reason] [note], !warnings PLAYER and !message, for
  staff whose role allows each, written to the moderators' log like the
  panel's actions, marked "(in game)". To anyone else they are plain chat.
  Usernames with spaces work.
- the login reply carries the player's own staffRole and
  staffPermissions (empty for everyone else), so a client can show staff
  tools only to staff.
- admin panel: a role picker on the account, "role" on POST
  /admin/api/account, and GET /admin/api/roles listing what each allows.
  The old moderator switch still works.

Also: the moderators' message box showed "--Message From Moderator--\n\n"
with the "\n" as written (strings 11134 and 11135 keep it as two
characters); they get real line breaks.

Staff powers need 2FA turned on, as the moderator panel already did. In
the game they come with the game session, which a trusted device opens
without a code; requiring a recent code there is left for later.

test/staff.js covers all of the above end to end.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
SnipzilandClaude Opus 5.5 committed 2026-10-16 22:42:46 -04:00
1 parent 63dc43ebb3
commit 2fba9f1612
10 files changed
+756 -21

No files matched your search

+202 -1
View File
@@ -6,6 +6,8 @@ const defpacks = require("./defpacks.js");
const disguises = require("./disguises.js");
const dens = require("./dens.js");
const {isSilenced} = require("./moderation.js");
const modlog = require("./modlog.js");
const roles = require("./roles.js");
const {MAX_DEN_ITEMS, MAX_WORN, activeAvatar, wornItems} = require("./player.js");
// things players can do by typing "!something" in the chat box, for what
@@ -47,7 +49,7 @@ const MAX_AGAIN = 100;
// A hosted party's own name is three string ids, no use outside the game.
const roomTitle = room => (room.partyHost ? `${room.partyHost.username}'s party` : room.displayName);
module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideBlocked, refreshForOthers, travel, reloadRoom, buyAgain, ask}) => {
module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideBlocked, refreshForOthers, travel, reloadRoom, buyAgain, ask, staffMove, staffModeration}) => {
// A plain message box with an OK button.
const tell = (session, text) => session.sendXt("xtl", -1, String(text).replace(/%/g, " percent"));
@@ -500,6 +502,201 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB
};
// ---- staff commands (lib/roles.js). each checks the role's permission on
// every use, and what is done is written down under the staff member's
// name, as in the moderator panel. to anyone without the permission they
// are plain chat, like any other "!word" the server does not know.
const MAX_STAFF_HOURS = 24 * 365 * 10;
const MAX_WARNING = 200;
const MAX_WARNINGS_SHOWN = 5;
const staffMay = (session, permission) => {
const moderation = staffModeration();
return roles.can(session.account, permission, moderation.banRemaining(session.account) !== 0);
};
// the longest run of leading words naming an online player, and the words
// after it: usernames may have spaces in them.
const splitName = words => {
for (let count = words.length; count > 0; count--) {
const target = findOnline(words.slice(0, count).join(" "));
if (target) {
return {target, rest: words.slice(count)};
}
}
return {target: null, rest: words};
};
// the same for any account, online or not.
const splitAccount = words => {
for (let count = words.length; count > 0; count--) {
const account = accounts.byName(words.slice(0, count).join(" "));
if (account) {
return account;
}
}
return null;
};
// finds the player the command is about, and checks the staff member may
// act on them (`onStaff`: also on staff below their rank).
const withTarget = (session, words, usage, onStaff, run) => {
const {target, rest} = splitName(words);
if (!words.length) {
tell(session, usage);
}
else if (!target) {
tell(session, `${words.join(" ")} is not online.`);
}
else if (target === session) {
tell(session, "That is you.");
}
else if (onStaff && !roles.outranks(session.account, target.account)) {
tell(session, "Staff of your rank or above are for someone higher up to deal with.");
}
else {
run(target, rest);
}
};
const logged = (session, action, target, detail = "") =>
modlog.record(session.account.username, `${action} (in game)`, target.account.username, detail);
const staffCommands = {
staff: {
permission: "staff.list",
run(session) {
const on = players().filter(other => roles.isActiveStaff(other.account))
.map(other => `${other.account.username} (${roles.ROLES[roles.roleOf(other.account)].label})`);
tell(session, on.length ? `Staff online: ${on.join(", ")}` : "No staff online.");
},
},
summon: {
permission: "player.summon",
run(session, words) {
withTarget(session, words, "Use: !summon PLAYER", true, target => {
const result = staffMove(target.account.username, session.account.username);
if (!result.ok) {
tell(session, result.error);
return;
}
tell(session, `${target.account.username} was brought here.`);
logged(session, "summon", target, result.room);
});
},
},
goto: {
permission: "player.goto",
run(session, words) {
withTarget(session, words, "Use: !goto PLAYER", false, target => {
const result = staffMove(session.account.username, target.account.username);
if (!result.ok) {
tell(session, result.error);
return;
}
logged(session, "goto", target, result.room);
});
},
},
kick: {
permission: "player.kick",
run(session, words) {
withTarget(session, words, "Use: !kick PLAYER", true, target => {
const name = target.account.username;
if (staffModeration().actions.kick(target.account)) {
tell(session, `${name} was disconnected.`);
logged(session, "kick", target);
}
else {
tell(session, `${name} is not online.`);
}
});
},
},
// "!silence PLAYER HOURS" (0: until lifted), "!silence PLAYER off".
silence: {
permission: "chat.silence",
run(session, words) {
withTarget(session, words, "Use: !silence PLAYER HOURS (0 until lifted), or !silence PLAYER off", true, (target, rest) => {
const {actions, PERMANENT} = staffModeration();
const name = target.account.username;
const amount = (rest[0] || "").toLowerCase();
if (amount === "off") {
actions.silence(target.account, 0);
tell(session, `${name} can chat again.`);
logged(session, "unsilence", target);
return;
}
const hours = /^\d{1,6}$/.test(amount) ? Number(amount) : NaN;
if (!(hours >= 0 && hours <= MAX_STAFF_HOURS)) {
tell(session, "Use: !silence PLAYER HOURS (0 until lifted), or !silence PLAYER off");
return;
}
actions.silence(target.account, hours === 0 ? PERMANENT : hours * 3600);
tell(session, hours === 0 ? `${name} cannot chat until it is lifted.` : `${name} cannot chat for ${hours} hours.`);
logged(session, "silence", target, String(hours || "until lifted"));
});
},
},
// "!warn PLAYER [REASON] [note]": a warning, kept on the account and
// shown with the game's own warning popup. REASON is behavior (the
// default), bullying, personal or scamming; the note is for staff.
warn: {
permission: "player.warn",
run(session, words) {
const usage = `Use: !warn PLAYER [${staffModeration().WARNINGS.join("|")}] [note]`;
withTarget(session, words, usage, true, (target, rest) => {
const {WARNINGS, actions} = staffModeration();
const asked = (rest[0] || "").toLowerCase();
const reason = WARNINGS.includes(asked) ? asked : "behavior";
const note = (WARNINGS.includes(asked) ? rest.slice(1) : rest).join(" ").trim().slice(0, MAX_WARNING);
const result = actions.warn(target.account, reason, session.account.username, note);
tell(session, `${target.account.username} warned (${reason}): warning ${result.count}.`);
logged(session, "warn", target, note ? `${reason} · ${note}` : reason);
});
},
},
// "!message PLAYER text": a message box from the moderators.
message: {
permission: "player.warn",
run(session, words) {
withTarget(session, words, "Use: !message PLAYER text", true, (target, rest) => {
const text = rest.join(" ").trim().slice(0, MAX_WARNING);
if (!text) {
tell(session, "Use: !message PLAYER text");
return;
}
if (staffModeration().actions.message(target.account, text)) {
tell(session, `Shown to ${target.account.username}.`);
logged(session, "message", target, text);
}
else {
tell(session, `${target.account.username} is not online.`);
}
});
},
},
// "!warnings PLAYER": their latest warnings, online or not.
warnings: {
permission: "player.warn",
run(session, words) {
const account = splitAccount(words);
if (!account) {
tell(session, words.length ? `No player called ${words.join(" ")}.` : "Use: !warnings PLAYER");
return;
}
const list = account.warnings || [];
if (!list.length) {
tell(session, `${account.username} has no warnings.`);
return;
}
const latest = list.slice(-MAX_WARNINGS_SHOWN).reverse()
.map(entry => `${new Date(entry.at * 1000).toISOString().slice(0, 10)} ${entry.reason} by ${entry.by}${entry.note ? ` (${entry.note})` : ""}`);
tell(session, `${account.username} has ${list.length} warning${list.length === 1 ? "" : "s"}. Latest: ${latest.join("; ")}`);
},
},
};
return {
// runs `text` if it is a command. returns what to do with the chat line:
// null to swallow it, or the text everyone should see in its place.
@@ -509,6 +706,10 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB
}
const typed = text.slice(PREFIX.length).trim().split(/\s+/);
const [name, ...words] = typed.map(word => word.toLowerCase());
if (Object.hasOwn(staffCommands, name) && staffMay(session, staffCommands[name].permission)) {
staffCommands[name].run(session, typed.slice(1));
return null;
}
if (Object.hasOwn(asTyped, name) && (name !== "den" || typed.length > 1)) {
asTyped[name](session, typed.slice(1));
return null;