Skip to content

Commit

Permalink
debug webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed May 28, 2024
1 parent 3dae03e commit 9720c4f
Show file tree
Hide file tree
Showing 32 changed files with 158 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import java.util.Arrays;

public class Bot {
public class DiscordApp {
public static final Logger LOGGER = LoggerFactory.getLogger("Kitsun");
public static Cached<Secrets> secrets = Cached.of("secrets", Secrets.class, () -> Secrets.DEFAULT);
public static JDA jda;

public static void main(String[] args) {
Bot.init();
DiscordApp.init();
KitsunHttpServer.init();
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ru/pinkgoosik/kitsun/api/FabricMeta.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.pinkgoosik.kitsun.api;

import ru.pinkgoosik.kitsun.feature.KitsunDebugger;
import ru.pinkgoosik.kitsun.debug.KitsunDebugWebhook;
import ru.pinkgoosik.kitsun.util.UrlParser;

import java.util.ArrayList;
Expand All @@ -16,7 +16,7 @@ public static Optional<ArrayList<Loader>> getFabricVersions() {
return Optional.of(new ArrayList<>(List.of(versions)));
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse fabric versions due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse fabric versions due to an exception:\n" + e);
}
return Optional.empty();
}
Expand All @@ -27,7 +27,7 @@ public static Optional<ArrayList<Entry>> getFabricVersions(String gameVersion) {
return Optional.of(new ArrayList<>(List.of(entries)));
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse fabric versions for game version " + gameVersion + " due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse fabric versions for game version " + gameVersion + " due to an exception:\n" + e);
}
return Optional.empty();
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/ru/pinkgoosik/kitsun/api/Modrinth.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import masecla.modrinth4j.model.project.Project;
import masecla.modrinth4j.model.user.ModrinthUser;
import masecla.modrinth4j.model.version.ProjectVersion;
import ru.pinkgoosik.kitsun.Bot;
import ru.pinkgoosik.kitsun.feature.KitsunDebugger;
import ru.pinkgoosik.kitsun.DiscordApp;
import ru.pinkgoosik.kitsun.debug.KitsunDebugWebhook;

import java.util.ArrayList;
import java.util.Optional;
Expand All @@ -16,14 +16,14 @@ public class Modrinth {
public static final String PROJECT_URL = "https://modrinth.com/project/%slug%";
public static final ModrinthAPI API = ModrinthAPI.rateLimited(
UserAgent.builder().authorUsername("tyap-lyap").projectName("kitsun").projectVersion("latest").build(),
Bot.secrets.get().modrinthApiKey);
DiscordApp.secrets.get().modrinthApiKey);

public static Optional<Project> getProject(String project) {
try {
return Optional.of(API.projects().get(project).join());
}
catch (Exception e) {
KitsunDebugger.report("Failed to get modrinth project " + project + " due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to get modrinth project " + project + " due to an exception:\n" + e);
}
return Optional.empty();
}
Expand All @@ -33,7 +33,7 @@ public static Optional<ArrayList<ProjectVersion>> getVersions(String project) {
return Optional.of(new ArrayList<>(API.versions().getProjectVersions(project, GetProjectVersions.GetProjectVersionsRequest.builder().build()).get()));
}
catch (Exception e) {
KitsunDebugger.report("Failed to get modrinth project " + project + " versions due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to get modrinth project " + project + " versions due to an exception:\n" + e);
}
return Optional.empty();
}
Expand All @@ -43,7 +43,7 @@ public static Optional<ModrinthUser> getUser(String id) {
return Optional.of(API.users().getUser(id).join());
}
catch (Exception e) {
KitsunDebugger.report("Failed to get modrinth user " + id + " due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to get modrinth user " + id + " due to an exception:\n" + e);
}
return Optional.empty();
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/ru/pinkgoosik/kitsun/api/QuiltMeta.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.pinkgoosik.kitsun.api;

import ru.pinkgoosik.kitsun.feature.KitsunDebugger;
import ru.pinkgoosik.kitsun.debug.KitsunDebugWebhook;
import ru.pinkgoosik.kitsun.util.UrlParser;

import java.util.ArrayList;
Expand All @@ -17,7 +17,7 @@ public static Optional<ArrayList<QuiltVersion>> getQuiltVersions() {
return Optional.of(new ArrayList<>(List.of(versions)));
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse quilt versions due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse quilt versions due to an exception:\n" + e);
}
return Optional.empty();
}
Expand All @@ -28,7 +28,7 @@ public static Optional<ArrayList<Entry>> getQuiltVersions(String gameVersion) {
return Optional.of(new ArrayList<>(List.of(entries)));
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse quilt versions for game version " + gameVersion + " due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse quilt versions for game version " + gameVersion + " due to an exception:\n" + e);
}
return Optional.empty();
}
Expand All @@ -39,7 +39,7 @@ public static Optional<ArrayList<QuiltMappingsVersion>> getQuiltMappingsVersions
return Optional.of(new ArrayList<>(List.of(versions)));
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse quilt mappings versions for game version " + gameVersion + " due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse quilt mappings versions for game version " + gameVersion + " due to an exception:\n" + e);
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ru.pinkgoosik.kitsun.Bot;
import ru.pinkgoosik.kitsun.DiscordApp;
import ru.pinkgoosik.kitsun.api.curseforge.entity.CurseForgeMod;
import ru.pinkgoosik.kitsun.feature.KitsunDebugger;
import ru.pinkgoosik.kitsun.debug.KitsunDebugWebhook;

