1
- import { ChannelType , ThreadChannel , TextChannel , Channel } from 'discord.js' ;
1
+ import {
2
+ ChannelType ,
3
+ ThreadChannel ,
4
+ TextChannel ,
5
+ Channel ,
6
+ ForumChannel ,
7
+ Message ,
8
+ } from 'discord.js' ;
2
9
import { Bot } from '../bot' ;
3
10
import { HelpThread } from '../entities/HelpThread' ;
4
11
import {
5
12
helpForumChannel ,
13
+ helpForumOpenTagName ,
14
+ helpForumResolvedTagName ,
6
15
helpRequestsChannel ,
7
16
howToGetHelpChannel ,
8
17
howToGiveHelpChannel ,
@@ -12,6 +21,8 @@ import {
12
21
} from '../env' ;
13
22
import { sendWithMessageOwnership } from '../util/send' ;
14
23
24
+ const MAX_TAG_COUNT = 5 ;
25
+
15
26
// Use a non-breaking space to force Discord to leave empty lines alone
16
27
const postGuidelines = ( here = true ) =>
17
28
listify ( `
@@ -49,6 +60,13 @@ const howToGiveHelp = listify(`
49
60
- \`!ask\` — for if an asker only posts "can I get help?"
50
61
` ) ;
51
62
63
+ const helperResolve = ( owner : string , helper : string ) => `
64
+ <@${ owner } >
65
+ Because your issue seemed to be resolved, this post was marked as resolved by <@${ helper } >.
66
+ If your issue is not resolved, **you can reopen this post by running \`!reopen\`**.
67
+ *If you have a different question, make a new post in <#${ helpForumChannel } >.*
68
+ ` ;
69
+
52
70
export async function helpForumModule ( bot : Bot ) {
53
71
const channel = await bot . client . guilds . cache
54
72
. first ( )
@@ -58,6 +76,8 @@ export async function helpForumModule(bot: Bot) {
58
76
return ;
59
77
}
60
78
const forumChannel = channel ;
79
+ const openTag = getTag ( forumChannel , helpForumOpenTagName ) ;
80
+ const resolvedTag = getTag ( forumChannel , helpForumResolvedTagName ) ;
61
81
62
82
const helpRequestChannel = await bot . client . guilds . cache
63
83
. first ( )
@@ -83,6 +103,8 @@ export async function helpForumModule(bot: Bot) {
83
103
threadId : thread . id ,
84
104
ownerId : owner . user . id ,
85
105
} ) . save ( ) ;
106
+
107
+ await setStatus ( thread , openTag ) ;
86
108
} ) ;
87
109
88
110
bot . client . on ( 'threadDelete' , async thread => {
@@ -108,7 +130,7 @@ export async function helpForumModule(bot: Bot) {
108
130
109
131
// Ensure the user has permission to ping helpers
110
132
const isAsker = msg . author . id === threadData . ownerId ;
111
- const isTrusted = bot . getTrustedMemberError ( msg ) === undefined ; // No error if trusted
133
+ const isTrusted = bot . isTrusted ( msg ) ;
112
134
113
135
if ( ! isAsker && ! isTrusted ) {
114
136
return sendWithMessageOwnership (
@@ -160,6 +182,50 @@ export async function helpForumModule(bot: Bot) {
160
182
} ,
161
183
} ) ;
162
184
185
+ bot . registerCommand ( {
186
+ aliases : [ 'resolved' , 'resolve' , 'close' , 'closed' , 'done' ] ,
187
+ description : 'Help System: Mark a post as resolved' ,
188
+ async listener ( msg ) {
189
+ changeStatus ( msg , true ) ;
190
+ } ,
191
+ } ) ;
192
+
193
+ bot . registerCommand ( {
194
+ aliases : [ 'reopen' , 'open' , 'unresolved' , 'unresolve' ] ,
195
+ description : 'Help System: Reopen a resolved post' ,
196
+ async listener ( msg ) {
197
+ changeStatus ( msg , false ) ;
198
+ } ,
199
+ } ) ;
200
+
201
+ async function changeStatus ( msg : Message , resolved : boolean ) {
202
+ const thread = msg . channel ;
203
+ if ( thread ?. type !== ChannelType . PublicThread ) {
204
+ return sendWithMessageOwnership (
205
+ msg ,
206
+ ':warning: Can only be run in a help post' ,
207
+ ) ;
208
+ }
209
+
210
+ const threadData = await getHelpThread ( thread . id ) ;
211
+ const isAsker = msg . author . id === threadData . ownerId ;
212
+ const isTrusted = bot . isTrusted ( msg ) ;
213
+
214
+ if ( ! isAsker && ! isTrusted ) {
215
+ return sendWithMessageOwnership (
216
+ msg ,
217
+ ':warning: Only the asker can change the status of a help post' ,
218
+ ) ;
219
+ }
220
+
221
+ await setStatus ( thread , resolved ? resolvedTag : openTag ) ;
222
+ await msg . react ( '✅' ) ;
223
+
224
+ if ( resolved && ! isAsker ) {
225
+ await thread . send ( helperResolve ( thread . ownerId ! , msg . author . id ) ) ;
226
+ }
227
+ }
228
+
163
229
bot . registerAdminCommand ( {
164
230
aliases : [ 'htgh' ] ,
165
231
async listener ( msg ) {
@@ -205,6 +271,22 @@ export async function helpForumModule(bot: Bot) {
205
271
channel . parent ?. id === forumChannel . id
206
272
) ;
207
273
}
274
+
275
+ function getTag ( channel : ForumChannel , name : string ) {
276
+ const tag = channel . availableTags . find ( x => x . name === name ) ;
277
+ if ( ! tag ) throw new Error ( `Could not find tag ${ name } ` ) ;
278
+ return tag . id ;
279
+ }
280
+
281
+ async function setStatus ( thread : ThreadChannel , tag : string ) {
282
+ let tags = thread . appliedTags . filter (
283
+ x => x !== openTag && x !== resolvedTag ,
284
+ ) ;
285
+ if ( tags . length === MAX_TAG_COUNT ) {
286
+ tags = tags . slice ( 0 , - 1 ) ;
287
+ }
288
+ await thread . setAppliedTags ( [ tag , ...tags ] ) ;
289
+ }
208
290
}
209
291
210
292
function listify ( text : string ) {
0 commit comments