commit
Hold reply DMs while the owner is active in that channel
Sending, typing or reacting in a channel marks the owner as there for five minutes. Replies in that window skip the DM; after it they resume. Co-Authored-By: Sandrone <[email protected]>
diff --git a/src/discord/client.ts b/src/discord/client.ts
index 38ebe16..5016ec0 100644
--- a/src/discord/client.ts
+++ b/src/discord/client.ts
@@ -8,6 +8,8 @@ export const client = new Client({
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions,
+ // Typing counts as being in chat, so reply DMs hold off
+ GatewayIntentBits.GuildMessageTyping,
],
// Lets ❌ and ❓ work on messages sent before the bot last started
partials: [
diff --git a/src/discord/replyNotice.ts b/src/discord/replyNotice.ts
index d5fe43b..43e7350 100644
--- a/src/discord/replyNotice.ts
+++ b/src/discord/replyNotice.ts
@@ -8,6 +8,7 @@ import {
} from "discord.js";
import { ownerUserId } from "../config/env.ts";
+import { isActive } from "../state/activity.ts";
import { client } from "./client.ts";
import { isProxied } from "./ownership.ts";
import { buildReplyCard } from "./replyCard.ts";
@@ -18,6 +19,9 @@ export async function notifyReply(message: Message): Promise<void> {
if (message.author.id === ownerUserId) return;
if (message.reference?.type !== MessageReferenceType.Default) return;
+ // They're already in this channel and will see it
+ if (isActive(message.channelId)) return;
+
const target = await message.fetchReference().catch(() => null);
if (!target || !isProxied(target)) return;
diff --git a/src/main.ts b/src/main.ts
index a59a5bc..0175166 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -7,12 +7,13 @@ import { refreshMembers } from "./api/members.ts";
import { refreshSystem } from "./api/system.ts";
import { connectLive } from "./api/live.ts";
import { memberRefreshMs } from "./config/constants.ts";
-import { discordToken } from "./config/env.ts";
+import { discordToken, ownerUserId } from "./config/env.ts";
import { client } from "./discord/client.ts";
import { proxyMessage } from "./discord/proxyMessage.ts";
import { handleReaction } from "./discord/reactions.ts";
import { notifyReply } from "./discord/replyNotice.ts";
import { updateStatus } from "./discord/status.ts";
+import { markActive } from "./state/activity.ts";
import { loadSettings } from "./state/settings.ts";
client.once(Events.ClientReady, (ready) => {
@@ -21,6 +22,8 @@ client.once(Events.ClientReady, (ready) => {
});
client.on(Events.MessageCreate, (message) => {
+ if (message.author.id === ownerUserId) markActive(message.channelId);
+
proxyMessage(message).catch((error) => {
console.error("Proxy failed:", error);
});
@@ -31,11 +34,17 @@ client.on(Events.MessageCreate, (message) => {
});
client.on(Events.MessageReactionAdd, (reaction, user) => {
+ if (user.id === ownerUserId) markActive(reaction.message.channelId);
+
handleReaction(reaction, user).catch((error) => {
console.error("Reaction failed:", error);
});
});
+client.on(Events.TypingStart, (typing) => {
+ if (typing.user.id === ownerUserId) markActive(typing.channel.id);
+});
+
await loadSettings();
await refreshSystem();
await refreshMembers();
diff --git a/src/state/activity.ts b/src/state/activity.ts
new file mode 100644
index 0000000..9806db0
--- /dev/null
+++ b/src/state/activity.ts
@@ -0,0 +1,16 @@
+/* src/state/activity.ts */
+
+// No sign of the owner in a channel for this long counts as away from it
+export const awayAfterMs = 5 * 60 * 1000;
+
+// Last time the owner typed, sent or reacted, by channel id
+const lastSeen = new Map<string, number>();
+
+export function markActive(channelId: string, now = Date.now()): void {
+ lastSeen.set(channelId, now);
+}
+
+export function isActive(channelId: string, now = Date.now()): boolean {
+ const seen = lastSeen.get(channelId);
+ return seen !== undefined && now - seen < awayAfterMs;
+}
diff --git a/tests/state/activity_test.ts b/tests/state/activity_test.ts
new file mode 100644
index 0000000..9af3b59
--- /dev/null
+++ b/tests/state/activity_test.ts
@@ -0,0 +1,22 @@
+/* tests/state/activity_test.ts */
+
+import { assertEquals } from "@std/assert";
+
+import { awayAfterMs, isActive, markActive } from "../../src/state/activity.ts";
+
+Deno.test("never seen in a channel means away", () => {
+ assertEquals(isActive("never"), false);
+});
+
+Deno.test("recent activity counts, old activity doesn't", () => {
+ markActive("chat", 1000);
+
+ assertEquals(isActive("chat", 1000 + awayAfterMs - 1), true);
+ assertEquals(isActive("chat", 1000 + awayAfterMs), false);
+});
+
+Deno.test("activity is per channel", () => {
+ markActive("general", 1000);
+
+ assertEquals(isActive("art", 1000), false);
+});