import java.io.FileNotFoundException;
import java.io.InputStream;
Expand All @@ -26,7 +26,7 @@ public static Optional<CurseForgeMod> getMod(String id) {
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(new URI(API_MOD_URL.replace("%id%", id)))
.headers("x-api-key", Bot.secrets.get().curseforgeApiKey)
.headers("x-api-key", DiscordApp.secrets.get().curseforgeApiKey)
.build();

HttpResponse<InputStream> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofInputStream());
Expand All @@ -38,7 +38,7 @@ public static Optional<CurseForgeMod> getMod(String id) {
return Optional.empty();
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse curseforge mod " + id + " due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse curseforge mod " + id + " due to an exception:\n" + e);
}
return Optional.empty();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ru/pinkgoosik/kitsun/api/mojang/MojangAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import ru.pinkgoosik.kitsun.feature.KitsunDebugger;
import ru.pinkgoosik.kitsun.debug.KitsunDebugWebhook;
import ru.pinkgoosik.kitsun.util.UrlParser;

import java.io.InputStreamReader;
Expand All @@ -26,7 +26,7 @@ public static Optional<VersionManifest> getManifest() {
return Optional.empty();
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse minecraft versions manifest due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse minecraft versions manifest due to an exception:\n" + e);
}
return Optional.empty();
}
Expand Down Expand Up @@ -61,7 +61,7 @@ public static Optional<String> getUuid(String nickname) {
return Optional.of(fromTrimmed(element.getAsJsonObject().get("id").getAsString()));
}
catch(Exception e) {
KitsunDebugger.report("Failed to parse " + nickname + "'s UUID due to an exception:\n" + e);
KitsunDebugWebhook.report("Failed to parse " + nickname + "'s UUID due to an exception:\n" + e);
return Optional.empty();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ru/pinkgoosik/kitsun/api/mojang/PatchNotes.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ru.pinkgoosik.kitsun.api.mojang;

import com.google.gson.*;
import ru.pinkgoosik.kitsun.Bot;
import ru.pinkgoosik.kitsun.DiscordApp;

import java.io.InputStreamReader;
import java.net.URL;
Expand All @@ -25,7 +25,7 @@ public static Optional<PatchNotesEntry> getEntry(String version) {
}
}
catch(Exception e) {
Bot.LOGGER.error("Failed to parse patch note entry due to an exception:\n" + e);
DiscordApp.LOGGER.error("Failed to parse patch note entry due to an exception:\n" + e);
}
return Optional.empty();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/ru/pinkgoosik/kitsun/cache/Cached.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ru.pinkgoosik.kitsun.Bot;
import ru.pinkgoosik.kitsun.feature.KitsunDebugger;
import ru.pinkgoosik.kitsun.DiscordApp;
import ru.pinkgoosik.kitsun.debug.KitsunDebugWebhook;
import ru.pinkgoosik.kitsun.util.FileUtils;

