mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-19 09:37:50 -05:00
feat: add unified Discord bot and dashboard
This commit is contained in:
1 parent
113bf9342e
commit
b11ae1dfa8
14 files changed
+1708
-14
No files matched your search
+86
-1
@@ -1084,4 +1084,89 @@ const moderators = () => accounts.all().filter(account => roles.roleOf(account))
|
||||
.map(account => ({username: account.username, role: roles.roleOf(account), twoStep: totp.isOn(account), lastSeen: account.lastSeen || null}))
|
||||
.sort((a, b) => a.username.localeCompare(b.username));
|
||||
|
||||
module.exports = {handle, forget, moderators, signedInAs, itemRoutes, reviewRoutes};
|
||||
// The Discord bot is deliberately a small adapter over the panel rather than
|
||||
// its own moderator system. Its actor is a Discord user id that is already
|
||||
// linked to one Burrow account. From there these methods use the same role,
|
||||
// rank, permission and action checks as POST /mod/api/act, and log every
|
||||
// successful change under the staff member's normal Burrow username.
|
||||
const discordActor = discordId => {
|
||||
const account = accounts.all().find(candidate => candidate.discordId === String(discordId));
|
||||
return mayModerate(account) ? account : null;
|
||||
};
|
||||
|
||||
const discordError = (actor, permission) => !actor
|
||||
? {error: "Link this Discord account to an active Burrow staff account first."}
|
||||
: !roles.can(actor, permission) ? {error: "Your Burrow role cannot do that."} : null;
|
||||
|
||||
const discordStatus = actor => {
|
||||
const error = discordError(actor, "server.view");
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
const memory = process.memoryUsage();
|
||||
const players = world.players();
|
||||
return {ok: true, uptimeSeconds: Math.floor(process.uptime()), players: players.length,
|
||||
rooms: new Set(players.map(player => player.room).filter(Boolean)).size,
|
||||
reports: reports.openCount(), memoryMb: Math.round(memory.rss / 1048576)};
|
||||
};
|
||||
|
||||
const discordPlayer = (actor, username) => {
|
||||
const error = discordError(actor, "player.view");
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
const target = accounts.byName(String(username || "").trim());
|
||||
return target ? {ok: true, player: playerDetail(target)} : {error: "No such player."};
|
||||
};
|
||||
|
||||
const discordReports = actor => {
|
||||
const error = discordError(actor, "player.view");
|
||||
return error || {ok: true, reports: reports.list(false), open: reports.openCount()};
|
||||
};
|
||||
|
||||
const discordStaff = actor => {
|
||||
const error = discordError(actor, "staff.list");
|
||||
return error || {ok: true, staff: moderators()};
|
||||
};
|
||||
|
||||
const discordAnnounce = (actor, text) => {
|
||||
const error = discordError(actor, "server.announce");
|
||||
if (error) return error;
|
||||
const message = String(text || "").trim().slice(0, 300);
|
||||
if (!message) return {error: "The announcement is empty."};
|
||||
const reached = world.announce(message);
|
||||
modlog.record(actor.username, "Discord announcement", "", `${reached} player${reached === 1 ? "" : "s"}`);
|
||||
return {ok: reached === 1 ? "Shown to 1 player." : `Shown to ${reached} players.`};
|
||||
};
|
||||
|
||||
const discordHandleReport = (actor, id) => {
|
||||
const error = discordError(actor, "player.warn");
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
const handled = reports.markHandled(Number(id));
|
||||
if (handled) {
|
||||
modlog.record(actor.username, "report dealt with", "", `#${Number(id)}`);
|
||||
}
|
||||
return handled ? {ok: "Marked as dealt with."} : {error: "No such report waiting."};
|
||||
};
|
||||
|
||||
const discordAct = (actor, username, body) => {
|
||||
if (!actor) {
|
||||
return {error: "Link this Discord account to an active Burrow staff account first."};
|
||||
}
|
||||
const target = accounts.byName(String(username || "").trim());
|
||||
if (!target) {
|
||||
return {error: "No such player."};
|
||||
}
|
||||
const result = act(actor, target, body || {});
|
||||
if (result.ok) {
|
||||
modlog.record(actor.username, String(body.action), target.username, result.detail || "");
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const discordBot = {actor: discordActor, authorize: discordError, status: discordStatus, player: discordPlayer, reports: discordReports,
|
||||
staff: discordStaff, announce: discordAnnounce, handleReport: discordHandleReport, act: discordAct};
|
||||
|
||||
module.exports = {handle, forget, moderators, signedInAs, itemRoutes, reviewRoutes, discordBot};
|
||||
Reference in new issue
Block a user