From d45c922c72680bdd34613d94927c297f1bfca76e Mon Sep 17 00:00:00 2001 From: "Tobias Burdow [Kaleidox]" Date: Sat, 2 Dec 2023 15:58:56 +0100 Subject: [PATCH] set moderation scope for certain status messages --- .../mcsd/api/model/IStatusMessage.java | 6 +++ .../org/comroid/mcsd/api/model/Status.java | 37 ++++++++++++++----- .../core/module/discord/DiscordModule.java | 34 +++++++++++++---- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/api/main/java/org/comroid/mcsd/api/model/IStatusMessage.java b/src/api/main/java/org/comroid/mcsd/api/model/IStatusMessage.java index 1fcbb108..169be6b5 100644 --- a/src/api/main/java/org/comroid/mcsd/api/model/IStatusMessage.java +++ b/src/api/main/java/org/comroid/mcsd/api/model/IStatusMessage.java @@ -1,9 +1,11 @@ package org.comroid.mcsd.api.model; +import org.comroid.api.BitmaskAttribute; import org.jetbrains.annotations.Nullable; public interface IStatusMessage { Status getStatus(); + Scope getScope(); @Nullable default String getMessage() { @@ -13,4 +15,8 @@ default String getMessage() { default String toStatusMessage() { return getStatus().getEmoji() + '\t' + "Server is " + getStatus().getName(); } + + enum Scope implements BitmaskAttribute { + Public, Moderation + } } diff --git a/src/api/main/java/org/comroid/mcsd/api/model/Status.java b/src/api/main/java/org/comroid/mcsd/api/model/Status.java index 98f24df0..7a4b5bdd 100644 --- a/src/api/main/java/org/comroid/mcsd/api/model/Status.java +++ b/src/api/main/java/org/comroid/mcsd/api/model/Status.java @@ -7,25 +7,28 @@ import org.jetbrains.annotations.Nullable; import java.awt.*; +import java.util.Objects; @Getter public enum Status implements IntegerAttribute, IStatusMessage { - unknown_status ("❔", McFormatCode.Dark_Gray), - offline ("❌", McFormatCode.Dark_Red), - starting ("⏯️", McFormatCode.Aqua), - in_maintenance_mode ("\uD83D\uDD27", McFormatCode.Yellow), - running_backup ("\uD83D\uDCBE", McFormatCode.Green), - updating ("\uD83D\uDD04️", McFormatCode.Light_Purple), - in_Trouble ("⚠️", McFormatCode.Gold), - online ("✅", McFormatCode.Dark_Green), - shutting_down ("\uD83D\uDED1", McFormatCode.Red); + unknown_status (Scope.Moderation, "❔", McFormatCode.Dark_Gray), + offline (Scope.Public, "❌", McFormatCode.Dark_Red), + starting (Scope.Moderation, "⏯️", McFormatCode.Aqua), + in_maintenance_mode (Scope.Moderation, "\uD83D\uDD27", McFormatCode.Yellow), + running_backup (Scope.Moderation, "\uD83D\uDCBE", McFormatCode.Green), + updating (Scope.Moderation, "\uD83D\uDD04️", McFormatCode.Light_Purple), + in_Trouble (Scope.Moderation, "⚠️", McFormatCode.Gold), + online (Scope.Public, "✅", McFormatCode.Dark_Green), + shutting_down (Scope.Moderation, "\uD83D\uDED1", McFormatCode.Red); private final String emoji; private final Color color; + private final Scope scope; - Status(String emoji, McFormatCode color) { + Status(Scope scope, String emoji, McFormatCode color) { if (!color.isColor()) throw new IllegalArgumentException("Invalid format code; must be color: "); + this.scope = scope; this.emoji = emoji; this.color = color.getColor(); } @@ -48,10 +51,24 @@ public Status getStatus() { @Value @SuppressWarnings("InnerClassMayBeStatic") public class Message implements IStatusMessage { + @Nullable Scope scope; @Nullable String message; + public Message(@Nullable String message) { + this(null, message); + } + + public Message(@Nullable Scope scope, @Nullable String message) { + this.scope = scope; + this.message = message; + } + public Status getStatus() { return Status.this; } + + public Scope getScope() { + return Objects.requireNonNullElseGet(scope, () -> getStatus().getScope()); + } } } diff --git a/src/core/main/java/org/comroid/mcsd/core/module/discord/DiscordModule.java b/src/core/main/java/org/comroid/mcsd/core/module/discord/DiscordModule.java index 74eba339..adf040dd 100644 --- a/src/core/main/java/org/comroid/mcsd/core/module/discord/DiscordModule.java +++ b/src/core/main/java/org/comroid/mcsd/core/module/discord/DiscordModule.java @@ -12,6 +12,7 @@ import org.comroid.api.Polyfill; import org.comroid.api.SupplierX; import org.comroid.api.ThrowingSupplier; +import org.comroid.mcsd.api.model.IStatusMessage; import org.comroid.mcsd.core.entity.Server; import org.comroid.mcsd.core.entity.User; import org.comroid.mcsd.core.model.DiscordMessageSource; @@ -70,13 +71,14 @@ public DiscordModule(Server parent) { .thenApply(adapter::messageTemplate).join(); final var bot = adapter.messageTemplate(id); - // status -> dc + // public status -> dc server.component(StatusModule.class).map(StatusModule::getBus).ifPresent(bus -> - addChildren(bus.mapData(msg -> new EmbedBuilder() - //.setAuthor(parent.getAlternateName(), - // Optional.ofNullable(parent.getHomepage()) - // .orElse(parent.getViewURL()), - // parent.getThumbnailURL()) + addChildren(bus.filterData(msg -> msg.getScope() == IStatusMessage.Scope.Public) + .mapData(msg -> new EmbedBuilder() + .setAuthor(server.getAlternateName(), + Optional.ofNullable(server.getHomepage()) + .orElse(server.getViewURL()), + server.getThumbnailURL()) .setDescription(msg.toStatusMessage()) .setColor(msg.getStatus().getColor()) .setFooter(msg.getMessage()) @@ -139,7 +141,25 @@ public DiscordModule(Server parent) { }); //moderation channel - Optional.ofNullable(server.getModerationChannelId()).ifPresent(id -> addChildren(/*todo*/)); + Optional.ofNullable(server.getModerationChannelId()).ifPresent(id -> { + final var bot = adapter.messageTemplate(id); + + // public status -> dc + server.component(StatusModule.class).map(StatusModule::getBus).ifPresent(bus -> + addChildren(bus.filterData(msg -> msg.getScope() == IStatusMessage.Scope.Moderation) + .mapData(msg -> new EmbedBuilder() + //.setAuthor(parent.getAlternateName(), + // Optional.ofNullable(parent.getHomepage()) + // .orElse(parent.getViewURL()), + // parent.getThumbnailURL()) + .setDescription(msg.toStatusMessage()) + .setColor(msg.getStatus().getColor()) + .setFooter(msg.getMessage()) + .setTimestamp(Instant.now())) + .mapData(DiscordMessageSource::new) + .peekData(msg -> msg.setAppend(false)) + .subscribeData(bot))); + }); // console channel Optional.ofNullable(server.getConsoleChannelId()).ifPresent(id -> {