I have massively fucked up

This commit is contained in:
floppydiskette 2024-08-21 21:13:55 +01:00
parent 7488385293
commit 178d7b5939
Signed by: fwoppydwisk
SSH key fingerprint: SHA256:Hqn452XQ1ETzUt/FthJu6+OFkS4NBxCv5VQSEvuk7CE
3 changed files with 26 additions and 42 deletions

View file

@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component> </component>
</project> </project>

View file

@ -4,13 +4,13 @@ import com.google.gson.Gson;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
//You can add more fields in this class, if your input json matches the structure //You can add more fields in this class, if your input json matches the structure
//You will need a valid config.json in the package com.freya02.bot for this to work //You will need a valid config.json in the package com.freya02.bot for this to work
public class Config { public class Config {
@SuppressWarnings("unused") private String token; @SuppressWarnings("unused") private String token;
@SuppressWarnings("unused") private String prefix; @SuppressWarnings("unused") private DBConfig dbConfig;
@SuppressWarnings("unused") private DBConfig mariadb;
/** /**
* Returns the configuration object for this bot * Returns the configuration object for this bot
@ -19,18 +19,18 @@ public class Config {
* @throws IOException if the config JSON could not be read * @throws IOException if the config JSON could not be read
*/ */
public static Config readConfig() throws IOException { public static Config readConfig() throws IOException {
System.out.println(Path.of("config.json"));
try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) { try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) {
assert in != null; assert in != null;
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8); Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
return new Gson().fromJson(reader, Config.class); return new Gson().fromJson(reader, Config.class);
} catch (IOException e) { } catch (IOException e) {
throw new IOException(""" throw new IOException("""
config.json was not found in the root folder (of the project), did you forget to create it ? config.json was not found in the root folder (of the project), did you forget to put it ?
Example structure: Example structure:
{ {
"token": "[your_bot_token_here]", "token": "[your_bot_token_here]"
"prefix": "!"
} }
""", e); """, e);
} }
@ -39,19 +39,17 @@ public class Config {
public String getToken() { public String getToken() {
return token; return token;
} }
public String getPrefix() {
return prefix;
}
public DBConfig getDbConfig() { public DBConfig getDbConfig() {
return mariadb; return dbConfig;
} }
public static class DBConfig { public static class DBConfig {
@SuppressWarnings("unused") private String host, user, password, database; @SuppressWarnings("unused") private String serverName, user, password, dbName;
@SuppressWarnings("unused") private int portNumber;
public String getHost() { public String getServerName() {
return host; return serverName;
} }
public String getUser() { public String getUser() {
@ -62,8 +60,12 @@ public class Config {
return password; return password;
} }
public String getDatabase() { public String getDbName() {
return database; return dbName;
}
public int getPortNumber() {
return portNumber;
} }
} }
} }

View file

@ -6,7 +6,6 @@ import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.GatewayIntent;
import net.hypr.doki.listeners.LevellingListener;
import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSource;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -19,21 +18,21 @@ public class Doki {
private static String prefix; private static String prefix;
private static final BasicDataSource dataSource = new BasicDataSource(); private static final BasicDataSource dataSource = new BasicDataSource();
private Doki(JDA jda, Config config) { private Doki(JDA jda, Config config) {
Doki.jda = jda; Doki.jda = jda;
Doki.config = config; Doki.config = config;
Doki.prefix = config.getPrefix();
} }
@SuppressWarnings("InstantiationOfUtilityClass") public JDA getJDA() {
private static void start() throws IOException, InterruptedException { return jda;
}
public static Doki start() throws IOException, InterruptedException {
config = Config.readConfig(); config = Config.readConfig();
final JDA jda = JDABuilder.createLight(config.getToken()) final JDA jda = JDABuilder.createLight(config.getToken())
.setActivity(Activity.customStatus("Banned from everywhere")) .setActivity(Activity.customStatus("Banned from everywhere"))
.enableIntents(GatewayIntent.MESSAGE_CONTENT) .enableIntents(GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(new LevellingListener())
.build() .build()
.awaitReady(); .awaitReady();
@ -43,34 +42,16 @@ public class Doki {
for (Guild guild : jda.getGuildCache()) { for (Guild guild : jda.getGuildCache()) {
LOGGER.info("\t- {} ({})", guild.getName(), guild.getId()); LOGGER.info("\t- {} ({})", guild.getName(), guild.getId());
} }
LOGGER.info("Registered prefix: " + config.getPrefix());
Config.DBConfig dbConfig = config.getDbConfig(); return new Doki(jda, config);
dataSource.setDriverClassName("org.mariadb.jdbc.Driver");
dataSource.setUrl("jdbc:mariadb://" + dbConfig.getHost() + "/" + dbConfig.getDatabase());
dataSource.setUsername(dbConfig.getUser());
dataSource.setPassword(dbConfig.getPassword());
dataSource.setMaxTotal(15);
dataSource.setMaxIdle(5);
dataSource.setMinIdle(2);
dataSource.setInitialSize(20);
new Doki(jda, config);
} }
public static BasicDataSource getDataSource() { return dataSource; }
public static JDA getJDA() { return jda; }
public static String getPrefix() { return prefix; }
public static void main(String[] args) { public static void main(String[] args) {
try { try {
start(); jda = start().getJDA();
jda = getJDA();
CommandsBuilder.newBuilder(437970062922612737L) CommandsBuilder.newBuilder(437970062922612737L)
.textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix(config.getPrefix())) .textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix("oki!"))
.build(jda, "net.hypr.doki.commands"); .build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Unable to start the bot", e); LOGGER.error("Unable to start the bot", e);
System.exit(-1); System.exit(-1);