11package net .javadiscord .javabot ;
22
3+ import java .time .ZoneOffset ;
4+ import java .util .Arrays ;
5+ import java .util .EnumSet ;
6+ import java .util .HashMap ;
7+ import java .util .List ;
8+ import java .util .Map ;
9+ import java .util .TimeZone ;
10+
11+ import javax .annotation .PostConstruct ;
12+
13+ import org .springframework .boot .SpringApplication ;
14+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
15+ import org .springframework .context .ApplicationContext ;
16+ import org .springframework .context .annotation .ComponentScan ;
17+ import org .springframework .context .annotation .FilterType ;
18+ import org .springframework .scheduling .annotation .EnableScheduling ;
19+
320import com .dynxsty .dih4jda .DIH4JDA ;
4- import com .dynxsty .dih4jda .DIH4JDABuilder ;
5- import com .dynxsty .dih4jda .DIH4JDALogger ;
621import com .dynxsty .dih4jda .interactions .commands .ContextCommand ;
7- import com .dynxsty .dih4jda .interactions .commands .RegistrationType ;
822import com .dynxsty .dih4jda .interactions .commands .SlashCommand ;
923import com .dynxsty .dih4jda .interactions .components .ButtonHandler ;
1024import com .dynxsty .dih4jda .interactions .components .ModalHandler ;
1125import com .dynxsty .dih4jda .interactions .components .SelectMenuHandler ;
12- import com .zaxxer .hikari .HikariDataSource ;
1326import io .sentry .Sentry ;
14- import lombok .Getter ;
15- import net .dv8tion .jda .api .JDA ;
16- import net .dv8tion .jda .api .JDABuilder ;
17- import net .dv8tion .jda .api .OnlineStatus ;
27+ import lombok .RequiredArgsConstructor ;
1828import net .dv8tion .jda .api .entities .Message ;
1929import net .dv8tion .jda .api .hooks .ListenerAdapter ;
20- import net .dv8tion .jda .api .requests .GatewayIntent ;
2130import net .dv8tion .jda .api .utils .AllowedMentions ;
22- import net .dv8tion .jda .api .utils .ChunkingFilter ;
23- import net .dv8tion .jda .api .utils .MemberCachePolicy ;
24- import net .dv8tion .jda .api .utils .cache .CacheFlag ;
2531import net .javadiscord .javabot .data .config .BotConfig ;
26- import net .javadiscord .javabot .data .h2db .DbHelper ;
2732import net .javadiscord .javabot .systems .AutoDetectableComponentHandler ;
2833import net .javadiscord .javabot .tasks .PresenceUpdater ;
2934import net .javadiscord .javabot .util .ExceptionLogger ;
30- import org .springframework .beans .factory .annotation .Autowired ;
31- import org .springframework .boot .SpringApplication ;
32- import org .springframework .boot .autoconfigure .SpringBootApplication ;
33- import org .springframework .context .ConfigurableApplicationContext ;
34- import org .springframework .context .annotation .ComponentScan ;
35- import org .springframework .context .annotation .FilterType ;
36- import org .springframework .scheduling .annotation .EnableScheduling ;
37-
38- import java .nio .file .Path ;
39- import java .time .ZoneOffset ;
40- import java .util .Arrays ;
41- import java .util .EnumSet ;
42- import java .util .HashMap ;
43- import java .util .List ;
44- import java .util .Map ;
45- import java .util .TimeZone ;
46- import java .util .concurrent .Executors ;
47- import java .util .concurrent .ScheduledExecutorService ;
48-
49- import javax .annotation .PostConstruct ;
5035
5136/**
5237 * The main class where the bot is initialized.
5742 excludeFilters = @ ComponentScan .Filter (type =FilterType .ASSIGNABLE_TYPE , classes = PresenceUpdater .class )
5843)
5944@ EnableScheduling
45+ @ RequiredArgsConstructor
6046public class Bot {
6147
62- @ Getter
63- private static BotConfig config ;
64-
65- @ Getter
66- private static DIH4JDA dih4jda ;
67-
68- @ Getter
69- private static HikariDataSource dataSource ;
48+ private final DIH4JDA dih4jda ;
49+ private final BotConfig config ;
50+ private final List <SlashCommand > commands ;
51+ private final List <ContextCommand > contextCommands ;
52+ private final List <ListenerAdapter > listeners ;
53+ private final ApplicationContext ctx ;
7054
71- @ Getter
72- private static ScheduledExecutorService asyncPool ;
73-
74- /**
75- * The constructor of this class, which also adds all {@link SlashCommand} and
76- * {@link ContextCommand} to the {@link DIH4JDA} instance.
77- *
78- * @param commands The {@link Autowired} list of {@link SlashCommand}s.
79- * @param contexts The {@link Autowired} list of {@link ContextCommand}s.
80- * @param listeners The {@link Autowired} list of {@link ListenerAdapter}s.
81- */
82- @ Autowired
83- public Bot (final List <SlashCommand > commands , final List <ContextCommand > contexts , final List <ListenerAdapter > listeners ) {
84- if (!commands .isEmpty ()) {
85- getDih4jda ().addSlashCommands (commands .toArray (SlashCommand []::new ));
86- }
87- if (!contexts .isEmpty ()) {
88- getDih4jda ().addContextCommands (contexts .toArray (ContextCommand []::new ));
89- }
90- try {
91- getDih4jda ().registerInteractions ();
92- } catch (ReflectiveOperationException e ) {
93- ExceptionLogger .capture (e , getClass ().getSimpleName ());
94- }
95- addEventListeners (listeners );
96- }
9755
9856 private void addEventListeners (final List <ListenerAdapter > listeners ) {
9957 for (ListenerAdapter listener : listeners ) {
@@ -103,7 +61,7 @@ private void addEventListeners(final List<ListenerAdapter> listeners) {
10361 }
10462
10563 /**
106- * Initialize Sentry.
64+ * Initializes Sentry, interactions and listeners .
10765 */
10866 @ PostConstruct
10967 public void init () {
@@ -112,6 +70,19 @@ public void init() {
11270 options .setTracesSampleRate (1.0 );
11371 options .setDebug (false );
11472 });
73+ if (!commands .isEmpty ()) {
74+ dih4jda .addSlashCommands (commands .toArray (SlashCommand []::new ));
75+ }
76+ if (!contextCommands .isEmpty ()) {
77+ dih4jda .addContextCommands (contextCommands .toArray (ContextCommand []::new ));
78+ }
79+ try {
80+ dih4jda .registerInteractions ();
81+ } catch (ReflectiveOperationException e ) {
82+ ExceptionLogger .capture (e , getClass ().getSimpleName ());
83+ }
84+ addEventListeners (listeners );
85+ registerComponentHandlers (ctx );
11586 }
11687
11788 /**
@@ -129,45 +100,28 @@ public void init() {
129100 */
130101 public static void main (String [] args ) throws Exception {
131102 TimeZone .setDefault (TimeZone .getTimeZone (ZoneOffset .UTC ));
132- config = new BotConfig (Path .of ("config" ));
133- dataSource = DbHelper .initDataSource (config );
134- asyncPool = Executors .newScheduledThreadPool (config .getSystems ().getAsyncPoolSize ());
135- JDA jda = JDABuilder .createDefault (config .getSystems ().getJdaBotToken ())
136- .setStatus (OnlineStatus .DO_NOT_DISTURB )
137- .setChunkingFilter (ChunkingFilter .ALL )
138- .setMemberCachePolicy (MemberCachePolicy .ALL )
139- .enableCache (CacheFlag .ACTIVITY )
140- .enableIntents (GatewayIntent .GUILD_MEMBERS , GatewayIntent .GUILD_PRESENCES , GatewayIntent .MESSAGE_CONTENT )
141- .build ();
142103 AllowedMentions .setDefaultMentions (EnumSet .of (Message .MentionType .ROLE , Message .MentionType .CHANNEL , Message .MentionType .USER , Message .MentionType .EMOJI ));
143- dih4jda = DIH4JDABuilder .setJDA (jda )
144- .setDefaultCommandType (RegistrationType .GLOBAL )
145- .disableLogging (DIH4JDALogger .Type .SMART_QUEUE_IGNORED )
146- .disableAutomaticCommandRegistration ()
147- .build ();
148- ConfigurableApplicationContext ctx = SpringApplication .run (Bot .class , args );
149- registerComponentHandlers (ctx );
150-
104+ SpringApplication .run (Bot .class , args );
151105 }
152106
153- private static void registerComponentHandlers (ConfigurableApplicationContext ctx ) {
107+ private void registerComponentHandlers (ApplicationContext ctx ) {
154108 Map <String , Object > interactionHandlers = ctx .getBeansWithAnnotation (AutoDetectableComponentHandler .class );
155109 Map <List <String >, ButtonHandler > buttonHandlers = new HashMap <>();
156110 Map <List <String >, ModalHandler > modalHandlers = new HashMap <>();
157111 Map <List <String >, SelectMenuHandler > selectMenuHandlers = new HashMap <>();
158112 for (Object handler : interactionHandlers .values ()) {
159113 AutoDetectableComponentHandler annotation = handler .getClass ().getAnnotation (AutoDetectableComponentHandler .class );
160114 List <String > keys = Arrays .asList (annotation .value ());
161- addHandler (buttonHandlers , keys , handler , ButtonHandler .class );
162- addHandler (modalHandlers , keys , handler , ModalHandler .class );
163- addHandler (selectMenuHandlers , keys , handler , SelectMenuHandler .class );
115+ addComponentHandler (buttonHandlers , keys , handler , ButtonHandler .class );
116+ addComponentHandler (modalHandlers , keys , handler , ModalHandler .class );
117+ addComponentHandler (selectMenuHandlers , keys , handler , SelectMenuHandler .class );
164118 }
165119 dih4jda .addButtonHandlers (buttonHandlers );
166120 dih4jda .addModalHandlers (modalHandlers );
167121 dih4jda .addSelectMenuHandlers (selectMenuHandlers );
168122 }
169123
170- private static <T > void addHandler (Map <List <String >, T > handlers , List <String > keys , Object handler , Class <T > type ) {
124+ private <T > void addComponentHandler (Map <List <String >, T > handlers , List <String > keys , Object handler , Class <T > type ) {
171125 if (type .isInstance (handler )) {
172126 T old = handlers .putIfAbsent (keys , type .cast (handler ));
173127 if (old !=null ) {
0 commit comments