Skip to content

Commit

Permalink
Switch emoji libs
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Nov 2, 2024
1 parent 2109ba4 commit 2e2282f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
52 changes: 16 additions & 36 deletions bot/src/main/java/me/duncte123/skybot/utils/TwemojiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,27 @@

package me.duncte123.skybot.utils;

import com.vdurmont.emoji.EmojiParser;
import net.fellbaum.jemoji.Emoji;
import net.fellbaum.jemoji.EmojiManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
* Adapted from https://gist.github.com/heyarny/71c246f2f7fa4d9d10904fb9d5b1fa1d
*/
public class TwemojiParser extends EmojiParser {
public class TwemojiParser {
private static final String BASE_URL = "https://raw.githubusercontent.com/twitter/twemoji/master/assets/72x72/";

private TwemojiParser() {}

public static String parseOne(String text) {
final List<UnicodeCandidate> emojis = getUnicodeCandidates(stripVariants(text));
final List<Emoji> emojis = EmojiManager.extractEmojisInOrder(stripVariants(text));

if (!emojis.isEmpty()) {
final String iconId = grabTheRightIcon(emojis.get(0).getEmoji().getUnicode());
final String iconId = grabTheRightIcon(emojis.getFirst());

return BASE_URL + iconId + ".png";
}
Expand All @@ -43,7 +48,7 @@ public static String parseOne(String text) {

// for future use
public static List<String> parseAll(String text) {
final List<UnicodeCandidate> emojis = getUnicodeCandidates(stripVariants(text));
final List<Emoji> emojis = EmojiManager.extractEmojisInOrder(stripVariants(text));

if (emojis.isEmpty()) {
return null;
Expand All @@ -52,8 +57,8 @@ public static List<String> parseAll(String text) {
final List<String> urls = new ArrayList<>();

// Kinda copied from EmojiParser but it does not have the variants on it
for (final UnicodeCandidate emoji : emojis) {
final String iconId = grabTheRightIcon(emoji.getEmoji().getUnicode());
for (final Emoji emoji : emojis) {
final String iconId = grabTheRightIcon(emoji);
final String iconUrl = BASE_URL + iconId + ".png";

urls.add(iconUrl);
Expand All @@ -62,39 +67,14 @@ public static List<String> parseAll(String text) {
return urls;
}

private static String toCodePoint(String unicodeSurrogates) {
final List<String> codes = new ArrayList<>();

int charAt;
int someValue = 0; // what is for?
int index = 0;

while (index < unicodeSurrogates.length()) {
charAt = unicodeSurrogates.charAt(index++);

if (someValue == 0) {
if (0xD800 <= charAt && charAt <= 0xDBFF) {
someValue = charAt;
} else {
codes.add(Integer.toString(charAt, 16));
}
} else {
final int calculation = 0x10000 + ((someValue - 0xD800) << 10) + (charAt - 0xDC00);

codes.add(Integer.toString(calculation, 16));
someValue = 0;
}
}

return String.join("-", codes);
}

public static String stripVariants(String rawText) {
// if variant is present as \uFE0F
return rawText.indexOf('\u200D') < 0 ? rawText.replace("\uFE0F", "") : rawText;
}

private static String grabTheRightIcon(String rawText) {
return toCodePoint(stripVariants(rawText));
private static String grabTheRightIcon(Emoji emoji) {
return emoji.getEmoji().codePoints().mapToObj(
operand -> Integer.toHexString(operand).toLowerCase(Locale.ROOT)
).collect(Collectors.joining("-"));
}
}
5 changes: 3 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ fun VersionCatalogBuilder.bot() {
library("jagTag", "com.github.jagrosh", "JagTag").version("6dbe1ba")
library("wolfram-alpha", "com.github.DuncteBot", "wolfram-alpha-java-binding").version("5c123ae")
library("duration-parser", "me.duncte123", "durationParser").version("1.1.3")
library("emoji-java", "com.github.minndevelopment", "emoji-java").version("master-SNAPSHOT")
// library("emoji-java", "com.github.minndevelopment", "emoji-java").version("master-SNAPSHOT")
library("jemoji", "net.fellbaum", "jemoji").version("1.5.2")
library("botCommons", "me.duncte123", "botCommons").version("3.1.18")

bundle("featureLibs", listOf("botCommons", "weebjava", "loadingBar", "jagTag", "wolfram-alpha", "duration-parser", "emoji-java"))
bundle("featureLibs", listOf("botCommons", "weebjava", "loadingBar", "jagTag", "wolfram-alpha", "duration-parser", "jemoji"))

library("javaLyrics", "com.github.DuncteBot.java-timed-lyrics", "protocol").version("1.2.0")
}
Expand Down

0 comments on commit 2e2282f

Please sign in to comment.