Skip to content

Commit

Permalink
Update preview handler to use unicode and gh api
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Apr 18, 2024
1 parent d5c355d commit c2da94a
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions src/main/java/org/geysermc/discordbot/listeners/PreviewHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -50,19 +76,18 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
String repo = matcher.group(1);
String pr = matcher.group(2);

RestClient.RestResponse<JSONObject> 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,
Expand All @@ -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()
Expand Down

0 comments on commit c2da94a

Please sign in to comment.