Skip to content

Commit fea2da6

Browse files
committed
react to closed theads with a checkmark
replaces the [Open]/[Closed]
1 parent 0590efa commit fea2da6

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/entities/HelpThread.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ export class HelpThread extends BaseEntity {
1515
// When the title was last set
1616
@Column({ nullable: true })
1717
titleSetTimestamp?: string;
18+
19+
// The id of the original message; nullable for backwards compat
20+
@Column({ nullable: true })
21+
origMessageId?: string;
1822
}

src/modules/helpthread.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ If you're not sure how, have a look through [StackOverflow's guide on asking a g
3232
// A zero-width space (necessary to prevent discord from trimming the leading whitespace), followed by a three non-breaking spaces.
3333
const indent = '\u200b\u00a0\u00a0\u00a0';
3434

35+
const closedEmoji = '☑️';
36+
3537
const helpInfo = (channel: TextChannel) =>
3638
new MessageEmbed()
3739
.setColor(GREEN)
@@ -92,19 +94,38 @@ export class HelpThreadModule extends Module {
9294
if (msg.author.id === this.client.user!.id) return;
9395
this.updateHelpInfo(msg.channel);
9496
let thread = await msg.startThread({
95-
name: `[Open] Help ${msg.member?.nickname ?? msg.author.username}`,
97+
name: `Help ${msg.member?.nickname ?? msg.author.username}`,
9698
autoArchiveDuration: ThreadAutoArchiveDuration.OneDay,
9799
});
98100
thread.send(helpThreadWelcomeMessage(msg.member!));
99101
await HelpThread.create({
100102
threadId: thread.id,
101103
ownerId: msg.author.id,
104+
origMessageId: msg.id,
102105
}).save();
103106
}
104107

105108
// Used to differentiate automatic archive from bot archive
106109
manuallyArchivedThreads = new Set<string>();
107110

111+
@listener({ event: 'threadUpdate' })
112+
async onThreadReopen(thread: ThreadChannel, ...a: any[]) {
113+
if (
114+
!isHelpThread(thread) ||
115+
!thread.archived ||
116+
((await thread.fetch()) as ThreadChannel).archived
117+
)
118+
return;
119+
const threadData = (await HelpThread.findOne(thread.id))!;
120+
if (!threadData.origMessageId) return;
121+
const origMessage = await thread.parent.messages.fetch(
122+
threadData.origMessageId,
123+
);
124+
origMessage.reactions
125+
.resolve(closedEmoji)
126+
?.users.remove(this.client.user!.id);
127+
}
128+
108129
@listener({ event: 'threadUpdate' })
109130
async onThreadExpire(thread: ThreadChannel) {
110131
if (
@@ -115,8 +136,7 @@ export class HelpThreadModule extends Module {
115136
return;
116137
await thread.send({ embeds: [threadExpireEmbed] });
117138
this.manuallyArchivedThreads.add(thread.id);
118-
await thread.setName(`[Closed] ${thread.name.replace(/\[.+?] /, '')}`);
119-
await thread.setArchived(true);
139+
await this.archiveThread(thread);
120140
}
121141

122142
@command({
@@ -139,10 +159,7 @@ export class HelpThreadModule extends Module {
139159
) {
140160
await msg.react('✅');
141161
this.manuallyArchivedThreads.add(thread.id);
142-
await thread.setName(
143-
`[Closed] ${thread.name.replace(/\[.+?] /, '')}`,
144-
);
145-
await thread.setArchived(true);
162+
await this.archiveThread(thread);
146163
} else {
147164
return await sendWithMessageOwnership(
148165
msg,
@@ -151,6 +168,16 @@ export class HelpThreadModule extends Module {
151168
}
152169
}
153170

171+
private async archiveThread(thread: ThreadChannel) {
172+
await thread.setArchived(true);
173+
const threadData = (await HelpThread.findOne(thread.id))!;
174+
if (!threadData.origMessageId) return;
175+
const origMessage = await thread.parent!.messages.fetch(
176+
threadData.origMessageId,
177+
);
178+
await origMessage.react(closedEmoji);
179+
}
180+
154181
private helpInfoLocks = new Map<string, Promise<void>>();
155182
private updateHelpInfo(channel: TextChannel) {
156183
this.helpInfoLocks.set(
@@ -263,7 +290,7 @@ export class HelpThreadModule extends Module {
263290
HelpThread.update(thread.id, {
264291
titleSetTimestamp: Date.now() + '',
265292
}),
266-
msg.channel.setName(`[Open] ${username} - ${title}`),
293+
msg.channel.setName(`${username} - ${title}`),
267294
]);
268295
}
269296

0 commit comments

Comments
 (0)