From eb59865c7616f2892570a682ef6f2cb29b16b449 Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Sat, 24 Aug 2024 15:46:55 +0300 Subject: [PATCH] Modify ban appeal embed colours depending on appeal state Closes #18 --- .../camelot/config/module/BanAppeals.groovy | 24 +++++++++++++++++++ .../camelot/module/BanAppealModule.java | 24 ++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/config/src/main/groovy/net/neoforged/camelot/config/module/BanAppeals.groovy b/config/src/main/groovy/net/neoforged/camelot/config/module/BanAppeals.groovy index 9cc406d..3485ab0 100644 --- a/config/src/main/groovy/net/neoforged/camelot/config/module/BanAppeals.groovy +++ b/config/src/main/groovy/net/neoforged/camelot/config/module/BanAppeals.groovy @@ -6,6 +6,8 @@ import net.neoforged.camelot.config.ConfigUtils import net.neoforged.camelot.config.MailConfiguration import net.neoforged.camelot.config.OAuthConfiguration +import java.awt.Color + /** * Module for ban appeals. * @@ -58,4 +60,26 @@ class BanAppeals extends ModuleConfiguration { * The amount of days in which the user is expected to get a response to their appeal. */ int responseTime = 7 + + /** + * Configuration for ban appeal colours. + */ + final Colors colors = new Colors() + + /** + * Configure the ban appeal colours. + */ + void colors(@DelegatesTo(value = Colors, strategy = Closure.DELEGATE_FIRST) Closure config) { + ConfigUtils.configure(colors, config) + } + + /** + * Configuration for ban appeal colours. + */ + static class Colors { + int approved = Color.GREEN.getRGB() + int rejected = Color.RED.getRGB() + int pendingReply = Color.GRAY.getRGB() + int ongoing = Color.YELLOW.getRGB() + } } diff --git a/src/main/java/net/neoforged/camelot/module/BanAppealModule.java b/src/main/java/net/neoforged/camelot/module/BanAppealModule.java index b5328da..ecca9b4 100644 --- a/src/main/java/net/neoforged/camelot/module/BanAppealModule.java +++ b/src/main/java/net/neoforged/camelot/module/BanAppealModule.java @@ -13,6 +13,7 @@ import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.UserSnowflake; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; @@ -204,7 +205,9 @@ private void onSubmitFollowup(Context context) throws Exception { guild.getChannelById(GuildMessageChannel.class, config().getAppealsChannels().get(guild.getIdLong())).retrieveMessageById(existing.threadId()) .map(Message::getStartedThread) - .flatMap(thread -> thread.sendMessageEmbeds(embed.build())) + .flatMap(thread -> thread.sendMessageEmbeds(embed.build()) + .and(thread.retrieveParentMessage() + .flatMap(msg -> msg.editMessageEmbeds(modifyColour(msg.getEmbeds().getFirst(), config().getColors().getOngoing()))))) .queue(); Database.appeals().useExtension(BanAppealsDAO.class, db -> db.setFollowup( @@ -299,7 +302,7 @@ private void onSubmitAppeal(Context context) throws Exception { .setTimestamp(Instant.now()) .setDescription("User appealed their ban for **" + Objects.requireNonNullElse(possibleBan.get().getReason(), "No reason given").replace("rec: ", "") + "**:\n") .appendDescription(payload.getString("reason")) - .setColor(Color.CYAN); + .setColor(config().getColors().getOngoing()); if (!payload.isNull("feedback") && !payload.getString("feedback").isBlank()) { if (payload.getString("feedback").length() > 1000) { @@ -354,11 +357,18 @@ private void onModal(ModalInteractionEvent event) { final var guild = event.getGuild(); assert guild != null; + final ThreadChannel thread = event.getMessage().getStartedThread(); + assert thread != null; + switch (split[1]) { case "followup" -> { final String replyText = event.getValue("reply").getAsString(); Database.appeals().useExtension(BanAppealsDAO.class, db -> db.setFollowup(event.getGuild().getIdLong(), userId, replyText)); + thread.retrieveParentMessage() + .flatMap(msg -> msg.editMessageEmbeds(modifyColour(msg.getEmbeds().getFirst(), config().getColors().getPendingReply()))) + .queue(); + retrieveUser.onSuccess(user -> sendMailFromServer(appeal.email(), user, guild, "Ban appeal follow-up", pre(text("The moderators of "), b(event.getGuild().getName()), text(" have sent you a message:")), pre(code(replyText)), @@ -386,9 +396,6 @@ private void onModal(ModalInteractionEvent event) { final Instant until = Instant.now().plus(blockDays, ChronoUnit.DAYS); Database.appeals().useExtension(BanAppealsDAO.class, db -> db.blockUntil(guild.getIdLong(), userId, "Previous appeal denied: " + reason, until.toEpochMilli())); - final ThreadChannel thread = event.getMessage().getStartedThread(); - assert thread != null; - event.deferReply(true) .flatMap(_ -> thread.sendMessage("Ban appeal **rejected** by " + event.getUser().getAsMention() + ". Reason: **" + reason + "**.")) .flatMap(_ -> closeAppeal(thread, true)) @@ -463,7 +470,8 @@ private void onButton(ButtonInteractionEvent event) { private RestAction closeAppeal(ThreadChannel thread, boolean rejected) { return thread.retrieveParentMessage() .flatMap(msg -> msg.editMessageComponents(List.of()) - .setContent(rejected ? "Appeal rejected" : "Appeal approved")) + .setContent(rejected ? "Appeal rejected" : "Appeal approved") + .setEmbeds(modifyColour(msg.getEmbeds().getFirst(), rejected ? config().getColors().getRejected() : config().getColors().getApproved()))) .flatMap(_ -> thread.getManager().setArchived(true)); } @@ -691,4 +699,8 @@ private static String getAvatar(JSONObject author, String discriminator) { } return String.format("https://cdn.discordapp.com/avatars/%s/%s.%s", id, avatar, avatar.startsWith("a_") ? "gif" : "png"); } + + private static MessageEmbed modifyColour(MessageEmbed embed, int colour) { + return new EmbedBuilder(embed).setColor(colour).build(); + } }