Skip to content

Commit

Permalink
Modify ban appeal embed colours depending on appeal state
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
Matyrobbrt committed Aug 24, 2024
1 parent d14fb4e commit eb59865
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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()
}
}
24 changes: 18 additions & 6 deletions src/main/java/net/neoforged/camelot/module/BanAppealModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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();
}
}

0 comments on commit eb59865

Please sign in to comment.