11import {
2- ButtonInteraction ,
3- Collection ,
4- CommandInteraction ,
5- ContextMenuInteraction ,
62 GuildApplicationCommandPermissionData ,
73 Interaction ,
84} from "discord.js" ;
9- import path from "path" ;
105import Bot from "../../api/bot" ;
116import Manager from "../../api/manager" ;
127import BaseInteraction from "../../api/interaction/interaction" ;
@@ -15,28 +10,32 @@ import { Routes } from "discord-api-types/v9";
1510import SlashCommand from "../../api/interaction/slashcommand" ;
1611import CustomButtonInteraction from "../../api/interaction/button" ;
1712import ContextMenuCommand from "../../api/interaction/contextmenucommand" ;
18- import { ApplicationCommandType } from "discord- api-types " ;
13+ import CustomModalInteraction from "../../ api/interaction/modal " ;
1914
2015export default class InteractionManager extends Manager {
2116 // private readonly interactionPath = process.cwd() + "/dist/interaction/";
2217 private slashCommandPath : string ;
2318 private cmCommandPath : string ;
2419 private buttonPath : string ;
20+ private modalPath : string ;
2521
2622 private slashCommands : Map < string , SlashCommand > = new Map ( ) ;
2723 private cmCommands : Map < string , ContextMenuCommand > = new Map ( ) ;
2824 private buttons : Map < string , CustomButtonInteraction > = new Map ( ) ;
25+ private modals : Map < string , CustomModalInteraction > = new Map ( ) ;
2926
3027 constructor (
3128 bot : Bot ,
3229 slashCommandPath : string ,
3330 cmCommandPath : string ,
34- buttonPath : string
31+ buttonPath : string ,
32+ modalPath : string ,
3533 ) {
3634 super ( bot ) ;
3735 this . slashCommandPath = slashCommandPath ;
3836 this . cmCommandPath = cmCommandPath ;
3937 this . buttonPath = buttonPath ;
38+ this . modalPath = modalPath ;
4039 }
4140
4241 /**
@@ -45,7 +44,7 @@ export default class InteractionManager extends Manager {
4544 public init ( ) {
4645 // this.loadInteractionHandlers();
4746 this . registerSlashAndContextMenuCommands ( ) ;
48- this . registerButtons ( ) ;
47+ this . registerButtonsAndModals ( ) ;
4948 }
5049
5150 /**
@@ -65,6 +64,10 @@ export default class InteractionManager extends Manager {
6564 handler = [ ...this . buttons . values ( ) ] . find ( ( x ) =>
6665 x . matchCustomId ( interaction . customId )
6766 ) as BaseInteraction ;
67+ } else if ( interaction . isModalSubmit ( ) ) {
68+ handler = [ ...this . modals . values ( ) ] . find ( ( x ) =>
69+ x . matchCustomId ( interaction . customId )
70+ ) as BaseInteraction ;
6871 } else return ;
6972
7073 // Return if not found
@@ -74,9 +77,10 @@ export default class InteractionManager extends Manager {
7477 try {
7578 await handler . handleInteraction ( { bot : this . bot , interaction } ) ;
7679 } catch ( e : any ) {
77- await interaction . reply (
78- "Command execution failed. Please contact a bot maintainer..."
79- ) ;
80+ await interaction . reply ( {
81+ content : "Command execution failed. Please contact a bot maintainer..." ,
82+ ephemeral : true
83+ } ) ;
8084 // Don't throw and let the bot handle this as an unhandled rejection. Instead,
8185 // take initiative to handle it as an error so we can see the trace.
8286 await this . bot . managers . error . handleErr ( e ) ;
@@ -161,16 +165,22 @@ export default class InteractionManager extends Manager {
161165 }
162166 }
163167
164- private async registerButtons ( ) {
168+ private async registerButtonsAndModals ( ) {
165169 try {
166170 // Dynamically load source files
167171 this . buttons = new Map (
168172 DynamicLoader . loadClasses ( this . buttonPath ) . map ( ( sc ) => [ sc . name , sc ] )
169173 ) ;
170-
171174 for ( const btn of this . buttons . keys ( ) ) {
172175 this . bot . logger . info ( `Loaded button '${ btn } '` ) ;
173176 }
177+
178+ this . modals = new Map (
179+ DynamicLoader . loadClasses ( this . modalPath ) . map ( ( sc ) => [ sc . name , sc ] )
180+ ) ;
181+ for ( const mdl of this . modals . keys ( ) ) {
182+ this . bot . logger . info ( `Loaded modal '${ mdl } '` ) ;
183+ }
174184 } catch ( error : any ) {
175185 await this . bot . managers . error . handleErr ( error ) ;
176186 }
0 commit comments