Skip to content

Commit ef5e6b6

Browse files
First pass on implementing internal API
1 parent 7920d0c commit ef5e6b6

File tree

9 files changed

+106
-79
lines changed

9 files changed

+106
-79
lines changed

build.gradle.kts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import com.github.jengelman.gradle.plugins.shadow.transformers.*
3+
14
plugins {
25
java
36
id("com.github.johnrengelman.shadow") version "7.1.2"
7+
id("io.spring.dependency-management") version "1.0.11.RELEASE"
48
checkstyle
59
}
610

@@ -26,6 +30,9 @@ dependencies {
2630
implementation("com.github.DenuxPlays:DIH4JDA:7ac2c9c77c")
2731
implementation("org.reflections:reflections:0.10.2")
2832

33+
// Discord OAuth2
34+
implementation("com.github.Mokulu:discord-oauth2-api:1.0.2")
35+
2936
implementation("com.google.code.gson:gson:2.9.0")
3037
implementation("org.yaml:snakeyaml:1.30")
3138
implementation("com.google.re2j:re2j:1.6")
@@ -52,6 +59,12 @@ dependencies {
5259

5360
// Sentry
5461
implementation("io.sentry:sentry:6.3.0")
62+
63+
// Spring Boot
64+
implementation("org.springframework.boot:spring-boot-starter-web:2.7.0")
65+
66+
// Apache HTTP-Client
67+
implementation("org.apache.httpcomponents:httpclient:4.5.13")
5568
}
5669

5770
tasks.withType<Jar> {
@@ -67,6 +80,19 @@ tasks.withType<JavaCompile>().configureEach {
6780
}
6881
tasks.withType<Test>{ useJUnitPlatform() }
6982

83+
tasks.withType<ShadowJar> {
84+
isZip64 = true
85+
// Required for Spring
86+
mergeServiceFiles()
87+
append("META-INF/spring.handlers")
88+
append("META-INF/spring.schemas")
89+
append("META-INF/spring.tooling")
90+
transform(PropertiesFileTransformer().apply {
91+
paths = listOf("META-INF/spring.factories")
92+
mergeStrategy = "append"
93+
})
94+
}
95+
7096
checkstyle {
7197
toolVersion = "9.1"
7298
configDirectory.set(File("checkstyle"))

src/main/java/net/javadiscord/javabot/Bot.java

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.dynxsty.dih4jda.DIH4JDABuilder;
55
import com.dynxsty.dih4jda.interactions.commands.RegistrationType;
66
import com.zaxxer.hikari.HikariDataSource;
7+
import io.mokulu.discord.oauth.DiscordOAuth;
78
import io.sentry.Sentry;
9+
import lombok.Getter;
810
import lombok.extern.slf4j.Slf4j;
911
import net.dv8tion.jda.api.JDA;
1012
import net.dv8tion.jda.api.JDABuilder;
@@ -16,6 +18,7 @@
1618
import net.dv8tion.jda.api.utils.MemberCachePolicy;
1719
import net.dv8tion.jda.api.utils.cache.CacheFlag;
1820
import net.javadiscord.javabot.data.config.BotConfig;
21+
import net.javadiscord.javabot.data.config.SystemsConfig;
1922
import net.javadiscord.javabot.data.h2db.DbHelper;
2023
import net.javadiscord.javabot.data.h2db.commands.QuickMigrateSubcommand;
2124
import net.javadiscord.javabot.data.h2db.message_cache.MessageCache;
@@ -45,6 +48,8 @@
4548
import net.javadiscord.javabot.util.InteractionUtils;
4649
import org.jetbrains.annotations.NotNull;
4750
import org.quartz.SchedulerException;
51+
import org.springframework.boot.SpringApplication;
52+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4853

4954
import java.nio.file.Path;
5055
import java.time.ZoneOffset;
@@ -59,26 +64,37 @@
5964
* The main class where the bot is initialized.
6065
*/
6166
@Slf4j
67+
@SpringBootApplication
6268
public class Bot {
6369

70+
@Getter
6471
private static BotConfig config;
6572

73+
@Getter
6674
private static AutoMod autoMod;
6775

76+
@Getter
6877
private static DIH4JDA dih4jda;
6978

79+
@Getter
80+
private static DiscordOAuth oAuth;
81+
82+
@Getter
7083
private static MessageCache messageCache;
7184

85+
@Getter
7286
private static ServerLockManager serverLockManager;
7387

88+
@Getter
7489
private static CustomTagManager customTagManager;
7590

91+
@Getter
7692
private static HikariDataSource dataSource;
7793

94+
@Getter
7895
private static ScheduledExecutorService asyncPool;
7996

80-
private Bot() {
81-
}
97+
public Bot() {}
8298

8399
/**
84100
* The main method that starts the bot. This involves a few steps:
@@ -112,6 +128,8 @@ public static void main(String[] args) throws Exception {
112128
.setCommandsPackage("net.javadiscord.javabot")
113129
.setDefaultCommandType(RegistrationType.GLOBAL)
114130
.build();
131+
SystemsConfig.ApiConfig apiConfig = config.getSystems().getApiConfig();
132+
oAuth = new DiscordOAuth(jda.getSelfUser().getApplicationId(), apiConfig.getClientSecret(), apiConfig.getRedirectUrl(), apiConfig.getScopes());
115133
customTagManager = new CustomTagManager(jda, dataSource);
116134
messageCache = new MessageCache();
117135
serverLockManager = new ServerLockManager(jda);
@@ -131,6 +149,7 @@ public static void main(String[] args) throws Exception {
131149
log.error("Could not initialize all scheduled tasks.", e);
132150
jda.shutdown();
133151
}
152+
SpringApplication.run(Bot.class, args);
134153
}
135154

136155
/**
@@ -186,80 +205,5 @@ private static void addComponentHandler(@NotNull DIH4JDA dih4jda) {
186205
List.of("qotw-submission-select"), new SubmissionInteractionManager()
187206
));
188207
}
189-
190-
/**
191-
* The set of configuration properties that this bot uses.
192-
*
193-
* @return The {@link BotConfig} which was set in {@link Bot#main(String[])}.
194-
*/
195-
public static BotConfig getConfig() {
196-
return config;
197-
}
198-
199-
/**
200-
* A static reference to the bots' {@link AutoMod} instance.
201-
*
202-
* @return The {@link AutoMod} instance which was created in {@link Bot#main(String[])}.
203-
*/
204-
public static AutoMod getAutoMod() {
205-
return autoMod;
206-
}
207-
208-
/**
209-
* A static reference to the bots' {@link DIH4JDA} instance.
210-
*
211-
* @return The {@link DIH4JDA} instance which was set in {@link Bot#main(String[])}.
212-
*/
213-
public static DIH4JDA getDIH4JDA() {
214-
return dih4jda;
215-
}
216-
217-
/**
218-
* The bots' {@link MessageCache}, which handles logging of deleted and edited messages.
219-
*
220-
* @return The {@link MessageCache} which was initialized in {@link Bot#main(String[])}.
221-
*/
222-
public static MessageCache getMessageCache() {
223-
return messageCache;
224-
}
225-
226-
/**
227-
* A reference to the bots' {@link ServerLockManager}.
228-
*
229-
* @return The {@link ServerLockManager} which was created in {@link Bot#main(String[])}.
230-
*/
231-
public static ServerLockManager getServerLockManager() {
232-
return serverLockManager;
233-
}
234-
235-
/**
236-
* A static reference to the {@link CustomTagManager} which handles and loads all registered Custom Commands.
237-
*
238-
* @return The {@link CustomTagManager} which was created in {@link Bot#main(String[])}.
239-
*/
240-
public static CustomTagManager getCustomTagManager() {
241-
return customTagManager;
242-
}
243-
244-
/**
245-
* A reference to the data source that provides access to the relational
246-
* database that this bot users for certain parts of the application. Use
247-
* this to obtain a connection and perform transactions.
248-
*
249-
* @return The {@link HikariDataSource} which was initialized in {@link Bot#main(String[])}.
250-
*/
251-
public static HikariDataSource getDataSource() {
252-
return dataSource;
253-
}
254-
255-
/**
256-
* A general-purpose thread pool that can be used by the bot to execute
257-
* tasks outside the main event processing thread.
258-
*
259-
* @return The {@link ScheduledExecutorService} which was set in {@link Bot#main(String[])}.
260-
*/
261-
public static ScheduledExecutorService getAsyncPool() {
262-
return asyncPool;
263-
}
264208
}
265209

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.javadiscord.javabot.api.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class MetricsData {
7+
private long memberCount;
8+
private long onlineCount;
9+
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.javadiscord.javabot.api.model;
2+
3+
import lombok.Data;
4+
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
5+
6+
import java.util.List;
7+
8+
@Data
9+
public class QOTWLeaderboardData {
10+
private List<QOTWAccount> accounts;
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.javadiscord.javabot.api.model;
2+
3+
import lombok.Data;
4+
import net.javadiscord.javabot.systems.help.model.HelpAccount;
5+
import net.javadiscord.javabot.systems.moderation.warn.model.Warn;
6+
import net.javadiscord.javabot.systems.qotw.model.QOTWAccount;
7+
import net.javadiscord.javabot.systems.user_preferences.model.UserPreference;
8+
9+
import java.util.List;
10+
11+
@Data
12+
public class UserProfileData {
13+
private long userId;
14+
private String avatarUrl;
15+
private QOTWAccount qotwAccount;
16+
private HelpAccount helpAccount;
17+
private List<UserPreference> preferences;
18+
private List<Warn> warns;
19+
}

src/main/java/net/javadiscord/javabot/data/config/SystemsConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class SystemsConfig {
4242
*/
4343
private HikariConfig hikariConfig = new HikariConfig();
4444

45+
/**
46+
* Configurations settings for the bots' internal API.
47+
*/
48+
private ApiConfig apiConfig = new ApiConfig();
49+
4550
/**
4651
* Configuration settings for certain commands which need an extra layer of
4752
* security.
@@ -53,6 +58,16 @@ public class SystemsConfig {
5358
*/
5459
private EmojiConfig emojiConfig = new EmojiConfig();
5560

61+
/**
62+
* Configurations settings for the bots' internal API.
63+
*/
64+
@Data
65+
public static class ApiConfig {
66+
private String clientSecret = "";
67+
private String redirectUrl = "";
68+
private String[] scopes = new String[]{};
69+
}
70+
5671
/**
5772
* Configuration settings for the Hikari connection pool.
5873
*/

src/main/java/net/javadiscord/javabot/listener/GuildJoinListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class GuildJoinListener extends ListenerAdapter {
1313
public void onGuildJoin(GuildJoinEvent event) {
1414
Bot.getConfig().addGuild(event.getGuild());
1515
try {
16-
Bot.getDIH4JDA().registerInteractions();
16+
Bot.getDih4jda().registerInteractions();
1717
} catch (ReflectiveOperationException e) {
1818
ExceptionLogger.capture(e, getClass().getSimpleName());
1919
}

src/main/java/net/javadiscord/javabot/systems/moderation/warn/model/Warn.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.Data;
44
import lombok.NoArgsConstructor;
5+
import org.jetbrains.annotations.NotNull;
56

67
import java.time.LocalDateTime;
78

@@ -28,7 +29,7 @@ public class Warn {
2829
* @param severity The severity of the warning.
2930
* @param reason The reason for the warning.
3031
*/
31-
public Warn(long userId, long warnedBy, WarnSeverity severity, String reason) {
32+
public Warn(long userId, long warnedBy, @NotNull WarnSeverity severity, String reason) {
3233
this.userId = userId;
3334
this.warnedBy = warnedBy;
3435
this.severity = severity.name();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=9000

0 commit comments

Comments
 (0)