Add embed message support to Discord bot and update appeal flow to use embeds for Discord notifications

This commit is contained in:
2025-08-24 00:43:58 +02:00
parent ffddffa8dc
commit eab1c9322b
6 changed files with 187 additions and 95 deletions
@@ -74,6 +74,8 @@ public class AppealController implements AppealsApi {
log.error("Failed to send appeal {} to Discord", appeal.id(), e);
}
appealMail.sendAppealNotification(appeal, history);
CompletableFuture<Optional<EmailVerification>> emailVerificationCompletableFuture = new CompletableFuture<>();
Connection.getConnection(Databases.DEFAULT)
.runQuery(sqlSession -> {
@@ -9,7 +9,7 @@ import com.alttd.altitudeweb.database.litebans.HistoryType;
import com.alttd.altitudeweb.database.litebans.UserType;
import com.alttd.altitudeweb.database.web_db.forms.Appeal;
import com.alttd.altitudeweb.setup.Connection;
import com.alttd.webinterface.bot.DiscordSender;
import com.alttd.webinterface.send_message.DiscordSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -18,14 +18,13 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
@Slf4j
@Service
public class AppealDiscord {
private static final String OUTPUT_TYPE = "EVIDENCE";
private static final String OUTPUT_TYPE = "APPEAL";
public void sendAppealToDiscord(Appeal appeal, HistoryRecord history) {
// Fetch channels
@@ -58,14 +57,56 @@ public class AppealDiscord {
return;
}
String message = buildMessage(appeal, history, bans, mutes, warns, kicks);
// Build embed
boolean active = history.getUntil() == null || history.getUntil() <= 0 || history.getUntil() > System.currentTimeMillis();
String createdAt = formatInstant(appeal.createdAt());
List<DiscordSender.EmbedField> fields = new ArrayList<>();
// Group: User
fields.add(new DiscordSender.EmbedField(
"User",
"Username: " + safe(appeal.username()) + "\n" +
"UUID: " + safe(String.valueOf(appeal.uuid())) + "\n" +
"Email: " + safe(appeal.email()) + "\n" +
"Submitted: " + createdAt,
false
));
// Group: Punishment
fields.add(new DiscordSender.EmbedField(
"Punishment",
"Type: " + safe(String.valueOf(appeal.historyType())) + "\n" +
"ID: " + safe(String.valueOf(appeal.historyId())) + "\n" +
"Reason: " + safe(history.getReason()) + "\n" +
"Active: " + active,
false
));
// Group: Previous punishments
fields.add(new DiscordSender.EmbedField(
"Previous punishments",
"Bans: " + bans + "\n" +
"Mutes: " + mutes + "\n" +
"Warnings: " + warns + "\n" +
"Kicks: " + kicks,
true
));
String description = safe(appeal.reason());
List<Long> channelIds = channels.stream()
.map(OutputChannel::channel)
.filter(Objects::nonNull)
.toList();
DiscordSender.getInstance().sendMessageToChannels(channelIds, message);
// colorRgb = null (use default), timestamp = appeal.createdAt if available
Instant timestamp = appeal.createdAt() != null ? appeal.createdAt() : Instant.now();
DiscordSender.getInstance().sendEmbedToChannels(
channelIds,
"New Appeal Submitted",
description,
fields,
null,
timestamp,
null
);
}
private CompletableFuture<Integer> getCountAsync(HistoryType type, java.util.UUID uuid) {
@@ -83,26 +124,8 @@ public class AppealDiscord {
return future;
}
private String buildMessage(Appeal appeal, HistoryRecord history, int bans, int mutes, int warns, int kicks) {
boolean active = history.getUntil() == null || history.getUntil() <= 0 || history.getUntil() > System.currentTimeMillis();
String createdAt = formatInstant(appeal.createdAt());
return new StringBuilder()
.append("New Appeal Submitted\n")
.append("Username: ").append(appeal.username()).append("\n")
.append("UUID: ").append(appeal.uuid()).append("\n")
.append("Email: ").append(appeal.email()).append("\n")
.append("Submitted at: ").append(createdAt).append("\n")
.append("Punishment type: ").append(appeal.historyType()).append("\n")
.append("Punishment id: ").append(appeal.historyId()).append("\n")
.append("Reason: ").append(history.getReason()).append("\n")
.append("Active: ").append(active).append("\n\n")
.append("Appeal:\n").append(appeal.reason()).append("\n\n")
.append("Previous punishments:\n")
.append("- Bans: ").append(bans).append("\n")
.append("- Mutes: ").append(mutes).append("\n")
.append("- Warnings: ").append(warns).append("\n")
.append("- Kicks: ").append(kicks)
.toString();
private String safe(String s) {
return s == null ? "unknown" : s;
}
private String formatInstant(Instant instant) {