diff --git a/README.md b/README.md index d43ddfb..fed3ba0 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ AutoMod is [Stoat's](https://stoat.chat) premier moderation bot, featuring loggi ## Features - Basic moderation commands, including kicking, warning, banning, and timing out users. +- A web dashboard for easy configuration. - Full message filtering. - Customisable event logging (including logging of message edits/deletions and moderator actions). - Full infraction system to keep track of problematic users. @@ -18,7 +19,6 @@ AutoMod is [Stoat's](https://stoat.chat) premier moderation bot, featuring loggi - Intuitive command syntax. - Anti-spam measures. - Handy extra commands for changing user nicknames and avatars, getting information from a UUID, etc. -- WIP web interface for easy configuration. ## Documentation and Support diff --git a/packages/api/src/middlewares/cors.ts b/packages/api/src/middlewares/cors.ts index 108bce9..cc8866c 100644 --- a/packages/api/src/middlewares/cors.ts +++ b/packages/api/src/middlewares/cors.ts @@ -1,8 +1,16 @@ import type { Request, Response, NextFunction } from "express"; import { app } from ".."; +const ALLOWED_ORIGIN = "https://automod.vale.rocks"; + app.use("*", (req: Request, res: Response, next: NextFunction) => { - res.header("Access-Control-Allow-Origin", "*"); + const origin = req.headers.origin; + if (origin === ALLOWED_ORIGIN) { + res.header("Access-Control-Allow-Origin", origin); + res.header("Access-Control-Allow-Credentials", "true"); + } else { + res.header("Access-Control-Allow-Origin", "*"); + } res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, x-auth-user, x-auth-token"); res.header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS"); diff --git a/packages/api/src/routes/stats.ts b/packages/api/src/routes/stats.ts index 82d095d..2aa9d9e 100644 --- a/packages/api/src/routes/stats.ts +++ b/packages/api/src/routes/stats.ts @@ -5,6 +5,10 @@ import { botReq } from "./internal/ws"; let SERVER_COUNT = 0; let BOT_NAME = "AutoMod"; let BOT_ID = ""; +let USER_COUNT = 0; +let INFRACTION_COUNT = 0; +let BOT_UPTIME = 0; +let BOT_PING: number | null = null; const fetchStats = async () => { try { @@ -13,6 +17,10 @@ const fetchStats = async () => { if (res["servers"]) SERVER_COUNT = Number(res["servers"]); if (res["botName"]) BOT_NAME = String(res["botName"]); if (res["botId"]) BOT_ID = String(res["botId"]); + if (res["users"]) USER_COUNT = Number(res["users"]); + if (res["infractions"]) INFRACTION_COUNT = Number(res["infractions"]); + if (res["uptime"] !== undefined) BOT_UPTIME = Number(res["uptime"]); + if (res["ping"] !== undefined) BOT_PING = res["ping"] as number | null; } catch (e) { console.error(e); } @@ -26,5 +34,9 @@ app.get("/stats", async (_req: Request, res: Response) => { servers: SERVER_COUNT, botName: BOT_NAME, botId: BOT_ID, + users: USER_COUNT, + infractions: INFRACTION_COUNT, + uptime: BOT_UPTIME, + ping: BOT_PING, }); }); diff --git a/packages/bot/src/bot/modules/api_communication.ts b/packages/bot/src/bot/modules/api_communication.ts index fd4fd75..e1e5aa2 100644 --- a/packages/bot/src/bot/modules/api_communication.ts +++ b/packages/bot/src/bot/modules/api_communication.ts @@ -154,9 +154,19 @@ wsEvents.on("req:requestLogin", async (data: any, cb: (data: WSResponse) => void } }); -wsEvents.on("req:stats", async (_data: any, cb: (data: { servers: number; botName: string; botId: string }) => void) => { +wsEvents.on("req:stats", async (_data: any, cb: (data: { servers: number; botName: string; botId: string; users: number; infractions: number; uptime: number; ping: number | null }) => void) => { const servers = bot.servers.size(); - cb({ servers, botName: bot.user?.username || "AutoMod", botId: bot.user?.id || "" }); + const users = bot.users.size(); + const infractions = await dbs.INFRACTIONS.estimatedDocumentCount().catch(() => dbs.INFRACTIONS.countDocuments({}).catch(() => 0)); + cb({ + servers, + botName: bot.user?.username || "AutoMod", + botId: bot.user?.id || "", + users, + infractions, + uptime: process.uptime(), + ping: bot.events.ping() ?? null, + }); }); export { wsEvents, wsSend };