Dough Git

commit

Add -g flag to d!autoproxy for every server

commit
f7474c251354d164470641a5e43bf1345b24127a
author
Clove Twilight <[email protected]>
date
2026-11-18 06:21
Co-Authored-By: Sandrone <[email protected]>

diff --git a/src/commands/autoproxy.ts b/src/commands/autoproxy.ts
index ddf45c4..fe9e26d 100644
--- a/src/commands/autoproxy.ts
+++ b/src/commands/autoproxy.ts
@@ -2,12 +2,22 @@
 
 import type { Message } from "discord.js";
 
-import { guildSettings, saveSettings } from "../state/settings.ts";
+import {
+    guildSettings,
+    saveSettings,
+    setAutoproxyEverywhere,
+    settings,
+} from "../state/settings.ts";
 
 export async function autoproxyCommand(
     message: Message,
     args: string[],
 ): Promise<void> {
+    if (args[0] === "-g") {
+        await globalAutoproxy(message, args[1]);
+        return;
+    }
+
     const [choice] = args;
 
     // Commands only run from server messages, so guildId is always set
@@ -20,7 +30,7 @@ export async function autoproxyCommand(
     }
 
     if (choice !== "on" && choice !== "off") {
-        await message.reply("Use `d!autoproxy on` or `d!autoproxy off`.");
+        await message.reply("Use `d!autoproxy on|off`, or `d!autoproxy -g on|off` for every server.");
         return;
     }
 
@@ -33,3 +43,24 @@ export async function autoproxyCommand(
     await saveSettings();
     await message.reply(`Autoproxy in this server is now **${choice}**.`);
 }
+
+async function globalAutoproxy(
+    message: Message,
+    choice: string | undefined,
+): Promise<void> {
+    if (choice === undefined) {
+        const guilds = Object.values(settings);
+        const enabledCount = guilds.filter((guild) => guild.autoproxy.enabled).length;
+        await message.reply(`Autoproxy is **on** in ${enabledCount} of ${guilds.length} servers.`);
+        return;
+    }
+
+    if (choice !== "on" && choice !== "off") {
+        await message.reply("Use `d!autoproxy -g on` or `d!autoproxy -g off`.");
+        return;
+    }
+
+    const serverCount = setAutoproxyEverywhere(choice === "on");
+    await saveSettings();
+    await message.reply(`Autoproxy is now **${choice}** in all ${serverCount} servers.`);
+}
diff --git a/src/commands/help.ts b/src/commands/help.ts
index 644e791..e464594 100644
--- a/src/commands/help.ts
+++ b/src/commands/help.ts
@@ -5,7 +5,7 @@ import type { Message } from "discord.js";
 const helpText = [
     "`d!edit <text>` edits the message you reply to, or your latest proxied message. `d!e` works too.",
     "`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. `d!ap` 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.",
     "-# React ❌ to delete a proxied message, ❓ to see who sent it.",
 ].join("\n");
 
diff --git a/src/state/settings.ts b/src/state/settings.ts
index e31aae3..32c0fa7 100644
--- a/src/state/settings.ts
+++ b/src/state/settings.ts
@@ -57,6 +57,13 @@ export async function syncGuilds(guildIds: Iterable<string>): Promise<void> {
     if (changed) await saveSettings();
 }
 
+// Every server the bot is in has an entry, via syncGuilds and addGuild
+export function setAutoproxyEverywhere(enabled: boolean): number {
+    const guilds = Object.values(settings);
+    for (const guild of guilds) guild.autoproxy.enabled = enabled;
+    return guilds.length;
+}
+
 export async function addGuild(guildId: string): Promise<void> {
     guildSettings(guildId);
     await saveSettings();
diff --git a/tests/state/settings_test.ts b/tests/state/settings_test.ts
index dbb017d..ed78a8f 100644
--- a/tests/state/settings_test.ts
+++ b/tests/state/settings_test.ts
@@ -2,7 +2,25 @@
 
 import { assertEquals } from "@std/assert";
 
-import { guildSettings, settings } from "../../src/state/settings.ts";
+import {
+    guildSettings,
+    setAutoproxyEverywhere,
+    settings,
+} from "../../src/state/settings.ts";
+
+Deno.test("setAutoproxyEverywhere changes every server", () => {
+    guildSettings("1").autoproxy.enabled = true;
+    guildSettings("2");
+
+    assertEquals(setAutoproxyEverywhere(false), 2);
+    assertEquals(guildSettings("1").autoproxy.enabled, false);
+
+    assertEquals(setAutoproxyEverywhere(true), 2);
+    assertEquals(guildSettings("2").autoproxy.enabled, true);
+
+    delete settings["1"];
+    delete settings["2"];
+});
 
 Deno.test("autoproxy is per server and off by default", () => {
     guildSettings("1").autoproxy.enabled = true;