Skip to content

fix: reconcile tags when changes occur #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"dependencies": {
"@uwu/configmasher": "latest",
"discord.js": "^14.15.3",
"ofetch": "^1.4.1"
"ofetch": "^1.4.1",
"throttle-debounce": "^5.0.2"
},
"trustedDependencies": [
"@biomejs/biome"
Expand Down
18 changes: 12 additions & 6 deletions src/commands/util/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import {
const getStateWord = (close) => (close ? "closed" : "reopened");
const getStateVerb = (close) => (close ? "close" : "reopen");

export function getTagsForCloseState(close: boolean) {
return {
tagToAdd: close
? config.helpChannel.closedTag
: config.helpChannel.openedTag,
tagToRemove: close
? config.helpChannel.openedTag
: config.helpChannel.closedTag,
};
}

export async function handleIssueState(
interaction: ChatInputCommandInteraction,
close = true,
Expand All @@ -29,12 +40,7 @@ export async function handleIssueState(
const stateWord = getStateWord(close);
const stateVerb = getStateVerb(close);

const tagToAdd = close
? config.helpChannel.closedTag
: config.helpChannel.openedTag;
const tagToRemove = close
? config.helpChannel.openedTag
: config.helpChannel.closedTag;
const { tagToAdd, tagToRemove } = getTagsForCloseState(close);

const postTags = threadChannel.appliedTags;

Expand Down
80 changes: 80 additions & 0 deletions src/events/channels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { config } from "../lib/config.js";
import { getTagsForCloseState } from "../commands/util/close.js";
import { isHelpPost } from "../lib/discord/channels.js";

import { debounce } from "throttle-debounce";

import { type Client, Events, type ThreadChannel } from "discord.js";

// Map to store initial thread states
const threadUpdateMap = new Map<string, ThreadChannel>();

// Create a debounced handler for processing thread updates
const handleEvent = debounce(
1000,
async (threadId: string, newThread: ThreadChannel) => {
const initialThread = threadUpdateMap.get(threadId);
if (!initialThread) return;

// Remove from map
threadUpdateMap.delete(threadId);

// Handle tag additions
const addedTags = newThread.appliedTags.filter(
(t) => !initialThread.appliedTags.includes(t),
);
if (addedTags.length > 0) {
for (const tag of addedTags) {
// If closed/opened tag is added, remove the opposite tag
if (
tag === config.helpChannel.closedTag ||
tag === config.helpChannel.openedTag
) {
const isClose = tag === config.helpChannel.closedTag;
const { tagToRemove } = getTagsForCloseState(isClose);
if (newThread.appliedTags.includes(tagToRemove)) {
await newThread.setAppliedTags(
newThread.appliedTags.filter((t) => t !== tagToRemove),
);
}
}
}
}

// Handle tag removals
const removedTags = initialThread.appliedTags.filter(
(t) => !newThread.appliedTags.includes(t),
);
if (removedTags.length > 0) {
for (const tag of removedTags) {
// If closed or opened tag is removed, add it back only if its opposite isn't present
if (
tag === config.helpChannel.closedTag ||
tag === config.helpChannel.openedTag
) {
const isClose = tag === config.helpChannel.closedTag;
const { tagToRemove } = getTagsForCloseState(isClose);
if (!newThread.appliedTags.includes(tagToRemove)) {
await newThread.setAppliedTags([...newThread.appliedTags, tag]);
}
}
}
}
},
);

export default function registerEvents(client: Client) {
client.on(Events.ThreadUpdate, async (oldThread, newThread) => {
if (!(await isHelpPost(newThread))) {
return;
}

// Store the initial state if this is the first update
if (!threadUpdateMap.has(newThread.id)) {
threadUpdateMap.set(newThread.id, oldThread);
}

// Trigger the debounced handler
handleEvent(newThread.id, newThread);
});
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { config } from "./lib/config.js";
import registerCommandEvents from "./events/commands.js";
import registerWalkthroughEvents from "./events/walkthrough.js";
import registerMessageEvents from "./events/messages.js";
import registerChannelEvents from "./events/channels.js";

import { Client, Events, GatewayIntentBits, ActivityType } from "discord.js";

Expand Down Expand Up @@ -37,6 +38,7 @@ client.once(Events.ClientReady, () => {
registerCommandEvents(client);
registerWalkthroughEvents(client);
registerMessageEvents(client);
registerChannelEvents(client);

shufflePresence();
setInterval(shufflePresence, config.presenceDelay);
Expand Down