Skip to content

Commit 7245fe8

Browse files
authored
Merge pull request #184 from tjjfvi/checkmark-closed
React to closed theads with a checkmark
2 parents 6a2a8e6 + 1894969 commit 7245fe8

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)
@@ -98,20 +100,39 @@ export class HelpThreadModule extends Module {
98100
);
99101
this.updateHelpInfo(msg.channel);
100102
let thread = await msg.startThread({
101-
name: `[Open] Help ${msg.member?.nickname ?? msg.author.username}`,
103+
name: `Help ${msg.member?.nickname ?? msg.author.username}`,
102104
autoArchiveDuration: ThreadAutoArchiveDuration.OneDay,
103105
});
104106
thread.send(helpThreadWelcomeMessage(msg.member!));
105107
await HelpThread.create({
106108
threadId: thread.id,
107109
ownerId: msg.author.id,
110+
origMessageId: msg.id,
108111
}).save();
109112
console.log(`Created a new help thread for`, msg.author);
110113
}
111114

112115
// Used to differentiate automatic archive from bot archive
113116
manuallyArchivedThreads = new Set<string>();
114117

118+
@listener({ event: 'threadUpdate' })
119+
async onThreadReopen(thread: ThreadChannel, ...a: any[]) {
120+
if (
121+
!isHelpThread(thread) ||
122+
!thread.archived ||
123+
((await thread.fetch()) as ThreadChannel).archived
124+
)
125+
return;
126+
const threadData = (await HelpThread.findOne(thread.id))!;
127+
if (!threadData.origMessageId) return;
128+
const origMessage = await thread.parent.messages.fetch(
129+
threadData.origMessageId,
130+
);
131+
origMessage.reactions
132+
.resolve(closedEmoji)
133+
?.users.remove(this.client.user!.id);
134+
}
135+
115136
@listener({ event: 'threadUpdate' })
116137
async onThreadExpire(thread: ThreadChannel) {
117138
if (
@@ -123,8 +144,7 @@ export class HelpThreadModule extends Module {
123144
console.log(`Help thread expired:`, thread);
124145
await thread.send({ embeds: [threadExpireEmbed] });
125146
this.manuallyArchivedThreads.add(thread.id);
126-
await thread.setName(`[Closed] ${thread.name.replace(/\[.+?] /, '')}`);
127-
await thread.setArchived(true);
147+
await this.archiveThread(thread);
128148
}
129149

130150
@command({
@@ -148,10 +168,7 @@ export class HelpThreadModule extends Module {
148168
console.log(`Closing help thread:`, thread);
149169
await msg.react('✅');
150170
this.manuallyArchivedThreads.add(thread.id);
151-
await thread.setName(
152-
`[Closed] ${thread.name.replace(/\[.+?] /, '')}`,
153-
);
154-
await thread.setArchived(true);
171+
await this.archiveThread(thread);
155172
} else {
156173
return await sendWithMessageOwnership(
157174
msg,
@@ -160,6 +177,16 @@ export class HelpThreadModule extends Module {
160177
}
161178
}
162179

180+
private async archiveThread(thread: ThreadChannel) {
181+
await thread.setArchived(true);
182+
const threadData = (await HelpThread.findOne(thread.id))!;
183+
if (!threadData.origMessageId) return;
184+
const origMessage = await thread.parent!.messages.fetch(
185+
threadData.origMessageId,
186+
);
187+
await origMessage.react(closedEmoji);
188+
}
189+
163190
private helpInfoLocks = new Map<string, Promise<void>>();
164191
private updateHelpInfo(channel: TextChannel) {
165192
this.helpInfoLocks.set(
@@ -272,7 +299,7 @@ export class HelpThreadModule extends Module {
272299
HelpThread.update(thread.id, {
273300
titleSetTimestamp: Date.now() + '',
274301
}),
275-
msg.channel.setName(`[Open] ${username} - ${title}`),
302+
msg.channel.setName(`${username} - ${title}`),
276303
]);
277304
}
278305

0 commit comments

Comments
 (0)