11import { GuildMember } from "discord.js" ;
22import { guildPluginEventListener } from "knub" ;
33import { SECONDS } from "../../../utils.js" ;
4+ import { renderRecursively } from "../../../utils.js" ;
45import { parseCustomId } from "../../../utils/parseCustomId.js" ;
56import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin.js" ;
67import { getAllRolesInButtons } from "../functions/getAllRolesInButtons.js" ;
78import { RoleButtonsPluginType , TRoleButtonOption } from "../types.js" ;
9+ import { renderTemplate , TemplateSafeValueContainer } from "../../../templateFormatter.js" ;
10+ import { memberToTemplateSafeMember , roleToTemplateSafeRole , userToTemplateSafeUser } from "../../../utils/templateSafeObjects.js" ;
811
912const ROLE_BUTTON_CD = 5 * SECONDS ;
1013
@@ -31,7 +34,6 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
3134 ephemeral : true ,
3235 content : "Invalid option selected" ,
3336 } )
34- // tslint:disable-next-line no-console
3537 . catch ( ( err ) => console . trace ( err . message ) ) ;
3638 return ;
3739 }
@@ -53,14 +55,25 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
5355 const rolesToRemove : string [ ] = [ ] ;
5456 const rolesToAdd : string [ ] = [ ] ;
5557
58+ const renderTemplateText = async ( str : string ) =>
59+ renderTemplate (
60+ str ,
61+ new TemplateSafeValueContainer ( {
62+ user : member ? memberToTemplateSafeMember ( member ) : userToTemplateSafeUser ( args . interaction . user ) ,
63+ role : role ? roleToTemplateSafeRole ( role ) : new TemplateSafeValueContainer ( { name : roleName , id : option . role_id } ) ,
64+ } ) ,
65+ ) ;
66+
5667 if ( member . roles . cache . has ( option . role_id ) ) {
5768 rolesToRemove . push ( option . role_id ) ;
69+
70+ const messageTemplate = config . buttons [ name ] . remove_message || `The role **${ roleName } ** will be removed shortly!` ;
71+ const formatted = typeof messageTemplate === "string"
72+ ? await renderTemplateText ( messageTemplate )
73+ : await renderRecursively ( messageTemplate , renderTemplateText ) ;
74+
5875 args . interaction
59- . reply ( {
60- ephemeral : true ,
61- content : `The role **${ roleName } ** will be removed shortly!` ,
62- } )
63- // tslint:disable-next-line no-console
76+ . reply ( { ephemeral : true , ...( typeof formatted === "string" ? { content : formatted } : formatted ) } )
6477 . catch ( ( err ) => console . trace ( err . message ) ) ;
6578 } else {
6679 rolesToAdd . push ( option . role_id ) ;
@@ -73,12 +86,13 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
7386 }
7487 }
7588
89+ const messageTemplate = config . buttons [ name ] . add_message || `You will receive the **${ roleName } ** role shortly!` ;
90+ const formatted = typeof messageTemplate === "string"
91+ ? await renderTemplateText ( messageTemplate )
92+ : await renderRecursively ( messageTemplate , renderTemplateText ) ;
93+
7694 args . interaction
77- . reply ( {
78- ephemeral : true ,
79- content : `You will receive the **${ roleName } ** role shortly!` ,
80- } )
81- // tslint:disable-next-line no-console
95+ . reply ( { ephemeral : true , ...( typeof formatted === "string" ? { content : formatted } : formatted ) } )
8296 . catch ( ( err ) => console . trace ( err . message ) ) ;
8397 }
8498
@@ -89,4 +103,4 @@ export const onButtonInteraction = guildPluginEventListener<RoleButtonsPluginTyp
89103 pluginData . getPlugin ( RoleManagerPlugin ) . removeRole ( member . user . id , roleId ) ;
90104 }
91105 } ,
92- } ) ;
106+ } ) ;
0 commit comments