File tree Expand file tree Collapse file tree 4 files changed +43
-20
lines changed Expand file tree Collapse file tree 4 files changed +43
-20
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ import type {
99 DefineContextMenuWithOptions ,
1010 DefineEvent ,
1111 DefineEventWithOptions ,
12- DefineModal ,
1312 DefinePrecondition ,
1413 DefinePreconditionWithName ,
1514 DefineSelectMenu ,
@@ -19,6 +18,10 @@ import type {
1918 HarmonixConfig ,
2019 HarmonixContextMenu ,
2120 HarmonixEvent ,
21+ HarmonixModal ,
22+ ModalCallback ,
23+ ModalConfig ,
24+ ModalInputs ,
2225 OptionsDef ,
2326 PreconditionCallback
2427} from './types'
@@ -96,7 +99,10 @@ export const defineButton: DefineButton = (config, callback) => {
9699 return { config, callback }
97100}
98101
99- export const defineModal : DefineModal = ( config , callback ) => {
102+ export const defineModal = < T extends ModalInputs = ModalInputs > (
103+ config : ModalConfig & { inputs ?: T } ,
104+ callback : ModalCallback < T >
105+ ) : HarmonixModal < T > => {
100106 return { config, callback }
101107}
102108
Original file line number Diff line number Diff line change @@ -57,10 +57,11 @@ export const getModal = (id: string) => {
5757 . setCustomId ( modal . config . id ! )
5858 . setTitle ( modal . config . title )
5959
60- if ( modal . config . textInputs ) {
61- for ( const input of modal . config . textInputs ) {
60+ if ( modal . config . inputs ) {
61+ for ( const id in modal . config . inputs ) {
62+ const input = modal . config . inputs [ id ]
6263 const inputBuilder = new TextInputBuilder ( )
63- . setCustomId ( input . id )
64+ . setCustomId ( id )
6465 . setLabel ( input . label )
6566 . setStyle (
6667 input . style ? TextInputStyle [ input . style ] : TextInputStyle . Short
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import consola from 'consola'
33import { colors } from 'consola/utils'
44import { toOption , resolveOption } from './utils'
55import { ctx } from './harmonix'
6- import type { Harmonix , ParsedOptions } from './types'
6+ import { ParsedInputs , type Harmonix , type ParsedOptions } from './types'
77
88export const registerEvents = ( harmonix : Harmonix ) => {
99 for ( const [ , event ] of harmonix . events . filter ( ( evt ) => ! evt . options . type ) ) {
@@ -117,14 +117,22 @@ export const registerButtons = (harmonix: Harmonix) => {
117117}
118118
119119export const registerModals = ( harmonix : Harmonix ) => {
120- harmonix . client ?. on ( Events . InteractionCreate , ( interaction ) => {
120+ harmonix . client ?. on ( Events . InteractionCreate , async ( interaction ) => {
121121 if ( ! interaction . isModalSubmit ( ) ) return
122122 const mdl = harmonix . modals . find (
123123 ( mdl ) => mdl . config . id === interaction . customId
124124 )
125125
126126 if ( ! mdl ) return
127- mdl . callback ( interaction )
127+ const inputs = Object . keys ( mdl . config . inputs ?? { } ) . reduce < ParsedInputs > (
128+ ( acc , input ) => ( {
129+ ...acc ,
130+ [ input ] : interaction . fields . getTextInputValue ( input )
131+ } ) ,
132+ { }
133+ )
134+
135+ mdl . callback ( interaction , { inputs } )
128136 } )
129137}
130138
Original file line number Diff line number Diff line change 11import type { ModalSubmitInteraction , TextInputStyle } from 'discord.js'
22
3- type ModalCallback = ( interaction : ModalSubmitInteraction ) => void
4-
53interface TextInput {
6- id : string
74 label : string
85 style : keyof typeof TextInputStyle
96 maxLength ?: number
@@ -13,20 +10,31 @@ interface TextInput {
1310 required ?: boolean
1411}
1512
16- export interface ModalConfig {
13+ export type ModalInputs = Record < string , TextInput >
14+
15+ export interface ModalConfig < T extends ModalInputs = ModalInputs > {
1716 id ?: string
1817 title : string
19- textInputs ?: TextInput [ ]
18+ inputs ?: T
19+ }
20+
21+ export type ParsedInputs < T extends ModalInputs = ModalInputs > = Record <
22+ { [ K in keyof T ] : K } [ keyof T ] ,
23+ string
24+ >
25+
26+ interface ModalContext < T extends ModalInputs = ModalInputs > {
27+ inputs : ParsedInputs < T >
2028}
2129
22- export type DefineModal = (
23- config : ModalConfig ,
24- callback : ModalCallback
25- ) => HarmonixModal
30+ export type ModalCallback < T extends ModalInputs = ModalInputs > = (
31+ interaction : ModalSubmitInteraction ,
32+ context : ModalContext < T >
33+ ) => void
2634
2735export type HarmonixModalInput = string | HarmonixModal
2836
29- export interface HarmonixModal {
30- config : ModalConfig
31- callback : ModalCallback
37+ export interface HarmonixModal < T extends ModalInputs = ModalInputs > {
38+ config : ModalConfig < T >
39+ callback : ModalCallback < T >
3240}
You can’t perform that action at this time.
0 commit comments