1
- import { command , Module , listener } from 'cookiecord' ;
1
+ import CookiecordClient , {
2
+ command ,
3
+ Module ,
4
+ listener ,
5
+ CommonInhibitors ,
6
+ } from 'cookiecord' ;
2
7
import { ThreadAutoArchiveDuration } from 'discord-api-types' ;
3
8
import {
4
9
Message ,
@@ -7,6 +12,7 @@ import {
7
12
ThreadChannel ,
8
13
MessageEmbed ,
9
14
GuildMember ,
15
+ Client ,
10
16
} from 'discord.js' ;
11
17
import { HelpThread } from '../entities/HelpThread' ;
12
18
import {
@@ -136,7 +142,14 @@ See <#${howToGetHelpChannel}> for info on how to get better help.
136
142
// The rate limit for thread naming is 2 time / 10 mins, tracked per thread
137
143
const titleSetCooldown = 5 * 60 * 1000 ;
138
144
145
+ const threadExpireHours = ThreadAutoArchiveDuration . OneDay ;
146
+ const threadCheckInterval = 60 * 60 * 1000 ;
147
+
139
148
export class HelpThreadModule extends Module {
149
+ constructor ( client : CookiecordClient ) {
150
+ super ( client ) ;
151
+ }
152
+
140
153
@listener ( { event : 'messageCreate' } )
141
154
async onNewQuestion ( msg : Message ) {
142
155
if ( ! isHelpChannel ( msg . channel ) ) return ;
@@ -150,7 +163,7 @@ export class HelpThreadModule extends Module {
150
163
this . updateHelpInfo ( msg . channel ) ;
151
164
let thread = await msg . startThread ( {
152
165
name : `Help ${ msg . member ?. nickname ?? msg . author . username } ` ,
153
- autoArchiveDuration : ThreadAutoArchiveDuration . OneDay ,
166
+ autoArchiveDuration : threadExpireHours ,
154
167
} ) ;
155
168
thread . send ( helpThreadWelcomeMessage ( msg . member ! ) ) ;
156
169
await HelpThread . create ( {
@@ -186,6 +199,29 @@ export class HelpThreadModule extends Module {
186
199
}
187
200
}
188
201
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
+
189
225
@listener ( { event : 'threadUpdate' } )
190
226
async onThreadExpire ( thread : ThreadChannel ) {
191
227
if (
@@ -194,6 +230,10 @@ export class HelpThreadModule extends Module {
194
230
this . manuallyArchivedThreads . delete ( thread . id )
195
231
)
196
232
return ;
233
+ this . _onThreadExpire ( thread ) ;
234
+ }
235
+
236
+ private async _onThreadExpire ( thread : ThreadChannel ) {
197
237
const threadData = ( await HelpThread . findOne ( thread . id ) ) ! ;
198
238
console . log ( `Help thread expired:` , thread ) ;
199
239
await thread . send ( {
0 commit comments