Skip to content

Commit

Permalink
set moderation scope for certain status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Dec 2, 2023
1 parent fc83519 commit d45c922
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -13,4 +15,8 @@ default String getMessage() {
default String toStatusMessage() {
return getStatus().getEmoji() + '\t' + "Server is " + getStatus().getName();
}

enum Scope implements BitmaskAttribute<Scope> {
Public, Moderation
}
}
37 changes: 27 additions & 10 deletions src/api/main/java/org/comroid/mcsd/api/model/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 -> {
Expand Down

0 comments on commit d45c922

Please sign in to comment.