Skip to content

Commit

Permalink
scan mc->dc for unicode & custom emoji
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
burdoto committed Oct 25, 2023
1 parent f6ad9c8 commit 10b0451
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -33,6 +37,8 @@
@ToString
@Component.Requires({ConsolePlayerEventModule.class,ConsoleModule.class})
public class DiscordModule extends ServerModule {
public static final Pattern EmojiPattern = Pattern.compile(".*:(?<name>[\\w-_]+):?.*");

public static final Factory<DiscordModule> Factory = new Factory<>(DiscordModule.class) {
@Override
public DiscordModule create(Server parent) {
Expand Down Expand Up @@ -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("<unknown emoji>");
}
});
return new DiscordMessageSource(str)
.setDisplayUser(player.getDisplayUser(User.DisplayUser.Type.Discord, User.DisplayUser.Type.Minecraft)
.assertion())
.setAppend(true);
Expand Down

0 comments on commit 10b0451

Please sign in to comment.