🔨 Refracto libs

This commit is contained in:
SebClem 2022-06-23 15:30:26 +02:00
parent 545a99a02c
commit da976ff541
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
5 changed files with 52 additions and 42 deletions

View File

@ -4,6 +4,7 @@ plugins {
id 'java' id 'java'
id 'groovy' id 'groovy'
id 'org.liquibase.gradle' version '2.0.4' id 'org.liquibase.gradle' version '2.0.4'
id "nebula.lint" version "16.9.0"
} }
@ -35,41 +36,33 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-log4j2") implementation("org.springframework.boot:spring-boot-starter-log4j2")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client") implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springdoc:springdoc-openapi-ui:1.6.8") implementation('org.springdoc:springdoc-openapi-ui:1.6.9')
implementation("org.springdoc:springdoc-openapi-security:1.6.8") implementation('org.springdoc:springdoc-openapi-security:1.6.9')
implementation('org.liquibase:liquibase-core') implementation('org.liquibase:liquibase-core')
implementation('io.jsonwebtoken:jjwt-api:0.11.5') implementation('io.jsonwebtoken:jjwt-api:0.11.5')
implementation('io.jsonwebtoken:jjwt-impl:0.11.5') implementation('io.jsonwebtoken:jjwt-impl:0.11.5')
implementation('io.jsonwebtoken:jjwt-gson:0.11.5') implementation('io.jsonwebtoken:jjwt-jackson:0.11.5')
implementation 'org.codehaus.groovy:groovy-all:3.0.8'
implementation 'com.sedmelluq:lavaplayer:1.3.77' implementation 'com.sedmelluq:lavaplayer:1.3.77'
implementation 'net.dv8tion:JDA:4.4.0_350' implementation 'net.dv8tion:JDA:4.4.0_350'
implementation group: 'org.json', name: 'json', version: '20210307'
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
implementation(platform("org.apache.logging.log4j:log4j-bom:2.17.1")) implementation(platform("org.apache.logging.log4j:log4j-bom:2.17.1"))
implementation group: 'org.hibernate', name: 'hibernate-validator', version: '7.0.4.Final' implementation group: 'org.hibernate', name: 'hibernate-validator', version: '7.0.4.Final'
// Use MySQL Connector-J // Use MySQL Connector-J
implementation 'mysql:mysql-connector-java' implementation 'mysql:mysql-connector-java:8.0.29'
implementation 'org.reflections:reflections:0.9.12' implementation 'org.reflections:reflections:0.10.2'
implementation 'org.apache.commons:commons-lang3:3.12.0' implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.google.api-client:google-api-client:1.31.5'
implementation 'com.google.apis:google-api-services-youtube:v3-rev20210410-1.31.0'
implementation group: 'org.jsoup', name: 'jsoup', version: '1.13.1'
liquibaseRuntime 'org.liquibase:liquibase-core:4.2.2' liquibaseRuntime 'org.liquibase:liquibase-core:4.8.0'
liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.1.1' liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:3.0.2'
liquibaseRuntime 'mysql:mysql-connector-java:5.1.34' liquibaseRuntime 'mysql:mysql-connector-java:8.0.29'
liquibaseRuntime group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1' liquibaseRuntime group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
liquibaseRuntime group: 'org.liquibase.ext', name: 'liquibase-hibernate5', version: '4.10.0' liquibaseRuntime group: 'org.liquibase.ext', name: 'liquibase-hibernate5', version: '4.10.0'
liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa' liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa'

View File

