forked from Java-Discord/JavaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringConfig.java
More file actions
106 lines (96 loc) · 3.54 KB
/
SpringConfig.java
File metadata and controls
106 lines (96 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package net.discordjug.javabot;
import java.nio.file.Path;
import java.util.Collection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.regex.Pattern;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.discordjug.javabot.data.config.PatternTypeAdapter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.dynxsty.dih4jda.DIH4JDA;
import xyz.dynxsty.dih4jda.DIH4JDABuilder;
import xyz.dynxsty.dih4jda.exceptions.DIH4JDAException;
import xyz.dynxsty.dih4jda.interactions.commands.application.RegistrationType;
import lombok.RequiredArgsConstructor;
import net.discordjug.javabot.annotations.PreRegisteredListener;
import net.discordjug.javabot.data.config.BotConfig;
import net.discordjug.javabot.data.config.SystemsConfig;
import net.discordjug.javabot.tasks.PresenceUpdater;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
/**
* This class holds all configuration settings and {@link Bean}s.
*/
@Configuration
@RequiredArgsConstructor
public class SpringConfig {
@Bean
PresenceUpdater standardActivityPresenceUpdater(ScheduledExecutorService threadPool) {
return PresenceUpdater.standardActivities(threadPool);
}
@Bean
ScheduledExecutorService asyncPool(BotConfig config) {
return Executors.newScheduledThreadPool(config.getSystems().getAsyncPoolSize());
}
@Bean
SystemsConfig systemsConfig(BotConfig botConfig) {
return botConfig.getSystems();
}
/**
* Initializes the {@link JDA} instances.
* @param botConfig the main configuration of the bot
* @param ctx the Spring application context used for obtaining all listeners
* @return the initialized {@link JDA} object
*/
@Bean
JDA jda(BotConfig botConfig, ApplicationContext ctx) {
Collection<Object> listeners = ctx.getBeansWithAnnotation(PreRegisteredListener.class).values();
return JDABuilder.createDefault(botConfig.getSystems().getJdaBotToken())
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.setChunkingFilter(ChunkingFilter.ALL)
.setMemberCachePolicy(MemberCachePolicy.VOICE)
.enableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES, GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(listeners.toArray())
.build();
}
/**
* Initializes {@link DIH4JDA}.
* @param jda the initialized {@link JDA} instance
* @return the initialized {@link DIH4JDA} object
* @throws DIH4JDAException if an error occurs while initializing {@link DIH4JDA}
*/
@Bean
DIH4JDA initializeDIH4JDA(JDA jda) throws DIH4JDAException {
DIH4JDA.setDefaultRegistrationType(RegistrationType.GLOBAL);
return DIH4JDABuilder.setJDA(jda)
.setGlobalSmartQueue(false)
.setGuildSmartQueue(false)
.disableAutomaticCommandRegistration()
.build();
}
@Bean
BotConfig botConfig() {
BotConfig botConfig = new BotConfig(Path.of("config"));
if (botConfig.getSystems().getJdaBotToken().isEmpty()) {
throw new RuntimeException("JDA Token not set. Stopping Bot...");
}
return botConfig;
}
@Bean
Gson gson() {
return new GsonBuilder()
.serializeNulls()
.setPrettyPrinting()
.registerTypeAdapter(Pattern.class, new PatternTypeAdapter())
.create();
}
}