66import com .dynxsty .dih4jda .interactions .commands .ContextCommand ;
77import com .dynxsty .dih4jda .interactions .commands .RegistrationType ;
88import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
9- import com .dynxsty .dih4jda .util .Checks ;
10- import com .dynxsty .dih4jda .util .ClassUtils ;
119import com .zaxxer .hikari .HikariDataSource ;
1210import io .sentry .Sentry ;
1311import lombok .Getter ;
3533import net .javadiscord .javabot .systems .qotw .commands .questions_queue .AddQuestionSubcommand ;
3634import net .javadiscord .javabot .systems .qotw .commands .view .QOTWQuerySubcommand ;
3735import net .javadiscord .javabot .systems .qotw .submissions .SubmissionInteractionManager ;
38- import net .javadiscord .javabot .systems .staff_commands .self_roles .SelfRoleInteractionManager ;
3936import net .javadiscord .javabot .systems .staff_commands .embeds .AddEmbedFieldSubcommand ;
4037import net .javadiscord .javabot .systems .staff_commands .embeds .CreateEmbedSubcommand ;
4138import net .javadiscord .javabot .systems .staff_commands .embeds .EditEmbedSubcommand ;
42- import net .javadiscord .javabot .systems .starboard . StarboardManager ;
39+ import net .javadiscord .javabot .systems .staff_commands . self_roles . SelfRoleInteractionManager ;
4340import net .javadiscord .javabot .systems .staff_commands .tags .CustomTagManager ;
4441import net .javadiscord .javabot .systems .staff_commands .tags .commands .CreateCustomTagSubcommand ;
4542import net .javadiscord .javabot .systems .staff_commands .tags .commands .EditCustomTagSubcommand ;
43+ import net .javadiscord .javabot .systems .starboard .StarboardManager ;
4644import net .javadiscord .javabot .systems .user_commands .leaderboard .ExperienceLeaderboardSubcommand ;
4745import net .javadiscord .javabot .tasks .MetricsUpdater ;
4846import net .javadiscord .javabot .tasks .PresenceUpdater ;
5149import net .javadiscord .javabot .util .InteractionUtils ;
5250import org .jetbrains .annotations .NotNull ;
5351import org .quartz .SchedulerException ;
54- import org .springframework .beans .factory .config . BeanDefinition ;
52+ import org .springframework .beans .factory .annotation . Autowired ;
5553import org .springframework .boot .SpringApplication ;
5654import org .springframework .boot .autoconfigure .SpringBootApplication ;
57- import org .springframework .context .annotation .ClassPathScanningCandidateComponentProvider ;
58- import org .springframework .core . type . filter . AssignableTypeFilter ;
55+ import org .springframework .context .annotation .ComponentScan ;
56+ import org .springframework .context . annotation . FilterType ;
5957
6058import java .nio .file .Path ;
6159import java .time .ZoneOffset ;
62- import java .util .*;
60+ import java .util .EnumSet ;
61+ import java .util .List ;
62+ import java .util .Map ;
63+ import java .util .TimeZone ;
6364import java .util .concurrent .Executors ;
6465import java .util .concurrent .ScheduledExecutorService ;
6566
6869 */
6970@ Slf4j
7071@ SpringBootApplication
72+ @ ComponentScan (includeFilters = @ ComponentScan .Filter (type = FilterType .ASSIGNABLE_TYPE , classes = {SlashCommand .class , ContextCommand .class }))
7173public class Bot {
7274
7375 @ Getter
@@ -94,6 +96,28 @@ public class Bot {
9496 @ Getter
9597 private static ScheduledExecutorService asyncPool ;
9698
99+ /**
100+ * The constructor of this class, which also adds all {@link SlashCommand} and
101+ * {@link ContextCommand} to the {@link DIH4JDA} instance.
102+ *
103+ * @param commands The {@link Autowired} list of {@link SlashCommand}s.
104+ * @param contexts The {@link Autowired} list of {@link ContextCommand}s.
105+ */
106+ @ Autowired
107+ public Bot (final List <SlashCommand > commands , final List <ContextCommand > contexts ) {
108+ if (!commands .isEmpty ()) {
109+ getDih4jda ().addSlashCommands (commands .toArray (SlashCommand []::new ));
110+ }
111+ if (!contexts .isEmpty ()) {
112+ getDih4jda ().addContextCommands (contexts .toArray (ContextCommand []::new ));
113+ }
114+ try {
115+ getDih4jda ().registerInteractions ();
116+ } catch (ReflectiveOperationException e ) {
117+ ExceptionLogger .capture (e , getClass ().getSimpleName ());
118+ }
119+ }
120+
97121 /**
98122 * The main method that starts the bot. This involves a few steps:
99123 * <ol>
@@ -125,11 +149,11 @@ public static void main(String[] args) throws Exception {
125149 dih4jda = DIH4JDABuilder .setJDA (jda )
126150 .setDefaultCommandType (RegistrationType .GLOBAL )
127151 .disableLogging (DIH4JDALogger .Type .SMART_QUEUE_IGNORED )
152+ .disableAutomaticCommandRegistration ()
128153 .build ();
129154 customTagManager = new CustomTagManager (jda , dataSource );
130155 messageCache = new MessageCache ();
131156 serverLockManager = new ServerLockManager (jda );
132- addCommands (dih4jda );
133157 addEventListeners (jda , dih4jda );
134158 addComponentHandler (dih4jda );
135159 // initialize Sentry
@@ -149,45 +173,6 @@ public static void main(String[] args) throws Exception {
149173 SpringApplication .run (Bot .class , args );
150174 }
151175
152- /**
153- * Uses Springs' ClassPath scanning in order to register all {@link SlashCommand} &
154- * {@link ContextCommand}s using {@link DIH4JDA}.
155- *
156- * @param dih4jda The {@link DIH4JDA} which is used in order to execute both {@link SlashCommand}s
157- * and {@link ContextCommand}.
158- */
159- private static void addCommands (DIH4JDA dih4jda ) {
160- List <SlashCommand > commands = new ArrayList <>();
161- List <ContextCommand > contexts = new ArrayList <>();
162- ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider (false );
163- provider .addIncludeFilter (new AssignableTypeFilter (SlashCommand .class ));
164- provider .addIncludeFilter (new AssignableTypeFilter (ContextCommand .class ));
165- Set <BeanDefinition > classes = provider .findCandidateComponents ("net/javadiscord/javabot" );
166- for (BeanDefinition bean : classes ) {
167- try {
168- Class <?> cls = Class .forName (bean .getBeanClassName ());
169- if (Checks .checkEmptyConstructor (cls )) {
170- Object instance = ClassUtils .getInstance (cls );
171- if (instance instanceof SlashCommand slashCommand ) {
172- commands .add (slashCommand );
173- } else if (instance instanceof ContextCommand contextCommand ) {
174- contexts .add (contextCommand );
175- }
176- } else {
177- log .error ("Class {} needs an empty constructor!" , cls .getSimpleName ());
178- }
179- } catch (ReflectiveOperationException e ) {
180- ExceptionLogger .capture (e , Bot .class .getSimpleName ());
181- }
182- }
183- if (!commands .isEmpty ()) {
184- dih4jda .addSlashCommands (commands .toArray (SlashCommand []::new ));
185- }
186- if (!contexts .isEmpty ()) {
187- dih4jda .addContextCommands (contexts .toArray (ContextCommand []::new ));
188- }
189- }
190-
191176 /**
192177 * Adds all the bots' event listeners to the JDA instance, except for
193178 * the {@link AutoMod} instance.
@@ -224,7 +209,7 @@ private static void addComponentHandler(@NotNull DIH4JDA dih4jda) {
224209 List .of ("self-role" ), new SelfRoleInteractionManager (),
225210 List .of ("qotw-submission" ), new SubmissionInteractionManager (),
226211 List .of ("help-channel" , "help-thank" ), new HelpChannelInteractionManager (),
227- List .of ("qotw-list-questions" ),new QOTWQuerySubcommand ()
212+ List .of ("qotw-list-questions" ), new QOTWQuerySubcommand ()
228213 ));
229214 dih4jda .addModalHandlers (Map .of (
230215 List .of ("qotw-add-question" ), new AddQuestionSubcommand (),
0 commit comments