commit
Add d!switch to set the front on the system site
Co-Authored-By: Sandrone <[email protected]>
diff --git a/src/api/switch.ts b/src/api/switch.ts
new file mode 100644
index 0000000..128c866
--- /dev/null
+++ b/src/api/switch.ts
@@ -0,0 +1,31 @@
+/* src/api/switch.ts */
+
+import { apiBase } from "../config/constants.ts";
+import { siteToken } from "../config/env.ts";
+
+// The new front comes back over the live socket, so nothing is set here.
+// Returns an error to show, or null when the switch went through
+export async function switchFront(memberIds: string[]): Promise<string | null> {
+ if (!siteToken) return "Switching isn't set up. Add `SITE_TOKEN` to the bot's `.env`.";
+
+ try {
+ const response = await fetch(`${apiBase}/switch`, {
+ method: "POST",
+ headers: {
+ "Authorization": `Bearer ${siteToken}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ members: memberIds,
+ }),
+ });
+
+ if (response.status === 401) return "The site rejected the bot's token. Check `SITE_TOKEN` matches `BOT_TOKEN`.";
+ if (!response.ok) return `The site couldn't switch (HTTP ${response.status}).`;
+
+ return null;
+ } catch (error) {
+ console.error("Switch failed:", error);
+ return "Couldn't reach the site to switch.";
+ }
+}
diff --git a/src/commands/help.ts b/src/commands/help.ts
index 5f0fb09..5607c24 100644
--- a/src/commands/help.ts
+++ b/src/commands/help.ts
@@ -7,6 +7,7 @@ const helpText = [
"`d!explain` explains the bot, and plurality. `d!ex` works too,",
"`d!autoproxy` shows the state for this server, `d!autoproxy on|off` sets it. Add `-g` for every server, like `d!autoproxy -g off`. `d!ap` works too.",
"`d!reproxy <member>` resends the message you reply to, or your latest proxied message, as someone else, like `d!reproxy c1`. `d!rp` works too.",
+ "`d!switch <member>` sets who's fronting on the site, like `d!switch c1 c2`. `d!switch out` clears it. `d!sw` works too.",
"-# React ❌ to delete a proxied message, ❓ to see who sent it.",
].join("\n");
diff --git a/src/commands/index.ts b/src/commands/index.ts
index bfdd409..a0dcd03 100644
--- a/src/commands/index.ts
+++ b/src/commands/index.ts
@@ -8,6 +8,7 @@ import { explainCommand } from "./explain.ts";
import { helpCommand } from "./help.ts";
import { parseCommand } from "./parse.ts";
import { reproxyCommand } from "./reproxy.ts";
+import { switchCommand } from "./switch.ts";
type CommandHandler = (
message: Message,
@@ -24,6 +25,8 @@ const commands: Record<string, CommandHandler> = {
ap: autoproxyCommand,
reproxy: reproxyCommand,
rp: reproxyCommand,
+ switch: switchCommand,
+ sw: switchCommand,
help: helpCommand
};
diff --git a/src/commands/switch.ts b/src/commands/switch.ts
new file mode 100644
index 0000000..7c15852
--- /dev/null
+++ b/src/commands/switch.ts
@@ -0,0 +1,41 @@
+/* src/commands/switch.ts */
+
+import type { Message } from "discord.js";
+
+import { switchFront } from "../api/switch.ts";
+import { membersByName, webhookName } from "../proxy/names.ts";
+import { system } from "../state/system.ts";
+
+export async function switchCommand(
+ message: Message,
+ args: string[],
+): Promise<void> {
+ if (args.length === 0) {
+ await message.reply("Use `d!switch <member>`, like `d!switch c1`, several like `d!sw c1 c2`, or `d!sw out`.");
+ return;
+ }
+
+ const switchingOut = args.length === 1 && args[0] === "out";
+ const { found, missing } = switchingOut
+ ? { found: [], missing: [] }
+ : membersByName(system.members, args);
+
+ if (missing.length > 0) {
+ const names = missing.map((name) => `\`${name}\``).join(", ");
+ await message.reply(`No member called ${names}. Use their short name, like \`c1\`.`);
+ return;
+ }
+
+ const problem = await switchFront(found.map((member) => member.id));
+
+ if (problem) {
+ await message.reply(problem);
+ return;
+ }
+
+ await message.reply(
+ switchingOut
+ ? "Switched out. Nobody is fronting."
+ : `Switched in **${webhookName(found, null)}**.`,
+ );
+}
diff --git a/src/config/env.ts b/src/config/env.ts
index a75ed17..88a4e6f 100644
--- a/src/config/env.ts
+++ b/src/config/env.ts
@@ -9,3 +9,6 @@ if (!token || !ownerId) {
export const discordToken: string = token;
export const ownerUserId: string = ownerId;
+
+// Optional: without it everything works except d!switch
+export const siteToken: string | undefined = Deno.env.get("SITE_TOKEN");