|
| 1 | +import { config } from "../lib/config.js"; |
| 2 | +import { getTagsForCloseState } from "../commands/util/close.js"; |
| 3 | +import { isHelpPost } from "../lib/discord/channels.js"; |
| 4 | + |
| 5 | +import { debounce } from "throttle-debounce"; |
| 6 | + |
| 7 | +import { type Client, Events, type ThreadChannel } from "discord.js"; |
| 8 | + |
| 9 | +// Map to store initial thread states |
| 10 | +const threadUpdateMap = new Map<string, ThreadChannel>(); |
| 11 | + |
| 12 | +// Create a debounced handler for processing thread updates |
| 13 | +const handleEvent = debounce( |
| 14 | + 1000, |
| 15 | + async (threadId: string, newThread: ThreadChannel) => { |
| 16 | + const initialThread = threadUpdateMap.get(threadId); |
| 17 | + if (!initialThread) return; |
| 18 | + |
| 19 | + // Remove from map |
| 20 | + threadUpdateMap.delete(threadId); |
| 21 | + |
| 22 | + // Handle tag additions |
| 23 | + const addedTags = newThread.appliedTags.filter( |
| 24 | + (t) => !initialThread.appliedTags.includes(t), |
| 25 | + ); |
| 26 | + if (addedTags.length > 0) { |
| 27 | + for (const tag of addedTags) { |
| 28 | + // If closed/opened tag is added, remove the opposite tag |
| 29 | + if ( |
| 30 | + tag === config.helpChannel.closedTag || |
| 31 | + tag === config.helpChannel.openedTag |
| 32 | + ) { |
| 33 | + const isClose = tag === config.helpChannel.closedTag; |
| 34 | + const { tagToRemove } = getTagsForCloseState(isClose); |
| 35 | + if (newThread.appliedTags.includes(tagToRemove)) { |
| 36 | + await newThread.setAppliedTags( |
| 37 | + newThread.appliedTags.filter((t) => t !== tagToRemove), |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Handle tag removals |
| 45 | + const removedTags = initialThread.appliedTags.filter( |
| 46 | + (t) => !newThread.appliedTags.includes(t), |
| 47 | + ); |
| 48 | + if (removedTags.length > 0) { |
| 49 | + for (const tag of removedTags) { |
| 50 | + // If closed or opened tag is removed, add it back only if its opposite isn't present |
| 51 | + if ( |
| 52 | + tag === config.helpChannel.closedTag || |
| 53 | + tag === config.helpChannel.openedTag |
| 54 | + ) { |
| 55 | + const isClose = tag === config.helpChannel.closedTag; |
| 56 | + const { tagToRemove } = getTagsForCloseState(isClose); |
| 57 | + if (!newThread.appliedTags.includes(tagToRemove)) { |
| 58 | + await newThread.setAppliedTags([...newThread.appliedTags, tag]); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | +); |
| 65 | + |
| 66 | +export default function registerEvents(client: Client) { |
| 67 | + client.on(Events.ThreadUpdate, async (oldThread, newThread) => { |
| 68 | + if (!(await isHelpPost(newThread))) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Store the initial state if this is the first update |
| 73 | + if (!threadUpdateMap.has(newThread.id)) { |
| 74 | + threadUpdateMap.set(newThread.id, oldThread); |
| 75 | + } |
| 76 | + |
| 77 | + // Trigger the debounced handler |
| 78 | + handleEvent(newThread.id, newThread); |
| 79 | + }); |
| 80 | +} |
0 commit comments