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' ;
29import { Bot } from '../bot' ;
310import { HelpThread } from '../entities/HelpThread' ;
411import {
512 helpForumChannel ,
13+ helpForumOpenTagName ,
14+ helpForumResolvedTagName ,
615 helpRequestsChannel ,
716 howToGetHelpChannel ,
817 howToGiveHelpChannel ,
@@ -12,6 +21,8 @@ import {
1221} from '../env' ;
1322import { sendWithMessageOwnership } from '../util/send' ;
1423
24+ const MAX_TAG_COUNT = 5 ;
25+
1526// Use a non-breaking space to force Discord to leave empty lines alone
1627const postGuidelines = ( here = true ) =>
1728 listify ( `
@@ -49,6 +60,13 @@ const howToGiveHelp = listify(`
4960- \`!ask\` — for if an asker only posts "can I get help?"
5061` ) ;
5162
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+
5270export async function helpForumModule ( bot : Bot ) {
5371 const channel = await bot . client . guilds . cache
5472 . first ( )
@@ -58,6 +76,8 @@ export async function helpForumModule(bot: Bot) {
5876 return ;
5977 }
6078 const forumChannel = channel ;
79+ const openTag = getTag ( forumChannel , helpForumOpenTagName ) ;
80+ const resolvedTag = getTag ( forumChannel , helpForumResolvedTagName ) ;
6181
6282 const helpRequestChannel = await bot . client . guilds . cache
6383 . first ( )
@@ -83,6 +103,8 @@ export async function helpForumModule(bot: Bot) {
83103 threadId : thread . id ,
84104 ownerId : owner . user . id ,
85105 } ) . save ( ) ;
106+
107+ await setStatus ( thread , openTag ) ;
86108 } ) ;
87109
88110 bot . client . on ( 'threadDelete' , async thread => {
@@ -108,7 +130,7 @@ export async function helpForumModule(bot: Bot) {
108130
109131 // Ensure the user has permission to ping helpers
110132 const isAsker = msg . author . id === threadData . ownerId ;
111- const isTrusted = bot . getTrustedMemberError ( msg ) === undefined ; // No error if trusted
133+ const isTrusted = bot . isTrusted ( msg ) ;
112134
113135 if ( ! isAsker && ! isTrusted ) {
114136 return sendWithMessageOwnership (
@@ -160,6 +182,50 @@ export async function helpForumModule(bot: Bot) {
160182 } ,
161183 } ) ;
162184
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+
163229 bot . registerAdminCommand ( {
164230 aliases : [ 'htgh' ] ,
165231 async listener ( msg ) {
@@ -205,6 +271,22 @@ export async function helpForumModule(bot: Bot) {
205271 channel . parent ?. id === forumChannel . id
206272 ) ;
207273 }
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+ }
208290}
209291
210292function listify ( text : string ) {
0 commit comments