Skip to content

Commit c332b60

Browse files
authored
Merge pull request #217 from tjjfvi/expire-threads
2 parents 468b24c + ef3ee9b commit c332b60

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/modules/helpthread.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { command, Module, listener } from 'cookiecord';
1+
import CookiecordClient, {
2+
command,
3+
Module,
4+
listener,
5+
CommonInhibitors,
6+
} from 'cookiecord';
27
import { ThreadAutoArchiveDuration } from 'discord-api-types';
38
import {
49
Message,
@@ -7,6 +12,7 @@ import {
712
ThreadChannel,
813
MessageEmbed,
914
GuildMember,
15+
Client,
1016
} from 'discord.js';
1117
import { HelpThread } from '../entities/HelpThread';
1218
import {
@@ -136,7 +142,14 @@ See <#${howToGetHelpChannel}> for info on how to get better help.
136142
// The rate limit for thread naming is 2 time / 10 mins, tracked per thread
137143
const titleSetCooldown = 5 * 60 * 1000;
138144

145+
const threadExpireHours = ThreadAutoArchiveDuration.OneDay;
146+
const threadCheckInterval = 60 * 60 * 1000;
147+
139148
export class HelpThreadModule extends Module {
149+
constructor(client: CookiecordClient) {
150+
super(client);
151+
}
152+
140153
@listener({ event: 'messageCreate' })
141154
async onNewQuestion(msg: Message) {
142155
if (!isHelpChannel(msg.channel)) return;
@@ -150,7 +163,7 @@ export class HelpThreadModule extends Module {
150163
this.updateHelpInfo(msg.channel);
151164
let thread = await msg.startThread({
152165
name: `Help ${msg.member?.nickname ?? msg.author.username}`,
153-
autoArchiveDuration: ThreadAutoArchiveDuration.OneDay,
166+
autoArchiveDuration: threadExpireHours,
154167
});
155168
thread.send(helpThreadWelcomeMessage(msg.member!));
156169
await HelpThread.create({
@@ -186,6 +199,29 @@ export class HelpThreadModule extends Module {
186199
}
187200
}
188201

202+
@listener({ event: 'ready' })
203+
@command({
204+
inhibitors: [CommonInhibitors.hasGuildPermission('MANAGE_MESSAGES')],
205+
})
206+
checkThreads() {
207+
setTimeout(() => this.checkThreads(), threadCheckInterval);
208+
this.client.guilds.cache.forEach(guild => {
209+
guild.channels.cache.forEach(async channel => {
210+
if (!isHelpChannel(channel)) return;
211+
const threads = await channel.threads.fetchActive();
212+
threads.threads.forEach(async thread => {
213+
const time =
214+
Date.now() -
215+
(await thread.messages.fetch({ limit: 1 })).first()!
216+
.createdTimestamp;
217+
if (time >= threadExpireHours * 60 * 1000) {
218+
this._onThreadExpire(thread);
219+
}
220+
});
221+
});
222+
});
223+
}
224+
189225
@listener({ event: 'threadUpdate' })
190226
async onThreadExpire(thread: ThreadChannel) {
191227
if (
@@ -194,6 +230,10 @@ export class HelpThreadModule extends Module {
194230
this.manuallyArchivedThreads.delete(thread.id)
195231
)
196232
return;
233+
this._onThreadExpire(thread);
234+
}
235+
236+
private async _onThreadExpire(thread: ThreadChannel) {
197237
const threadData = (await HelpThread.findOne(thread.id))!;
198238
console.log(`Help thread expired:`, thread);
199239
await thread.send({

0 commit comments

Comments
 (0)