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">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

View file

@ -4,13 +4,13 @@ import com.google.gson.Gson;
import java.io.*;
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 will need a valid config.json in the package com.freya02.bot for this to work
public class Config {
@SuppressWarnings("unused") private String token;
@SuppressWarnings("unused") private String prefix;
@SuppressWarnings("unused") private DBConfig mariadb;
@SuppressWarnings("unused") private DBConfig dbConfig;
/**
* Returns the configuration object for this bot
@ -19,18 +19,18 @@ public class Config {
* @throws IOException if the config JSON could not be read
*/
public static Config readConfig() throws IOException {
System.out.println(Path.of("config.json"));
try (InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("config.json")) {
assert in != null;
Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8);
return new Gson().fromJson(reader, Config.class);
} catch (IOException e) {
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:
{
"token": "[your_bot_token_here]",
"prefix": "!"
"token": "[your_bot_token_here]"
}
""", e);
}
@ -39,19 +39,17 @@ public class Config {
public String getToken() {
return token;
}
public String getPrefix() {
return prefix;
}
public DBConfig getDbConfig() {
return mariadb;
return 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() {
return host;
public String getServerName() {
return serverName;
}
public String getUser() {
@ -62,8 +60,12 @@ public class Config {
return password;
}
public String getDatabase() {
return database;
public String getDbName() {
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.entities.*;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.hypr.doki.listeners.LevellingListener;
import org.apache.commons.dbcp2.BasicDataSource;
import org.slf4j.Logger;
@ -19,21 +18,21 @@ public class Doki {
private static String prefix;
private static final BasicDataSource dataSource = new BasicDataSource();
private Doki(JDA jda, Config config) {
Doki.jda = jda;
Doki.config = config;
Doki.prefix = config.getPrefix();
}
@SuppressWarnings("InstantiationOfUtilityClass")
private static void start() throws IOException, InterruptedException {
public JDA getJDA() {
return jda;
}
public static Doki start() throws IOException, InterruptedException {
config = Config.readConfig();
final JDA jda = JDABuilder.createLight(config.getToken())
.setActivity(Activity.customStatus("Banned from everywhere"))
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(new LevellingListener())
.build()
.awaitReady();
@ -43,34 +42,16 @@ public class Doki {
for (Guild guild : jda.getGuildCache()) {
LOGGER.info("\t- {} ({})", guild.getName(), guild.getId());
}
LOGGER.info("Registered prefix: " + config.getPrefix());
Config.DBConfig dbConfig = config.getDbConfig();
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);
return 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) {
try {
start();
jda = getJDA();
jda = start().getJDA();
CommandsBuilder.newBuilder(437970062922612737L)
.textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix(config.getPrefix()))
.build(jda, "net.hypr.doki.commands");
.textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix("oki!"))
.build(jda, "net.hypr.doki.commands"); //Registering listeners is taken care of by the lib
} catch (Exception e) {
LOGGER.error("Unable to start the bot", e);
System.exit(-1);