Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ export const config = parse<AltJTalkConfig>({
},
});

export const { numThreads } = parse({
export const { emojiDictionary, numThreads } = parse({
emojiDictionary: {
key: "EMOJI_DICTIONARY",
default:
"https://raw.githubusercontent.com/yagays/emoji-ja/master/data/emoji_ja.json",
},
numThreads: {
key: "NUM_THREADS",
parse: Number,
Expand Down
29 changes: 27 additions & 2 deletions src/synthesis/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ import { rulesExtended } from "discord-markdown-parser";
import type { Guild, Message } from "discord.js";
import SimpleMarkdown from "simple-markdown";
import type { SingleASTNode, ASTNode, Capture } from "simple-markdown";
import { emojiDictionary } from "../env";

interface EmojiDictionaryJson {
[emoji: string]: { short_name: string };
}

async function fetchDictionary() {
const response = await fetch(emojiDictionary);
if (!response.ok) throw new Error("Failed to fetch emoji dictionary");

const json = await response.json();
if (
typeof json !== "object" ||
json === null ||
!Object.values(json).every(
(value: { short_name: string }) => typeof value?.short_name === "string",
)
)
throw new Error("Invalid JSON");

return json as EmojiDictionaryJson;
}

const emojis = await fetchDictionary();

const parser = SimpleMarkdown.parserFor(
{
Expand Down Expand Up @@ -117,8 +141,9 @@ function text(ast: ASTNode, guild: Guild | null): string {
return " @ヒア ";
}
case "twemoji": {
// TODO: proper text to read aloud
return stringOrEmpty(ast.name);
const name = stringOrEmpty(ast.name);
const emoji = emojis[name];
return emoji ? emoji.short_name : name;
}
case "timestamp": {
const timestamp = stringOrEmpty(ast.timestamp);
Expand Down