11package net .javadiscord .javabot ;
22
33import java .time .ZoneOffset ;
4- import java .util .Arrays ;
4+ import java .util .ArrayList ;
55import java .util .EnumSet ;
6- import java .util .HashMap ;
76import java .util .List ;
87import java .util .Map ;
98import java .util .TimeZone ;
109
1110import javax .annotation .PostConstruct ;
1211
12+ import org .jetbrains .annotations .NotNull ;
1313import org .springframework .boot .SpringApplication ;
1414import org .springframework .boot .autoconfigure .SpringBootApplication ;
1515import org .springframework .context .ApplicationContext ;
1616import org .springframework .context .annotation .ComponentScan ;
1717import org .springframework .context .annotation .FilterType ;
1818import org .springframework .scheduling .annotation .EnableScheduling ;
1919
20- import com .dynxsty .dih4jda .DIH4JDA ;
21- import com .dynxsty .dih4jda .interactions .commands .ContextCommand ;
22- import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
23- import com .dynxsty .dih4jda .interactions .commands .SlashCommand .Subcommand ;
24- import com .dynxsty .dih4jda .interactions .components .ButtonHandler ;
25- import com .dynxsty .dih4jda .interactions .components .ModalHandler ;
26- import com .dynxsty .dih4jda .interactions .components .SelectMenuHandler ;
20+ import xyz .dynxsty .dih4jda .DIH4JDA ;
21+ import xyz .dynxsty .dih4jda .interactions .commands .application .ContextCommand ;
22+ import xyz .dynxsty .dih4jda .interactions .commands .application .SlashCommand ;
23+ import xyz .dynxsty .dih4jda .interactions .commands .application .SlashCommand .Subcommand ;
24+ import xyz .dynxsty .dih4jda .interactions .components .ButtonHandler ;
25+ import xyz .dynxsty .dih4jda .interactions .components .IdMapping ;
26+ import xyz .dynxsty .dih4jda .interactions .components .ModalHandler ;
27+ import xyz .dynxsty .dih4jda .interactions .components .StringSelectMenuHandler ;
2728
2829import io .sentry .Sentry ;
2930import lombok .RequiredArgsConstructor ;
3435import net .javadiscord .javabot .annotations .PreRegisteredListener ;
3536import net .javadiscord .javabot .data .config .BotConfig ;
3637import net .javadiscord .javabot .tasks .PresenceUpdater ;
37- import net .javadiscord .javabot .util .ExceptionLogger ;
3838
3939/**
4040 * The main class where the bot is initialized.
@@ -81,11 +81,7 @@ public void init() {
8181 if (!contextCommands .isEmpty ()) {
8282 dih4jda .addContextCommands (contextCommands .toArray (ContextCommand []::new ));
8383 }
84- try {
85- dih4jda .registerInteractions ();
86- } catch (ReflectiveOperationException e ) {
87- ExceptionLogger .capture (e , getClass ().getSimpleName ());
88- }
84+ dih4jda .registerInteractions ();
8985 addEventListeners (listeners );
9086 registerComponentHandlers (ctx );
9187 }
@@ -95,7 +91,7 @@ public void init() {
9591 * <ol>
9692 * <li>Setting the time zone to UTC, to keep our sanity when working with times.</li>
9793 * <li>Loading the configuration JSON file.</li>
98- * <li>Creating and configuring the {@link JDA} instance that enables the bots' Discord connectivity.</li>
94+ * <li>Creating and configuring the {@link net.dv8tion.jda.api. JDA} instance that enables the bots' Discord connectivity.</li>
9995 * <li>Initializing the {@link DIH4JDA} instance.</li>
10096 * <li>Adding event listeners to the bot.</li>
10197 * </ol>
@@ -109,29 +105,26 @@ public static void main(String[] args) throws Exception {
109105 SpringApplication .run (Bot .class , args );
110106 }
111107
112- private void registerComponentHandlers (ApplicationContext ctx ) {
108+ private void registerComponentHandlers (@ NotNull ApplicationContext ctx ) {
113109 Map <String , Object > interactionHandlers = ctx .getBeansWithAnnotation (AutoDetectableComponentHandler .class );
114- Map < List <String >, ButtonHandler > buttonHandlers = new HashMap <>();
115- Map < List <String >, ModalHandler > modalHandlers = new HashMap <>();
116- Map < List <String >, SelectMenuHandler > selectMenuHandlers = new HashMap <>();
110+ List <IdMapping < ButtonHandler >> buttonMappings = new ArrayList <>();
111+ List <IdMapping < ModalHandler >> modalMappings = new ArrayList <>();
112+ List <IdMapping < StringSelectMenuHandler >> stringSelectMappings = new ArrayList <>();
117113 for (Object handler : interactionHandlers .values ()) {
118114 AutoDetectableComponentHandler annotation = handler .getClass ().getAnnotation (AutoDetectableComponentHandler .class );
119- List < String > keys = Arrays . asList ( annotation .value () );
120- addComponentHandler (buttonHandlers , keys , handler , ButtonHandler .class );
121- addComponentHandler (modalHandlers , keys , handler , ModalHandler .class );
122- addComponentHandler (selectMenuHandlers , keys , handler , SelectMenuHandler .class );
115+ String [] keys = annotation .value ();
116+ addComponentHandler (buttonMappings , keys , handler , ButtonHandler .class );
117+ addComponentHandler (modalMappings , keys , handler , ModalHandler .class );
118+ addComponentHandler (stringSelectMappings , keys , handler , StringSelectMenuHandler .class );
123119 }
124- dih4jda .addButtonHandlers ( buttonHandlers );
125- dih4jda .addModalHandlers ( modalHandlers );
126- dih4jda .addSelectMenuHandlers ( selectMenuHandlers );
120+ dih4jda .addButtonMappings ( buttonMappings . toArray ( IdMapping []:: new ) );
121+ dih4jda .addModalMappings ( modalMappings . toArray ( IdMapping []:: new ) );
122+ dih4jda .addStringSelectMenuMappings ( stringSelectMappings . toArray ( IdMapping []:: new ) );
127123 }
128124
129- private <T > void addComponentHandler (Map < List <String >, T > handlers , List < String > keys , Object handler , Class <T > type ) {
125+ private <T > void addComponentHandler (List <IdMapping < T >> handlers , String [] keys , Object handler , @ NotNull Class <T > type ) {
130126 if (type .isInstance (handler )) {
131- T old = handlers .putIfAbsent (keys , type .cast (handler ));
132- if (old !=null ) {
133- throw new IllegalStateException ("The same interaction name was registered multiple times" );
134- }
127+ handlers .add (IdMapping .of (type .cast (handler ), keys ));
135128 }
136129 }
137130}
0 commit comments