1- import { CommandInteraction , Message } from "discord.js"
1+ import { APIInteractionDataResolvedGuildMember } from "discord-api-types/v10"
2+ import { CommandInteraction , Message , MessageMentions } from "discord.js"
23
34import config from "../../data/config.json"
45import client from "../../main"
@@ -38,7 +39,7 @@ export default class Follow extends Command {
3839 super ( {
3940 name,
4041 category : "News" ,
41- usage : `follow <list|add|remove> <${ Object . keys ( descriptions ) . join ( "|" ) } >\` or just \`follow list` ,
42+ usage : `follow <list|add|remove> <${ Object . keys ( descriptions ) . join ( "|" ) } > [pingRole, in the case of add] \` or just \`follow list` ,
4243 help : `Follow certain events in a channel
4344
4445**Possible events**:
@@ -77,6 +78,10 @@ Example of adding news: \`${config.prefix}follow add news\``,
7778 value : d
7879 }
7980 } )
81+ } , {
82+ name : "pingrole" ,
83+ description : "Role to ping" ,
84+ type : "MENTIONABLE"
8085 } ]
8186 } , {
8287 name : "remove" ,
@@ -115,7 +120,7 @@ Example of adding news: \`${config.prefix}follow add news\``,
115120 if ( sub == "list" ) {
116121 return this . runList ( source , options . getString ( "category" ) as ( FollowCategory | null ) )
117122 } else if ( sub == "add" ) {
118- return this . runFollow ( source , options . getString ( "category" , true ) as FollowCategory )
123+ return this . runFollow ( source , options . getString ( "category" , true ) as FollowCategory , options . getMentionable ( "pingrole" , false ) )
119124 } else if ( sub == "remove" ) {
120125 return this . runUnfollow ( source , options . getString ( "category" , true ) as FollowCategory )
121126 }
@@ -132,6 +137,7 @@ Example of adding news: \`${config.prefix}follow add news\``,
132137 const sub = args [ 0 ] ?. toLowerCase ( ) ?? "help"
133138 args . shift ( )
134139 const otherArgs = args [ 0 ] ?. toLowerCase ( )
140+ args . shift ( )
135141
136142 const category : FollowCategory | undefined = otherArgs ? Object . keys ( descriptions ) . find ( r => r . toLowerCase ( ) == otherArgs ) as ( FollowCategory | undefined ) : undefined
137143 if ( ! category )
@@ -145,7 +151,7 @@ Example of adding news: \`${config.prefix}follow add news\``,
145151 if ( [ "list" , "l" ] . includes ( sub ) ) {
146152 return this . runList ( source , category )
147153 } else if ( [ "add" , "a" , "follow" , "enable" , "on" ] . includes ( sub ) ) {
148- return this . runFollow ( source , category )
154+ return this . runFollow ( source , category , args [ 0 ] )
149155 } else if ( [ "remove" , "delete" , "d" , "r" , "disable" , "off" , "unfollow" ] . includes ( sub ) ) {
150156 return this . runUnfollow ( source , category )
151157 } else {
@@ -162,14 +168,15 @@ Example of adding news: \`${config.prefix}follow add news\``,
162168 if ( ! category ) {
163169 const following = followManager . following ( source . guild )
164170
165- const channels : { category : string , channelname : string } [ ] = [ ]
171+ const channels : { category : string , channelname : string , pingRole : string } [ ] = [ ]
166172 for ( const follow of following )
167173 try {
168174 const channel = await client . channels . fetch ( follow . channelID )
169175 if ( isNewsable ( channel ) )
170176 channels . push ( {
171177 channelname : channel . name ,
172- category : follow . category
178+ category : follow . category ,
179+ pingRole : follow . pingRole
173180 } )
174181 } catch ( error ) {
175182 followManager . dropChannel ( follow . channelID )
@@ -178,9 +185,9 @@ Example of adding news: \`${config.prefix}follow add news\``,
178185
179186 return sendMessage ( source , `Following per event: \`\`\`
180187${ createTable (
181- [ "Event" , "|" , "Channel" ] ,
188+ [ "Event" , "|" , "Channel" , "|" , "Ping role ID" ] ,
182189 channels . map (
183- k => [ k . category , "|" , k . channelname ]
190+ k => [ k . category , "|" , k . channelname , "|" , k . pingRole ]
184191 ) ) } \`\`\``, undefined , true )
185192 }
186193
@@ -209,15 +216,33 @@ ${createTable(
209216
210217 return sendMessage ( source , `Unfollowed ${ category } in <#${ channel . id } >` , undefined , true )
211218 }
212- async runFollow ( source : CommandSource , category : FollowCategory ) : Promise < SendMessage | undefined > {
219+ async runFollow ( source : CommandSource , category : FollowCategory , pingRole : string | { id : string } | APIInteractionDataResolvedGuildMember | null ) : Promise < SendMessage | undefined > {
213220 const channel = await source . channel ?. fetch ( )
214221 if ( ! channel || ! isNewsable ( channel ) || source . guild == null )
215222 return sendMessage ( source , "Unable to follow in this channel" , undefined , true )
216223
224+ let pingedRole = ""
225+ if ( typeof pingRole == "string" ) {
226+ if ( pingRole . match ( / ^ \d + $ / ) )
227+ pingedRole = pingRole
228+ else {
229+ const response = MessageMentions . ROLES_PATTERN . exec ( pingRole )
230+ if ( response == null )
231+ return sendMessage ( source , "Unable to extract ping role from message" , undefined , true )
232+ pingedRole = response [ 1 ]
233+ }
234+ } else if ( ( pingRole as { id : string } ) ?. id ) {
235+ const role = await source . guild . roles . fetch ( ( pingRole as { id : string } ) . id )
236+ if ( ! role )
237+ return sendMessage ( source , "Unable to extract ping role from command" , undefined , true )
238+ pingedRole = role . id
239+ } else if ( pingRole )
240+ return sendMessage ( source , "Unable to extract ping role from command" , undefined , true )
241+
217242 const { followManager } = client
218243
219- followManager . addFollow ( source . guild , channel , category , getUserID ( source ) )
244+ followManager . addFollow ( source . guild , channel , category , getUserID ( source ) , pingedRole )
220245
221- return sendMessage ( source , `Now following ${ category } in <#${ channel . id } >` , undefined , true )
246+ return sendMessage ( source , `Now following ${ category } in <#${ channel . id } > (Ping role: ${ pingedRole == "" ? "none" : `<@& ${ pingedRole } > - make sure the bot can ping this role - this might require the @everyone permission!` } ) ` , undefined , true )
222247 }
223248}
0 commit comments