From 292815ebf1f4d8f2a08114b66d3e53ef2c074117 Mon Sep 17 00:00:00 2001 From: PinkGoosik Date: Sat, 25 May 2024 12:25:55 +0500 Subject: [PATCH] improve mod update webhook --- .../kitsun/feature/ModUpdatesPublisher.java | 24 +++++------ .../kitsun/http/ModUpdateWebhook.java | 40 +++++++++++-------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/main/java/ru/pinkgoosik/kitsun/feature/ModUpdatesPublisher.java b/src/main/java/ru/pinkgoosik/kitsun/feature/ModUpdatesPublisher.java index 3dcb986..f575033 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/feature/ModUpdatesPublisher.java +++ b/src/main/java/ru/pinkgoosik/kitsun/feature/ModUpdatesPublisher.java @@ -61,7 +61,7 @@ private boolean updated(ArrayList versions) { private void publish(ArrayList versions, long delay) { ProjectVersion modVersion = versions.get(0); if(Bot.jda.getGuildChannelById(channel) instanceof StandardGuildMessageChannel messageChannel) { - messageChannel.sendMessageEmbeds(createEmbed(modVersion)).queueAfter(delay, TimeUnit.SECONDS, message -> {},throwable -> { + messageChannel.sendMessageEmbeds(createEmbed(this.cachedProject, modVersion)).queueAfter(delay, TimeUnit.SECONDS, message -> {},throwable -> { KitsunDebugger.ping("Failed to send update message of the " + this.project + " project due to an exception:\n" + throwable); }); } @@ -69,7 +69,7 @@ private void publish(ArrayList versions, long delay) { ServerData.get(server).save(); } - private MessageEmbed createEmbed(ProjectVersion version) { + public static MessageEmbed createEmbed(Project project, ProjectVersion version) { String changelogPart = ""; if(!version.getChangelog().isBlank() && version.getChangelog().length() < 5000) { @@ -84,18 +84,18 @@ private MessageEmbed createEmbed(ProjectVersion version) { } String linksPart = ""; - if(cachedProject.getSourceUrl() != null) { - linksPart = linksPart + "\n[Source Code](" + cachedProject.getSourceUrl() + ")"; + if(project.getSourceUrl() != null) { + linksPart = linksPart + "\n[Source Code](" + project.getSourceUrl() + ")"; } - if(cachedProject.getIssuesUrl() != null) { + if(project.getIssuesUrl() != null) { if(!linksPart.isBlank()) linksPart = linksPart + " | "; else linksPart = linksPart + "\n"; - linksPart = linksPart + "[Issue Tracker](" + cachedProject.getIssuesUrl() + ")"; + linksPart = linksPart + "[Issue Tracker](" + project.getIssuesUrl() + ")"; } - if(cachedProject.getWikiUrl() != null) { + if(project.getWikiUrl() != null) { if(!linksPart.isBlank()) linksPart = linksPart + " | "; else linksPart = linksPart + "\n"; - linksPart = linksPart + "[Wiki](" + cachedProject.getWikiUrl() + ")"; + linksPart = linksPart + "[Wiki](" + project.getWikiUrl() + ")"; } String versionType = version.getVersionType().name().toLowerCase().substring(0, 1).toUpperCase() + version.getVersionType().name().toLowerCase().substring(1); String minecraftVersions = " for " + version.getGameVersions().get(0); @@ -103,16 +103,16 @@ private MessageEmbed createEmbed(ProjectVersion version) { if(version.getGameVersions().size() > 1) { minecraftVersions = minecraftVersions + " - " + version.getGameVersions().get(version.getGameVersions().size() - 1); } - String iconUrl = cachedProject.getIconUrl() != null ? cachedProject.getIconUrl() : "https://i.imgur.com/rM5bzkK.png"; + String iconUrl = project.getIconUrl() != null ? project.getIconUrl() : "https://i.imgur.com/rM5bzkK.png"; String versionNum = version.getVersionNumber(); return new EmbedBuilder() - .setAuthor(cachedProject.getTitle()) - .setTitle((versionNum.contains("+") ? versionNum.split("\\+")[0] : versionNum) + " " + versionType + minecraftVersions, Modrinth.getUrl(cachedProject)) + .setAuthor(project.getTitle()) + .setTitle((versionNum.contains("+") ? versionNum.split("\\+")[0] : versionNum) + " " + versionType + minecraftVersions, Modrinth.getUrl(project)) .setDescription(changelogPart + linksPart) .setColor(KitsunColors.getCyan().getRGB()) .setThumbnail(iconUrl) - .setFooter("Modrinth Project | " + cachedProject.getLicense().getName(), "https://cdn.discordapp.com/emojis/1040805093395673128.png") + .setFooter("Modrinth Project | " + project.getLicense().getName(), "https://cdn.discordapp.com/emojis/1040805093395673128.png") .setTimestamp(version.getDatePublished()) .build(); } diff --git a/src/main/java/ru/pinkgoosik/kitsun/http/ModUpdateWebhook.java b/src/main/java/ru/pinkgoosik/kitsun/http/ModUpdateWebhook.java index 91076f9..62c66d3 100644 --- a/src/main/java/ru/pinkgoosik/kitsun/http/ModUpdateWebhook.java +++ b/src/main/java/ru/pinkgoosik/kitsun/http/ModUpdateWebhook.java @@ -1,8 +1,12 @@ package ru.pinkgoosik.kitsun.http; import com.sun.net.httpserver.HttpExchange; +import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildMessageChannel; import ru.pinkgoosik.kitsun.Bot; -import ru.pinkgoosik.kitsun.cache.ServerData; +import ru.pinkgoosik.kitsun.api.Modrinth; +import ru.pinkgoosik.kitsun.feature.KitsunDebugger; +import ru.pinkgoosik.kitsun.feature.ModUpdatesPublisher; +import ru.pinkgoosik.kitsun.util.ChannelUtils; import ru.pinkgoosik.kitsun.util.ServerUtils; public class ModUpdateWebhook extends KitsunHttpHandler { @@ -12,26 +16,30 @@ public void handle(HttpExchange exchange) { var map = parseParams(exchange); if (map.containsKey("token") && Bot.secrets.get().http.token.equals(map.get("token"))) { - if(map.containsKey("server") && map.containsKey("project")) { + if(map.containsKey("server") && map.containsKey("project") && map.containsKey("channel")) { if (!ServerUtils.exist(map.get("server"))) { success(exchange, "Server not found"); return; } - var data = ServerData.get(map.get("server")); - - for(var publisher : data.modUpdates.get()) { - if(publisher.project.equals(map.get("project"))) { - success(exchange, "Success"); - try { - Thread.sleep(5 * 1000); - publisher.check(0); - } - catch (Exception ignored) {} - return; - } + + if(ChannelUtils.exist(map.get("server"), map.get("channel"))) { + success(exchange, "Channel not found"); + return; } - success(exchange, "Project not found"); - return; + + success(exchange, "Success"); + + try { + Thread.sleep(5 * 1000); + var project = Modrinth.getProject(map.get("project")); + var versions = Modrinth.getVersions(map.get("project")); + + if(project.isPresent() && versions.isPresent() && Bot.jda.getGuildChannelById(map.get("channel")) instanceof StandardGuildMessageChannel messageChannel) { + messageChannel.sendMessageEmbeds(ModUpdatesPublisher.createEmbed(project.get(), versions.get().get(0))).queue(message -> {}, throwable -> { + KitsunDebugger.ping("Failed to send update message of the " + project.get() + " project due to an exception:\n" + throwable); + }); + } + } catch (Exception ignored) {} } }