diff --git a/config.js b/config.js index 0261323..62b4b5e 100644 --- a/config.js +++ b/config.js @@ -285,6 +285,10 @@ module.exports = { idleTimeoutMs: 15 * 60000, // the client sends a keep-alive far more often tokenLifetimeDays: 30, // login tokens kept by the launcher's "remember me" maxChatLength: 300, + // warnings (lib/moderation.js warn): the Nth one within `days` also + // silences the player for that many minutes, [[N, minutes], ...]. a + // longer silence already on them stays. steps [] turns it off. + warningSilence: {days: 30, steps: [[3, 60], [5, 24 * 60]]}, // days typed chat and whispers are kept for the moderator panel's chat // history (lib/chatlog.js); 0 keeps none. chatLogDays: env.AJ_CHAT_LOG_DAYS === undefined ? 30 : Math.max(0, Number(env.AJ_CHAT_LOG_DAYS) || 0), diff --git a/lib/commands.js b/lib/commands.js index a24d20d..efef264 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -682,7 +682,8 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB 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}.`); + tell(session, `${target.account.username} warned (${reason}): warning ${result.count}.` + + (result.silencedMinutes ? ` Silenced for ${result.silencedMinutes} minutes, for that many warnings.` : "")); logged(session, "warn", target, note ? `${reason} · ${note}` : reason); }); }, @@ -707,6 +708,33 @@ module.exports = ({findOnline, players, watchers, sendItemList, blocks, setHideB }); }, }, + // "!unwarn PLAYER": takes back their latest warning (one given by + // mistake), online or not. + unwarn: { + permission: "player.warn", + run(session, words) { + const account = splitAccount(words); + if (!account) { + tell(session, words.length ? `No player called ${words.join(" ")}.` : "Use: !unwarn PLAYER"); + return; + } + if (account === session.account) { + tell(session, "That is you."); + return; + } + if (!roles.outranks(session.account, account)) { + tell(session, "Staff of your rank or above are for someone higher up to deal with."); + return; + } + const removed = staffModeration().actions.unwarn(account); + if (!removed) { + tell(session, `${account.username} has no warnings.`); + return; + } + tell(session, `Took back ${account.username}'s latest warning (${removed.reason}).`); + modlog.record(session.account.username, "unwarn (in game)", account.username, removed.reason); + }, + }, // "!warnings PLAYER": their latest warnings, online or not. warnings: { permission: "player.warn", diff --git a/lib/moderation.js b/lib/moderation.js index 60331f0..919b081 100644 --- a/lib/moderation.js +++ b/lib/moderation.js @@ -4,6 +4,7 @@ const accounts = require("./accounts.js"); const achievements = require("./achievements.js"); const bans = require("./bans.js"); const dens = require("./dens.js"); +const config = require("../config.js"); const {CURRENCIES} = require("./player.js"); // moderation: kick, ban, silence, clear a den, grant gems, message a player. @@ -29,6 +30,7 @@ const WARNINGS = { }; const MAX_WARNINGS_KEPT = 100; const MAX_WARNING_NOTE = 200; +const SECONDS_PER_DAY = 86400; const now = () => Math.floor(Date.now() / 1000); @@ -47,6 +49,21 @@ const banOf = account => { return seconds === 0 ? null : {seconds, reason: account.banReason || bans.NO_REASON, by: account.bannedBy || "", since: account.bannedAt || null}; }; +// the warnings an account has had in the last config.warningSilence.days +// (all of them when that is 0). +const recentWarnings = account => { + const days = (config.warningSilence || {}).days || 0; + const since = now() - days * SECONDS_PER_DAY; + return (account.warnings || []).filter(entry => !days || entry.at >= since).length; +}; + +// the minutes of silence a warning brings when it is the Nth recent one +// (config.warningSilence.steps, [[N, minutes], ...]); 0 for none. +const silenceForWarning = count => { + const step = ((config.warningSilence || {}).steps || []).find(([warnings]) => warnings === count); + return step ? step[1] : 0; +}; + const isSilenced = account => account.silencedUntil === PERMANENT || (account.silencedUntil || 0) > now(); // seconds of silence left, PERMANENT, or 0 when the account may chat. @@ -185,20 +202,47 @@ module.exports = ({findOnline}) => { }, // a warning: kept on the account (who, when, why and a note for staff), - // and shown with the game's own warning popup if they are online. - // returns {count, online}, or null for a reason that is not one. + // and shown with the game's own warning popup: now if they are in a + // room, else once they are next (greet). the Nth warning within + // config.warningSilence.days may also silence them for a while; a + // longer silence already on them is left alone. + // returns {count, online, silencedMinutes}, or null for a reason that + // is not one. warn(target, reason, by, note = "") { if (!Object.hasOwn(WARNINGS, reason)) { return null; } - target.warnings = [...(target.warnings || []), {at: now(), by, reason, note: String(note).slice(0, MAX_WARNING_NOTE)}] + const session = findOnline(target.username); + const seen = Boolean(session && session.greeted); + target.warnings = [...(target.warnings || []), {at: now(), by, reason, note: String(note).slice(0, MAX_WARNING_NOTE), seen}] .slice(-MAX_WARNINGS_KEPT); accounts.save(); - const session = findOnline(target.username); - if (session) { + if (seen) { discipline(session, "fw", WARNINGS[reason]); } - return {count: target.warnings.length, online: Boolean(session)}; + let silencedMinutes = silenceForWarning(recentWarnings(target)); + const left = silenceRemaining(target); + if (silencedMinutes && left !== PERMANENT && left < silencedMinutes * SECONDS_PER_MINUTE) { + actions.silence(target, silencedMinutes * SECONDS_PER_MINUTE); + } + else { + silencedMinutes = 0; + } + return {count: target.warnings.length, online: Boolean(session), silencedMinutes}; + }, + + // takes back their latest warning (one given by mistake). a silence it + // brought stays; that is lifted on its own (silence 0). returns the + // warning taken back, or null when there is none. + unwarn(target) { + const list = target.warnings || []; + if (!list.length) { + return null; + } + const removed = list[list.length - 1]; + target.warnings = list.slice(0, -1); + accounts.save(); + return removed; }, }; @@ -207,7 +251,20 @@ module.exports = ({findOnline}) => { const refuse = reply => session => session.sendXt(reply, -1, 0); const handlers = {fk: refuse("fk"), fb: refuse("fb"), fs: refuse("fs"), fm: refuse("fm"), fg: refuse("fg"), fp: refuse("fp"), fc() {}}; - return {handlers, actions, banRemaining, banOf, isSilenced, silenceRemaining, PERMANENT, WARNINGS: Object.keys(WARNINGS)}; + // the first room has loaded (lib/world.js): a warning given while they + // were away is shown now, the latest only (the game has one warning + // popup), and all of them count as seen. + const greet = session => { + const unseen = (session.account.warnings || []).filter(entry => entry.seen === false); + if (!unseen.length) { + return; + } + unseen.forEach(entry => { entry.seen = true; }); + accounts.save(); + discipline(session, "fw", WARNINGS[unseen[unseen.length - 1].reason] ?? WARNINGS.behavior); + }; + + return {handlers, actions, greet, banRemaining, banOf, isSilenced, silenceRemaining, PERMANENT, WARNINGS: Object.keys(WARNINGS)}; }; Object.assign(module.exports, {isSilenced, banRemaining, banOf}); diff --git a/lib/modpanel.js b/lib/modpanel.js index f25e846..6337715 100644 --- a/lib/modpanel.js +++ b/lib/modpanel.js @@ -144,12 +144,18 @@ const wholeNumber = (value, min, max) => (Number.isInteger(value) && value >= mi const ACTION_PERMISSIONS = { ban: "player.ban", banReason: "player.ban", unban: "player.ban", silence: "chat.silence", unsilence: "chat.silence", - kick: "player.kick", message: "player.warn", warn: "player.warn", + kick: "player.kick", message: "player.warn", warn: "player.warn", unwarn: "player.warn", rejectName: "player.names", acceptName: "player.names", clearBio: "player.names", banAddress: "address.ban", summon: "player.summon", goto: "player.goto", }; +// minutes as a person would say them: "1 hour", "1 day", "90 minutes". +const silenceText = minutes => { + const [count, unit] = minutes % 1440 === 0 ? [minutes / 1440, "day"] : minutes % 60 === 0 ? [minutes / 60, "hour"] : [minutes, "minute"]; + return `${count} ${unit}${count === 1 ? "" : "s"}`; +}; + const act = (mod, target, body) => { const {actions, PERMANENT} = world.moderation; const name = target.username; @@ -209,9 +215,15 @@ const act = (mod, target, body) => { if (!result) { return {error: `A warning's reason: ${world.moderation.WARNINGS.join(", ")}.`}; } - return {ok: `${name} warned (${reason}): warning ${result.count}.${result.online ? "" : " They are offline, so they will not see it."}`, + return {ok: `${name} warned (${reason}): warning ${result.count}.${result.online ? "" : " They are offline: they will see it when they next play."}` + + (result.silencedMinutes ? ` That many warnings also silenced them for ${silenceText(result.silencedMinutes)}.` : ""), detail: `${reason}${body.note ? ` · ${String(body.note).slice(0, 200)}` : ""}`}; } + case "unwarn": { + const removed = actions.unwarn(target); + return removed ? {ok: `Took back ${name}'s latest warning (${removed.reason}).`, detail: removed.reason} + : {error: `${name} has no warnings.`}; + } case "rejectName": if (target.nameRejected) { return {error: "Already rejected."}; diff --git a/lib/roles.js b/lib/roles.js index 3c554a7..7341903 100644 --- a/lib/roles.js +++ b/lib/roles.js @@ -16,7 +16,7 @@ const totp = require("./totp.js"); const PERMISSIONS = { "player.view": "see players, chat, reports, requests, bans and paintings in the moderator panel", "staff.list": "see which staff are online (!staff)", - "player.warn": "warn a player and send them a message (!warn, !warnings, !message)", + "player.warn": "warn a player, take a warning back and send them a message (!warn, !unwarn, !warnings, !message)", "player.goto": "go to the room a player is in (!goto)", "chat.silence": "silence and unsilence a player (!silence)", "player.kick": "disconnect a player (!kick)", diff --git a/lib/world.js b/lib/world.js index 6b7cdaa..795f630 100644 --- a/lib/world.js +++ b/lib/world.js @@ -464,7 +464,7 @@ const sendUserVars = session => { }; const findOnline = username => online.get(String(username).toLowerCase()); -const STAFF_WINDOW_COMMANDS = ["warn", "message", "silence", "kick", "summon", "goto", "warnings"]; +const STAFF_WINDOW_COMMANDS = ["warn", "unwarn", "message", "silence", "kick", "summon", "goto", "warnings"]; const MAX_STAFF_WINDOW_WORDS = 3; // username, then a reason, hours or the message (each one field) // a custom colour (lib/colours.js) for these four RGB values, made if it is @@ -603,6 +603,7 @@ const handlers = { } buddies.greet(session); minigames.greet(session); + moderation.greet(session); // never asked what chat they want filtered: the window is put to // them, this once (the patched client's "xfa"). if (!session.account.filter.chosen) { diff --git a/public/mod.html b/public/mod.html index c708f61..9a2de58 100644 --- a/public/mod.html +++ b/public/mod.html @@ -316,6 +316,7 @@ +