@ -1,6 +1,8 @@
package net.Broken.SlashCommands; package net.Broken.SlashCommands;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.Broken.SlashCommand; import net.Broken.SlashCommand;
import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.EmbedMessageUtils;
import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.MessageBuilder;
@ -9,7 +11,6 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -17,6 +18,7 @@ import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List; import java.util.List;
/** /**
@ -38,9 +40,11 @@ public class Cat implements SlashCommand {
a.append(inputLine); a.append(inputLine);
in.close(); in.close();
JSONObject json = new JSONObject(a.toString()); TypeReference<HashMap<String, String>> typeRef = new TypeReference<>() {};
ObjectMapper mapper = new ObjectMapper();
HashMap<String, String> json = mapper.readValue(a.toString(), typeRef);
event.reply(json.getString("file")).queue(); event.reply(json.get("file")).queue();
} catch (IOException e) { } catch (IOException e) {
logger.catching(e); logger.catching(e);

View File

@ -4,7 +4,7 @@ import net.Broken.SlashCommand;
import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.FindContentOnWebPage; import net.Broken.Tools.FindContentOnWebPage;
import net.Broken.Tools.LimitChecker; import net.Broken.Tools.LimitChecker;
import net.Broken.Tools.TrueRandom; import net.Broken.Tools.Random.TrueRandom;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.OptionData;
@ -75,7 +75,7 @@ public abstract class NumberedSlashCommand implements SlashCommand {
private void fillRandomQueue() throws IOException { private void fillRandomQueue() throws IOException {
TrueRandom trueRandom = TrueRandom.getINSTANCE(); TrueRandom trueRandom = TrueRandom.getINSTANCE();
ArrayList<Integer> numbers = trueRandom.getNumbers(minNumber, maxNumber); List<Integer> numbers = trueRandom.getNumbers(minNumber, maxNumber);
randomQueue.addAll(numbers); randomQueue.addAll(numbers);

View File

@ -0,0 +1,18 @@
package net.Broken.Tools.Random.Data;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
public record RandomData(String jsonrpc, String method, int id, Params params, Error error, Result result) {
public record Params(String apiKey, int n, int min, int max, boolean replacement) {
}
public record Error(long code, String message, Object data){};
public record Result(Random random, String bitsUsed, String bitsLeft, String requestsLeft, String advisoryDelay){
public record Random(List<Integer> data, String completionTime){};
}
}

View File

@ -1,7 +1,9 @@
package net.Broken.Tools; package net.Broken.Tools.Random;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.Broken.BotConfigLoader; import net.Broken.BotConfigLoader;
import net.Broken.SpringContext; import net.Broken.SpringContext;
import net.Broken.Tools.Random.Data.RandomData;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
@ -11,12 +13,10 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TrueRandom { public class TrueRandom {
@ -32,13 +32,15 @@ public class TrueRandom {
return INSTANCE; return INSTANCE;
} }
public ArrayList<Integer> getNumbers(int min, int max) throws IOException { public List<Integer> getNumbers(int min, int max) throws IOException {
// TODO Migrate to native http client // TODO Migrate to native http client
HttpClient httpClient = HttpClientBuilder.create().build(); HttpClient httpClient = HttpClientBuilder.create().build();
String postVal = "{\"jsonrpc\":\"2.0\",\"method\":\"generateIntegers\",\"params\":{\"apiKey\":\"" + apiKey + "\",\"n\":50,\"min\":" + min + ",\"max\":" + max + ",\"replacement\":" + (((max - min) >= 50) ? "false" : "true") + "},\"id\":41}"; RandomData postData = new RandomData("2.0", "generateIntegers", 41, new RandomData.Params(apiKey, 50, min, max, (max - min) < 50), null, null);
StringEntity entity = new StringEntity(postVal, ContentType.APPLICATION_JSON); ObjectMapper mapper = new ObjectMapper();
StringEntity entity = new StringEntity(mapper.writeValueAsString(postData), ContentType.APPLICATION_JSON);
String url = "https://api.random.org/json-rpc/2/invoke"; String url = "https://api.random.org/json-rpc/2/invoke";
HttpPost request = new HttpPost(url); HttpPost request = new HttpPost(url);
request.setEntity(entity); request.setEntity(entity);
@ -56,28 +58,21 @@ public class TrueRandom {
InputStream responseIS = response.getEntity().getContent(); InputStream responseIS = response.getEntity().getContent();
String content = IOUtils.toString(responseIS, StandardCharsets.UTF_8); String content = IOUtils.toString(responseIS, StandardCharsets.UTF_8);
logger.trace(content);
JSONObject json = new JSONObject(content); RandomData responseData = mapper.readValue(content, RandomData.class);
if (json.keySet().contains("error")) {
if (responseData.error() != null) {
logger.error("Request fail!"); logger.error("Request fail!");
logger.error("Request : " + postVal); logger.error("Response : " + responseData.error().message());
logger.error("Response : " + content);
throw new IOException(); throw new IOException();
} }
logger.debug("Request left: " + json.getJSONObject("result").getInt("requestsLeft")); logger.debug("Request left: " + responseData.result().requestsLeft());
logger.debug("Bits left: " + json.getJSONObject("result").getInt("bitsLeft")); logger.debug("Bits left: " + responseData.result().bitsLeft());
logger.debug("Numbers: " + json.getJSONObject("result").getJSONObject("random").getJSONArray("data")); logger.debug("Numbers: " + responseData.result().random().data());
List<Object> numbers = json.getJSONObject("result").getJSONObject("random").getJSONArray("data").toList(); return responseData.result().random().data();
ArrayList<Integer> converted = new ArrayList<>();
for (Object nbr : numbers) {
converted.add((Integer) nbr);
}
return converted;
} }