import java.io.BufferedReader;
Expand Down Expand Up @@ -44,14 +44,14 @@ public T read(Class<T> type) {
return GSON.fromJson(reader, type);
}
catch(FileNotFoundException e) {
Bot.LOGGER.info("File " + filePath + " is not found! Setting to default.");
DiscordApp.LOGGER.info("File " + filePath + " is not found! Setting to default.");
var dat = defaultBuilder.create();
this.data = dat;
this.save();
return dat;
}
catch(Exception e) {
Bot.LOGGER.error("Failed to read cached data due to an exception:\n" + e + "\n Setting to default.");
DiscordApp.LOGGER.error("Failed to read cached data due to an exception:\n" + e + "\n Setting to default.");
return defaultBuilder.create();
}
}
Expand All @@ -65,7 +65,7 @@ public void save() {
}
}
catch(Exception e) {
KitsunDebugger.ping("Failed to save cached data due to an exception:\n" + e);
KitsunDebugWebhook.ping("Failed to save cached data due to an exception:\n" + e);
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import ru.pinkgoosik.kitsun.Bot;
import ru.pinkgoosik.kitsun.DiscordApp;
import ru.pinkgoosik.kitsun.command.member.*;
import ru.pinkgoosik.kitsun.command.admin.*;

Expand Down Expand Up @@ -51,8 +51,8 @@ public static void onConnect() {
}
});

Bot.jda.updateCommands().addCommands(globalCommands).queue();
var guild = Bot.getGuild("854349856164020244");
DiscordApp.jda.updateCommands().addCommands(globalCommands).queue();
var guild = DiscordApp.getGuild("854349856164020244");

if(guild != null) {
guild.updateCommands().addCommands(tlCommands).queue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import ru.pinkgoosik.kitsun.Bot;
import ru.pinkgoosik.kitsun.DiscordApp;
import ru.pinkgoosik.kitsun.cache.ServerData;
import ru.pinkgoosik.kitsun.command.CommandHelper;
import ru.pinkgoosik.kitsun.command.KitsunCommand;
Expand Down Expand Up @@ -48,7 +48,7 @@ public void respond(SlashCommandInteractionEvent ctx, CommandHelper helper) {
if(channel instanceof VoiceChannel vc) {
var category = vc.getParentCategory();
if(category != null) {
var perms = category.getPermissionOverride(Objects.requireNonNull(Objects.requireNonNull(Bot.jda.getGuildById(dat.server)).getMemberById(Bot.jda.getSelfUser().getId())));
var perms = category.getPermissionOverride(Objects.requireNonNull(Objects.requireNonNull(DiscordApp.jda.getGuildById(dat.server)).getMemberById(DiscordApp.jda.getSelfUser().getId())));
if(perms != null && !perms.getAllowed().contains(Permission.ADMINISTRATOR)) {
helper.ephemeral(Embeds.error("Bot doesn't have permission of administrator! It required for auto channels to work properly."));
return;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ru/pinkgoosik/kitsun/config/Secrets.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Secrets {
public String githubToken = "";
public String note = "";
public String activity = "";
public String debugWebhook = "";
public HttpConfig http = new HttpConfig();

public static final Secrets DEFAULT = new Secrets();
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/ru/pinkgoosik/kitsun/debug/KitsunDebugWebhook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.pinkgoosik.kitsun.debug;

import ru.pinkgoosik.kitsun.DiscordApp;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;

public class KitsunDebugWebhook {
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder().build();

public static void ping(String message) {
DiscordApp.LOGGER.error(message);
sendMessage("<@287598520268095488>\n" + message);
}

public static void report(String message) {
DiscordApp.LOGGER.error(message);
sendMessage(message);
}

public static void info(String text) {
DiscordApp.LOGGER.info(text);
sendMessage(text);
}

private static void sendMessage(String content) {
if(DiscordApp.secrets.get().debugWebhook.isBlank()) return;

try {
byte[] out = ("{\"content\":\"" + content + "\"}").getBytes(StandardCharsets.UTF_8);
int length = out.length;

HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofByteArray(out))
.uri(new URI(DiscordApp.secrets.get().debugWebhook))
.header("Content-Type", "application/json")
.build();

HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofString());
}
catch (Exception e) {
DiscordApp.LOGGER.info("Failed to execute webhook due to an exception: " + e);
}
}

}
Loading

0 comments on commit 9720c4f

Please sign in to comment.