@@ -21,13 +21,87 @@ export type ModalField = {
2121 value ?: string ;
2222} ;
2323
24+ export class BotModalSubmitInteraction {
25+ public readonly guild : Discord . Guild | null ;
26+ public readonly guild_id : string | null ;
27+ public readonly channel : Discord . TextBasedChannel | null ;
28+ public readonly channel_id : string | null ;
29+ public readonly member : Discord . GuildMember | Discord . APIInteractionGuildMember | null ;
30+ public readonly user : Discord . User ;
31+ public readonly customId : string ;
32+ public readonly replied : boolean ;
33+ public readonly deferred : boolean ;
34+
35+ constructor (
36+ private readonly interaction : Discord . ModalSubmitInteraction ,
37+ private readonly field_configs : ModalField [ ] ,
38+ ) {
39+ this . guild = interaction . guild ;
40+ this . guild_id = interaction . guildId ;
41+ this . channel = interaction . channel ;
42+ this . channel_id = interaction . channelId ;
43+ this . member = interaction . member ;
44+ this . user = interaction . user ;
45+ this . customId = interaction . customId ;
46+ this . replied = interaction . replied ;
47+ this . deferred = interaction . deferred ;
48+ }
49+
50+ get_field_value ( field_custom_id : string ) : string {
51+ return this . interaction . fields . getTextInputValue ( field_custom_id ) ;
52+ }
53+
54+ get_all_field_values ( ) : Record < string , string > {
55+ const values : Record < string , string > = { } ;
56+ for ( const field of this . field_configs ) {
57+ values [ field . custom_id ] = this . get_field_value ( field . custom_id ) ;
58+ }
59+ return values ;
60+ }
61+
62+ async reply ( options : Discord . InteractionReplyOptions ) : Promise < Discord . InteractionResponse > {
63+ return await this . interaction . reply ( options ) ;
64+ }
65+
66+ async deferReply ( options ?: Discord . InteractionDeferReplyOptions ) : Promise < Discord . InteractionResponse > {
67+ return await this . interaction . deferReply ( options ) ;
68+ }
69+
70+ async editReply ( options : Discord . InteractionEditReplyOptions ) : Promise < Discord . Message > {
71+ return await this . interaction . editReply ( options ) ;
72+ }
73+
74+ async followUp ( options : Discord . InteractionReplyOptions ) : Promise < Discord . Message > {
75+ return await this . interaction . followUp ( options ) ;
76+ }
77+
78+ async deferUpdate ( options ?: Discord . InteractionDeferUpdateOptions ) : Promise < Discord . InteractionResponse > {
79+ return await this . interaction . deferUpdate ( options ) ;
80+ }
81+
82+ async update ( options : Discord . InteractionUpdateOptions ) : Promise < Discord . InteractionResponse > {
83+ if ( ! this . interaction . isFromMessage ( ) ) {
84+ throw new Error ( "update() can only be called on modal interactions from messages" ) ;
85+ }
86+ return await this . interaction . update ( options ) ;
87+ }
88+
89+ isFromMessage ( ) : this is BotModalSubmitInteraction & { message : Discord . Message } {
90+ return this . interaction . isFromMessage ( ) ;
91+ }
92+
93+ get raw_interaction ( ) : Discord . ModalSubmitInteraction {
94+ return this . interaction ;
95+ }
96+ }
97+
2498export class BotModalHandler < Args extends unknown [ ] = [ ] > {
2599 private readonly metadata_fields : ModalParameterType [ ] = [ ] ;
26100 private readonly field_configs : ModalField [ ] = [ ] ;
27101
28102 constructor (
29103 public readonly base_custom_id : string ,
30- public readonly handler : ( interaction : Discord . ModalSubmitInteraction , ...args : Args ) => Promise < void > ,
104+ public readonly handler : ( interaction : BotModalSubmitInteraction , ...args : Args ) => Promise < void > ,
31105 public readonly permissions ?: bigint ,
32106 metadata_fields : ModalParameterType [ ] = [ ] ,
33107 field_configs : ModalField [ ] = [ ] ,
@@ -79,7 +153,8 @@ export class BotModalHandler<Args extends unknown[] = []> {
79153
80154 try {
81155 const parsed_args = this . parse_arguments ( raw_args ) ;
82- await this . handler ( interaction , ...parsed_args ) ;
156+ const bot_interaction = new BotModalSubmitInteraction ( interaction , this . field_configs ) ;
157+ await this . handler ( bot_interaction , ...parsed_args ) ;
83158 } catch ( error ) {
84159 M . error ( `Error handling modal ${ this . base_custom_id } :` , error ) ;
85160
@@ -209,7 +284,7 @@ export class BotModal<Args extends unknown[] = []> {
209284export class ModalInteractionBuilder <
210285 Args extends unknown [ ] = [ ] ,
211286 HasHandler extends boolean = false ,
212- > extends BaseBuilder < HasHandler , [ Discord . ModalSubmitInteraction , ...Args ] > {
287+ > extends BaseBuilder < HasHandler , [ BotModalSubmitInteraction , ...Args ] > {
213288 private readonly metadata_fields : ModalParameterType [ ] = [ ] ;
214289 private permissions ?: bigint ;
215290 private title = "Modal" ;
@@ -276,7 +351,7 @@ export class ModalInteractionBuilder<
276351 }
277352
278353 set_handler (
279- handler : ( interaction : Discord . ModalSubmitInteraction , ...args : Args ) => Promise < void > ,
354+ handler : ( interaction : BotModalSubmitInteraction , ...args : Args ) => Promise < void > ,
280355 ) : ModalInteractionBuilder < Args , true > {
281356 this . handler = handler ;
282357 return this as unknown as ModalInteractionBuilder < Args , true > ;
0 commit comments