From 10b0451c7b3af8093d6649c2b29fd87260a1f29b Mon Sep 17 00:00:00 2001 From: "Tobias Burdow [Kaleidox]" Date: Wed, 25 Oct 2023 10:14:44 +0200 Subject: [PATCH] scan mc->dc for unicode & custom emoji Closes #42 --- core/build.gradle | 3 +++ .../core/module/discord/DiscordModule.java | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/core/build.gradle b/core/build.gradle index 73892892..79d45641 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -108,6 +108,9 @@ dependencies { api 'net.dv8tion:JDA:5.+' api 'club.minnced:discord-webhooks:0.+' + // emoji + api 'com.kcthota:emoji4j:6.+' + // utility developmentOnly 'org.springframework.boot:spring-boot-starter-actuator' developmentOnly 'org.springframework.boot:spring-boot-devtools' 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 adfae66b..408a991c 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 @@ -1,13 +1,16 @@ package org.comroid.mcsd.core.module.discord; +import emoji4j.EmojiUtils; import lombok.Getter; import lombok.SneakyThrows; import lombok.ToString; import lombok.extern.java.Log; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.emoji.CustomEmoji; import org.comroid.api.Component; import org.comroid.api.Polyfill; +import org.comroid.api.SupplierX; import org.comroid.mcsd.core.entity.Server; import org.comroid.mcsd.core.entity.User; import org.comroid.mcsd.core.model.DiscordMessageSource; @@ -21,6 +24,7 @@ import java.time.Instant; import java.util.Optional; +import java.util.regex.Pattern; import java.util.stream.Collectors; import static org.comroid.mcsd.core.util.ApplicationContextProvider.bean; @@ -33,6 +37,8 @@ @ToString @Component.Requires({ConsolePlayerEventModule.class,ConsoleModule.class}) public class DiscordModule extends ServerModule { + public static final Pattern EmojiPattern = Pattern.compile(".*:(?[\\w-_]+):?.*"); + public static final Factory Factory = new Factory<>(DiscordModule.class) { @Override public DiscordModule create(Server parent) { @@ -83,7 +89,19 @@ public DiscordModule(Server parent) { chatBus.filterData(msg -> msg.getType().isFlagSet(server.getPublicChannelEvents())) .mapData(msg -> { var player = bean(UserRepo.class).get(msg.getUsername()).assertion(); - return new DiscordMessageSource(msg.toString()) + String str = msg.toString(); + str = EmojiPattern.matcher(str).replaceAll(match -> { + var name = match.group(1); + if (EmojiUtils.isEmoji(name)) + return EmojiUtils.getEmoji(name).getEmoji(); + else { + var results = adapter.getJda().getEmojisByName(name, true); + return SupplierX.ofStream(results.stream()) + .map(CustomEmoji::getAsMention) + .orElse(""); + } + }); + return new DiscordMessageSource(str) .setDisplayUser(player.getDisplayUser(User.DisplayUser.Type.Discord, User.DisplayUser.Type.Minecraft) .assertion()) .setAppend(true);