From c2da94a0d539afeaab6fc072942aa91eb45dc32e Mon Sep 17 00:00:00 2001 From: rtm516 Date: Thu, 18 Apr 2024 16:50:00 +0100 Subject: [PATCH] Update preview handler to use unicode and gh api --- .../discordbot/listeners/PreviewHandler.java | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/geysermc/discordbot/listeners/PreviewHandler.java b/src/main/java/org/geysermc/discordbot/listeners/PreviewHandler.java index 5b8799cb..88578f8c 100644 --- a/src/main/java/org/geysermc/discordbot/listeners/PreviewHandler.java +++ b/src/main/java/org/geysermc/discordbot/listeners/PreviewHandler.java @@ -1,3 +1,28 @@ +/* + * Copyright (c) 2024 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/GeyserDiscordBot + */ + package org.geysermc.discordbot.listeners; import net.dv8tion.jda.api.EmbedBuilder; @@ -8,13 +33,14 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.utils.messages.MessageCreateData; +import org.geysermc.discordbot.GeyserBot; import org.geysermc.discordbot.storage.ServerSettings; import org.geysermc.discordbot.util.BotColors; -import org.json.JSONObject; -import pw.chew.chewbotcca.util.RestClient; +import org.kohsuke.github.GHPullRequest; import javax.annotation.Nonnull; +import java.io.IOException; import java.time.Instant; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,19 +76,18 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { String repo = matcher.group(1); String pr = matcher.group(2); - RestClient.RestResponse restResponse = RestClient - .getJsonObject("https://api.github.com/repos/GeyserMC/" + repo + "/pulls/" + pr); - JSONObject serverResponse = restResponse.body(); - - if (serverResponse.has("message")) { + GHPullRequest pullRequest; + try { + pullRequest = GeyserBot.getGithub().getRepository("GeyserMC/" + repo).getPullRequest(Integer.parseInt(pr)); + } catch (IOException e) { // The linked PR does not exist return; } previewChannel.createForumPost( - serverResponse.getString("title"), + pullRequest.getTitle(), MessageCreateData.fromEmbeds(new EmbedBuilder() - .setTitle(serverResponse.getString("title")) + .setTitle(pullRequest.getTitle()) .setColor(BotColors.SUCCESS.getColor()) .setDescription(event.getMessage().getContentRaw()) .setAuthor(event.getAuthor().getEffectiveName(), null, @@ -71,12 +96,12 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { .setTimestamp(Instant.now()) .build())) .addActionRow( - Button.link(serverResponse.getString("html_url"), "Discuss on GitHub") - .withEmoji(Emoji.fromFormatted("💬")), - Button.link(serverResponse.getString("html_url") + "/files", "View Changes") - .withEmoji(Emoji.fromFormatted("📝")), - Button.link(serverResponse.getString("html_url") + "/checks", "Download Artifacts") - .withEmoji(Emoji.fromFormatted("📦"))) + Button.link(String.valueOf(pullRequest.getHtmlUrl()), "Discuss on GitHub") + .withEmoji(Emoji.fromUnicode("\ud83d\udcac")), + Button.link(pullRequest.getHtmlUrl() + "/files", "View Changes") + .withEmoji(Emoji.fromUnicode("\ud83d\udcdd")), + Button.link(pullRequest.getHtmlUrl() + "/checks", "Download Artifacts") + .withEmoji(Emoji.fromUnicode("\ud83d\udce6"))) .queue(forumPost -> { // Reply to the original message with the link to the forum post event.getMessage().replyEmbeds(new EmbedBuilder()