-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Previews channel support Signed-off-by: Joshua Castle <[email protected]> * Order settings command list Signed-off-by: Joshua Castle <[email protected]> * Address review Signed-off-by: Joshua Castle <[email protected]> --------- Signed-off-by: Joshua Castle <[email protected]>
- Loading branch information
Showing
6 changed files
with
157 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/org/geysermc/discordbot/listeners/PreviewHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.geysermc.discordbot.listeners; | ||
|
||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel; | ||
import net.dv8tion.jda.api.entities.channel.concrete.NewsChannel; | ||
import net.dv8tion.jda.api.entities.emoji.Emoji; | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
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.storage.ServerSettings; | ||
import org.geysermc.discordbot.util.BotColors; | ||
import org.json.JSONObject; | ||
import pw.chew.chewbotcca.util.RestClient; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import java.time.Instant; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class PreviewHandler extends ListenerAdapter { | ||
private static final Pattern GH_PR_PATTERN = Pattern.compile("https://github\\.com/GeyserMC/(.+)/pull/(\\d+)"); | ||
|
||
@Override | ||
public void onMessageReceived(@Nonnull MessageReceivedEvent event) { | ||
if (event.getAuthor().isBot()) | ||
return; | ||
|
||
NewsChannel previewFeedsChannel = ServerSettings.getPreviewFeedsChannel(event.getGuild()); | ||
if (previewFeedsChannel == null) | ||
return; | ||
if (event.getChannel().getIdLong() != previewFeedsChannel.getIdLong()) | ||
return; | ||
|
||
// Create a post in the preview forum channel with this content | ||
ForumChannel previewChannel = ServerSettings.getPreviewChannel(event.getGuild()); | ||
if (previewChannel == null) | ||
return; | ||
|
||
// Check if the message contains a GitHub PR link and extract the repo and PR | ||
// number via matcher | ||
String content = event.getMessage().getContentRaw(); | ||
|
||
Matcher matcher = GH_PR_PATTERN.matcher(content); | ||
if (!matcher.find()) { | ||
return; | ||
} | ||
|
||
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")) { | ||
// The linked PR does not exist | ||
return; | ||
} | ||
|
||
previewChannel.createForumPost( | ||
serverResponse.getString("title"), | ||
MessageCreateData.fromEmbeds(new EmbedBuilder() | ||
.setTitle(serverResponse.getString("title")) | ||
.setColor(BotColors.SUCCESS.getColor()) | ||
.setDescription(event.getMessage().getContentRaw()) | ||
.setAuthor(event.getAuthor().getEffectiveName(), null, | ||
event.getAuthor().getEffectiveAvatarUrl()) | ||
.setImage("https://opengraph.githubassets.com/1/GeyserMC/" + repo + "/pull/" + pr) | ||
.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("📦"))) | ||
.queue(forumPost -> { | ||
// Reply to the original message with the link to the forum post | ||
event.getMessage().replyEmbeds(new EmbedBuilder() | ||
.setColor(BotColors.SUCCESS.getColor()) | ||
.setDescription("The above preview can be discussed in:\n### <#" | ||
+ forumPost.getMessage().getId() + ">") | ||
.setTimestamp(Instant.now()) | ||
.build()).queue(); | ||
}); | ||
|
||
// Remove embeds from the original message | ||
event.getMessage().suppressEmbeds(true).queue(); | ||
|
||
// Publish message | ||
event.getMessage().crosspost().queue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters