22
33import com .dynxsty .dih4jda .DIH4JDA ;
44import com .dynxsty .dih4jda .DIH4JDABuilder ;
5+ import com .dynxsty .dih4jda .DIH4JDALogger ;
6+ import com .dynxsty .dih4jda .interactions .commands .ContextCommand ;
57import com .dynxsty .dih4jda .interactions .commands .RegistrationType ;
8+ import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
69import com .zaxxer .hikari .HikariDataSource ;
710import io .sentry .Sentry ;
11+ import lombok .Getter ;
812import lombok .extern .slf4j .Slf4j ;
913import net .dv8tion .jda .api .JDA ;
1014import net .dv8tion .jda .api .JDABuilder ;
2731import net .javadiscord .javabot .systems .moderation .report .ReportManager ;
2832import net .javadiscord .javabot .systems .moderation .server_lock .ServerLockManager ;
2933import net .javadiscord .javabot .systems .qotw .commands .questions_queue .AddQuestionSubcommand ;
34+ import net .javadiscord .javabot .systems .qotw .commands .view .QOTWQuerySubcommand ;
3035import net .javadiscord .javabot .systems .qotw .submissions .SubmissionInteractionManager ;
31- import net .javadiscord .javabot .systems .staff_commands .self_roles .SelfRoleInteractionManager ;
3236import net .javadiscord .javabot .systems .staff_commands .embeds .AddEmbedFieldSubcommand ;
3337import net .javadiscord .javabot .systems .staff_commands .embeds .CreateEmbedSubcommand ;
3438import net .javadiscord .javabot .systems .staff_commands .embeds .EditEmbedSubcommand ;
35- import net .javadiscord .javabot .systems .starboard . StarboardManager ;
39+ import net .javadiscord .javabot .systems .staff_commands . self_roles . SelfRoleInteractionManager ;
3640import net .javadiscord .javabot .systems .staff_commands .tags .CustomTagManager ;
3741import net .javadiscord .javabot .systems .staff_commands .tags .commands .CreateCustomTagSubcommand ;
3842import net .javadiscord .javabot .systems .staff_commands .tags .commands .EditCustomTagSubcommand ;
43+ import net .javadiscord .javabot .systems .starboard .StarboardManager ;
3944import net .javadiscord .javabot .systems .user_commands .leaderboard .ExperienceLeaderboardSubcommand ;
4045import net .javadiscord .javabot .tasks .MetricsUpdater ;
4146import net .javadiscord .javabot .tasks .PresenceUpdater ;
4449import net .javadiscord .javabot .util .InteractionUtils ;
4550import org .jetbrains .annotations .NotNull ;
4651import org .quartz .SchedulerException ;
52+ import org .springframework .beans .factory .annotation .Autowired ;
53+ import org .springframework .boot .SpringApplication ;
54+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
55+ import org .springframework .context .annotation .ComponentScan ;
56+ import org .springframework .context .annotation .FilterType ;
4757
4858import java .nio .file .Path ;
4959import java .time .ZoneOffset ;
5868 * The main class where the bot is initialized.
5969 */
6070@ Slf4j
71+ @ SpringBootApplication
72+ @ ComponentScan (includeFilters = @ ComponentScan .Filter (type = FilterType .ASSIGNABLE_TYPE , classes = {SlashCommand .class , ContextCommand .class }))
6173public class Bot {
6274
75+ @ Getter
6376 private static BotConfig config ;
6477
78+ @ Getter
6579 private static AutoMod autoMod ;
6680
81+ @ Getter
6782 private static DIH4JDA dih4jda ;
6883
84+ @ Getter
6985 private static MessageCache messageCache ;
7086
87+ @ Getter
7188 private static ServerLockManager serverLockManager ;
7289
90+ @ Getter
7391 private static CustomTagManager customTagManager ;
7492
93+ @ Getter
7594 private static HikariDataSource dataSource ;
7695
96+ @ Getter
7797 private static ScheduledExecutorService asyncPool ;
7898
79- private Bot () {
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+ }
80119 }
81120
82121 /**
83122 * The main method that starts the bot. This involves a few steps:
84123 * <ol>
85124 * <li>Setting the time zone to UTC, to keep our sanity when working with times.</li>
86125 * <li>Loading the configuration JSON file.</li>
87- * <li>Creating and configuring the {@link JDA} instance that enables the bot's Discord connectivity.</li>
126+ * <li>Creating and configuring the {@link JDA} instance that enables the bots' Discord connectivity.</li>
88127 * <li>Initializing the {@link DIH4JDA} instance.</li>
89128 * <li>Adding event listeners to the bot.</li>
90129 * </ol>
@@ -108,8 +147,9 @@ public static void main(String[] args) throws Exception {
108147 .build ();
109148 AllowedMentions .setDefaultMentions (EnumSet .of (Message .MentionType .ROLE , Message .MentionType .CHANNEL , Message .MentionType .USER , Message .MentionType .EMOJI ));
110149 dih4jda = DIH4JDABuilder .setJDA (jda )
111- .setCommandsPackage ("net.javadiscord.javabot" )
112- .setDefaultCommandType (RegistrationType .GUILD )
150+ .setDefaultCommandType (RegistrationType .GLOBAL )
151+ .disableLogging (DIH4JDALogger .Type .SMART_QUEUE_IGNORED )
152+ .disableAutomaticCommandRegistration ()
113153 .build ();
114154 customTagManager = new CustomTagManager (jda , dataSource );
115155 messageCache = new MessageCache ();
@@ -130,10 +170,11 @@ public static void main(String[] args) throws Exception {
130170 log .error ("Could not initialize all scheduled tasks." , e );
131171 jda .shutdown ();
132172 }
173+ SpringApplication .run (Bot .class , args );
133174 }
134175
135176 /**
136- * Adds all the bot's event listeners to the JDA instance, except for
177+ * Adds all the bots' event listeners to the JDA instance, except for
137178 * the {@link AutoMod} instance.
138179 *
139180 * @param jda The JDA bot instance to add listeners to.
@@ -157,7 +198,7 @@ private static void addEventListeners(@NotNull JDA jda, @NotNull DIH4JDA dih4jda
157198 new PingableNameListener (),
158199 new HugListener ()
159200 );
160- dih4jda .addListener (new DIH4JDAListener ());
201+ dih4jda .addEventListener (new DIH4JDAListener ());
161202 }
162203
163204 private static void addComponentHandler (@ NotNull DIH4JDA dih4jda ) {
@@ -167,7 +208,8 @@ private static void addComponentHandler(@NotNull DIH4JDA dih4jda) {
167208 List .of ("resolve-report" ), new ReportManager (),
168209 List .of ("self-role" ), new SelfRoleInteractionManager (),
169210 List .of ("qotw-submission" ), new SubmissionInteractionManager (),
170- List .of ("help-channel" , "help-thank" ), new HelpChannelInteractionManager ()
211+ List .of ("help-channel" , "help-thank" ), new HelpChannelInteractionManager (),
212+ List .of ("qotw-list-questions" ), new QOTWQuerySubcommand ()
171213 ));
172214 dih4jda .addModalHandlers (Map .of (
173215 List .of ("qotw-add-question" ), new AddQuestionSubcommand (),
@@ -184,80 +226,5 @@ private static void addComponentHandler(@NotNull DIH4JDA dih4jda) {
184226 List .of ("qotw-submission-select" ), new SubmissionInteractionManager ()
185227 ));
186228 }
187-
188- /**
189- * The set of configuration properties that this bot uses.
190- *
191- * @return The {@link BotConfig} which was set in {@link Bot#main(String[])}.
192- */
193- public static BotConfig getConfig () {
194- return config ;
195- }
196-
197- /**
198- * A static reference to the bots' {@link AutoMod} instance.
199- *
200- * @return The {@link AutoMod} instance which was created in {@link Bot#main(String[])}.
201- */
202- public static AutoMod getAutoMod () {
203- return autoMod ;
204- }
205-
206- /**
207- * A static reference to the bots' {@link DIH4JDA} instance.
208- *
209- * @return The {@link DIH4JDA} instance which was set in {@link Bot#main(String[])}.
210- */
211- public static DIH4JDA getDIH4JDA () {
212- return dih4jda ;
213- }
214-
215- /**
216- * The bots' {@link MessageCache}, which handles logging of deleted and edited messages.
217- *
218- * @return The {@link MessageCache} which was initialized in {@link Bot#main(String[])}.
219- */
220- public static MessageCache getMessageCache () {
221- return messageCache ;
222- }
223-
224- /**
225- * A reference to the bots' {@link ServerLockManager}.
226- *
227- * @return The {@link ServerLockManager} which was created in {@link Bot#main(String[])}.
228- */
229- public static ServerLockManager getServerLockManager () {
230- return serverLockManager ;
231- }
232-
233- /**
234- * A static reference to the {@link CustomTagManager} which handles and loads all registered Custom Commands.
235- *
236- * @return The {@link CustomTagManager} which was created in {@link Bot#main(String[])}.
237- */
238- public static CustomTagManager getCustomTagManager () {
239- return customTagManager ;
240- }
241-
242- /**
243- * A reference to the data source that provides access to the relational
244- * database that this bot users for certain parts of the application. Use
245- * this to obtain a connection and perform transactions.
246- *
247- * @return The {@link HikariDataSource} which was initialized in {@link Bot#main(String[])}.
248- */
249- public static HikariDataSource getDataSource () {
250- return dataSource ;
251- }
252-
253- /**
254- * A general-purpose thread pool that can be used by the bot to execute
255- * tasks outside the main event processing thread.
256- *
257- * @return The {@link ScheduledExecutorService} which was set in {@link Bot#main(String[])}.
258- */
259- public static ScheduledExecutorService getAsyncPool () {
260- return asyncPool ;
261- }
262229}
263230
0 commit comments