Skip to content

Commit

Permalink
improve mod update webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed May 25, 2024
1 parent 9905919 commit 292815e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
24 changes: 12 additions & 12 deletions src/main/java/ru/pinkgoosik/kitsun/feature/ModUpdatesPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ private boolean updated(ArrayList<ProjectVersion> versions) {
private void publish(ArrayList<ProjectVersion> 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);
});
}
latestVersion = modVersion.getId();
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) {
Expand All @@ -84,35 +84,35 @@ 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);

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();
}
Expand Down
40 changes: 24 additions & 16 deletions src/main/java/ru/pinkgoosik/kitsun/http/ModUpdateWebhook.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {}
}
}

Expand Down

0 comments on commit 292815e

Please sign in to comment.