feat: add unified Discord bot and dashboard

This commit is contained in:
Snipzil committed 2026-10-20 22:13:50 -04:00
1 parent 113bf9342e
commit b11ae1dfa8
14 files changed
+1708 -14

No files matched your search

+19 -2
View File
@@ -45,6 +45,7 @@ const MERGE_SECONDS = 10;
const perTenMinutes = limits.limiter(10, 600000);
const now = () => Math.floor(Date.now() / 1000);
const clean = (text, max) => String(text === undefined ? "" : text).replace(/[\u0000-\u0008\u000b-\u001f\u007f]/g, " ").trim().slice(0, max);
const listeners = new Set();
const load = () => accounts.database().exec(SCHEMA);
@@ -68,9 +69,18 @@ const file = ({reporter, target, reason, detail = "", context = "", room = ""})
if (!perTenMinutes.allow(reporter.toLowerCase()) || openCount() >= MAX_OPEN) {
return false;
}
db.prepare("INSERT INTO reports (created_at, reporter, target, reason, detail, context, room) VALUES (?, ?, ?, ?, ?, ?, ?)")
const inserted = db.prepare("INSERT INTO reports (created_at, reporter, target, reason, detail, context, room) VALUES (?, ?, ?, ?, ?, ?, ?)")
.run(now(), reporter, target, Number.isInteger(reason) ? reason : 0, clean(detail, MAX_DETAIL), clean(context, MAX_CONTEXT), clean(room, 100));
console.log(`[world] ${logText(reporter)} reported "${logText(target)}" (${logText(REASONS[reason] || `reason ${reason}`)})`);
const report = row(db.prepare("SELECT * FROM reports WHERE id = ?").get(Number(inserted.lastInsertRowid)));
for (const listener of listeners) {
try {
listener(report);
}
catch (err) {
console.error(`[reports] listener failed: ${logText(err.message)}`);
}
}
return true;
};
@@ -95,8 +105,15 @@ const list = (handled = false) => accounts.database()
const markHandled = id => accounts.database().prepare("UPDATE reports SET handled_at = ? WHERE id = ? AND handled_at IS NULL").run(now(), id).changes > 0;
// Lets optional integrations (the Discord bot) receive a newly filed report
// without making a report depend on an external service being available.
const onFile = listener => {
listeners.add(listener);
return () => listeners.delete(listener);
};
// the reports about one player, newest first, for their timeline.
const about = username => accounts.database().prepare("SELECT * FROM reports WHERE target = ? COLLATE NOCASE ORDER BY id DESC LIMIT 100")
.all(String(username)).map(row);
module.exports = {load, file, list, about, markHandled, openCount, MAX_DETAIL};
module.exports = {load, file, list, about, markHandled, onFile, openCount, MAX_DETAIL};