Skip to content

Commit

Permalink
feat: improve and add placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmakila committed Feb 12, 2024
1 parent dd784d0 commit a68a30b
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 45 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group = "fi.fabianadrian"
version = "2.0.0"
version = "2.1.0"
description = "A simple plugin that forwards chat messages to a webhook."
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;

public record Message(Audience author, Component message) {
public record Message(Audience author, Component message, boolean cancelled) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;

import java.util.Date;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class ClientManager {
private final WebhookChatLogger wcl;
private final MiniMessage miniMessage = MiniMessage.miniMessage();
private final DiscordClient discordClient;
private WebhookChatLoggerConfig config;
private DateTimeFormatter dateTimeFormatter;

public ClientManager(WebhookChatLogger wcl) {
this.wcl = wcl;
Expand All @@ -32,18 +35,25 @@ public void send(Message message) {
public void reload() {
this.config = this.wcl.config();
this.discordClient.reload();

String pattern = this.config.placeholders().timestampFormat();
ZoneId zoneId = this.config.placeholders().timeZone();
this.dateTimeFormatter = DateTimeFormatter.ofPattern(pattern).withZone(zoneId);
}

private Component parseMessage(Message message) {
String authorName = message.author().getOrDefault(Identity.NAME, "unknown author");
Component authorDisplayName = message.author().getOrDefault(Identity.DISPLAY_NAME, Component.text(authorName));

String timestamp = this.config.timestampFormat().format(new Date());
String timestamp = this.dateTimeFormatter.format(Instant.now());

String cancelled = message.cancelled() ? this.config.placeholders().cancelled() : "";

TagResolver.Builder resolverBuilder = TagResolver.builder().resolvers(
Placeholder.unparsed("author_name", authorName),
Placeholder.component("author_display_name", authorDisplayName),
Placeholder.component("message", message.message()),
Placeholder.unparsed("cancelled", cancelled),
Placeholder.unparsed("timestamp", timestamp)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fi.fabianadrian.webhookchatlogger.common.config;

import fi.fabianadrian.webhookchatlogger.common.config.serializer.SimpleDateFormatSerializer;
import fi.fabianadrian.webhookchatlogger.common.config.serializer.ZoneIdSerializer;
import org.slf4j.Logger;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
Expand Down Expand Up @@ -42,7 +42,7 @@ public static <C> ConfigManager<C> create(Path configFolder, String fileName, Cl
.build();

ConfigurationOptions options = new ConfigurationOptions.Builder()
.addSerialiser(new SimpleDateFormatSerializer())
.addSerialiser(new ZoneIdSerializer())
.sorter(new AnnotationBasedSorter())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
package fi.fabianadrian.webhookchatlogger.common.config;

import fi.fabianadrian.webhookchatlogger.common.config.section.DiscordConfigSection;
import fi.fabianadrian.webhookchatlogger.common.config.section.PlaceholderConfigSection;
import space.arim.dazzleconf.annote.ConfComments;
import space.arim.dazzleconf.annote.ConfDefault;
import space.arim.dazzleconf.annote.ConfKey;
import space.arim.dazzleconf.annote.SubSection;
import space.arim.dazzleconf.sorter.AnnotationBasedSorter;

import java.text.SimpleDateFormat;


public interface WebhookChatLoggerConfig {
@AnnotationBasedSorter.Order(0)
@ConfDefault.DefaultString("[<timestamp>] <author_name>: <message>")
@ConfDefault.DefaultString("<cancelled>[<timestamp>] <author_name>: <message>")
@ConfComments({
"Which format logged messages will use. Supports MiniMessage and MiniPlaceholders.",
"Keep in mind that not all webhooks support all features e.g. colors.",
"Available placeholders:",
"<author_name>, <author_display_name>, <message>, <timestamp>"
"<author_name>, <author_display_name>, <message>, <timestamp>, <cancelled>"
})
String messageFormat();

@AnnotationBasedSorter.Order(1)
@ConfDefault.DefaultString("HH:mm:ss")
@ConfComments({
"Format for the <timestamp> placeholder."
})
SimpleDateFormat timestampFormat();

@AnnotationBasedSorter.Order(2)
@ConfDefault.DefaultBoolean(true)
@ConfComments("Whether cancelled chat messages should be sent to the webhook.")
boolean logCancelledMessages();

@AnnotationBasedSorter.Order(2)
@ConfComments("Configuration options for various placeholders.")
@SubSection
PlaceholderConfigSection placeholders();

@AnnotationBasedSorter.Order(3)
@SubSection
@ConfKey("discord")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fi.fabianadrian.webhookchatlogger.common.config.section;

import space.arim.dazzleconf.annote.ConfComments;
import space.arim.dazzleconf.annote.ConfDefault;
import space.arim.dazzleconf.sorter.AnnotationBasedSorter;

import java.time.ZoneId;

public interface PlaceholderConfigSection {

@AnnotationBasedSorter.Order(0)
@ConfDefault.DefaultString("HH:mm:ss")
@ConfComments({
"Format for the <timestamp> placeholder."
})
String timestampFormat();

@AnnotationBasedSorter.Order(1)
@ConfDefault.DefaultString("default")
@ConfComments({
"The timezone used in <timestamp> placeholder.",
"Set to 'default' to use the server timezone."
})
ZoneId timeZone();

@AnnotationBasedSorter.Order(2)
@ConfDefault.DefaultString("[Cancelled] ")
@ConfComments("The text used in <cancelled> placeholder.")
String cancelled();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fi.fabianadrian.webhookchatlogger.common.config.serializer;

import space.arim.dazzleconf.error.BadValueException;
import space.arim.dazzleconf.serialiser.Decomposer;
import space.arim.dazzleconf.serialiser.FlexibleType;
import space.arim.dazzleconf.serialiser.ValueSerialiser;

import java.time.ZoneId;

public class ZoneIdSerializer implements ValueSerialiser<ZoneId> {
@Override
public Class<ZoneId> getTargetClass() {
return ZoneId.class;
}

@Override
public ZoneId deserialise(FlexibleType flexibleType) throws BadValueException {
String string = flexibleType.getString();

if ("default".equalsIgnoreCase(string) || string.isBlank()) {
return ZoneId.systemDefault();
}

return ZoneId.of(string);
}

@Override
public Object serialise(ZoneId zoneId, Decomposer decomposer) {
return zoneId.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void onChat(AsyncChatEvent event) {
return;
}

Message message = new Message(event.getPlayer(), event.message());
Message message = new Message(event.getPlayer(), event.message(), event.isCancelled());
this.wcl.clientManager().send(message);
}
}

0 comments on commit a68a30b

Please sign in to comment.