diff --git a/src/commands/language/translate.ts b/src/commands/language/translate.ts index 11f7f9a..fff6120 100644 --- a/src/commands/language/translate.ts +++ b/src/commands/language/translate.ts @@ -59,7 +59,9 @@ export async function translateImpl( files: [ { name: `${displaySource}-${displayTarget}.txt`, - attachment: `--- From ${displaySource} to ${displayTarget} ---\n${translatedText}`, + attachment: Buffer.from( + `--- From ${displaySource} to ${displayTarget} ---\n${translatedText}` + ), }, ...(attachment ? [attachment] : []), ], diff --git a/src/commands/ocr/ocr.ts b/src/commands/ocr/ocr.ts index 501d722..4478bd2 100644 --- a/src/commands/ocr/ocr.ts +++ b/src/commands/ocr/ocr.ts @@ -30,7 +30,7 @@ export function buildOcrPayload( files: [ { name: "ocr.txt", - attachment: text, + attachment: Buffer.from(text), }, ...(attachment ? [attachment] : []), ], diff --git a/src/commands/utility/charinfo.ts b/src/commands/utility/charinfo.ts new file mode 100644 index 0000000..be6652f --- /dev/null +++ b/src/commands/utility/charinfo.ts @@ -0,0 +1,70 @@ +import { bold, hyperlink, inlineCode, SlashCommandBuilder } from "discord.js"; +import { defineCommand } from ".."; +import { pluralize, shell } from "../../utils/functions"; +import { abort } from "../../utils/error"; + +export default defineCommand({ + data: new SlashCommandBuilder() + .setName("charinfo") + .setDescription("Unicode character info for given text") + .addStringOption((option) => + option + .setName("text") + .setDescription("The text to get info for") + .setRequired(true) + ), + + async execute(interaction) { + const text = interaction.options.getString("text", true); + const footer = pluralize(text.length, "character"); + + const namesResult = await shell({ + input: text, + })`python3 src/scripts/charinfo.py`; + if (namesResult.failed) { + abort("Failed to get character names"); + } + + const names = JSON.parse(namesResult.stdout) as string[]; + + const description = text + .split("") + .map((char, i) => { + const code = char.codePointAt(0)!.toString(16).toUpperCase(); + return `${inlineCode(char)} - ${inlineCode( + "U+" + code.padStart(4, "0") + )} - ${bold( + hyperlink( + names[i], + `http://www.fileformat.info/info/unicode/char/${code}` + ) + )}`; + }) + .join("\n"); + + if (description.length > 4096) { + await interaction.reply({ + files: [ + { + name: "charinfo.md", + attachment: Buffer.from(`${description}\n\n${footer}`), + }, + ], + }); + return; + } + + await interaction.reply({ + embeds: [ + { + title: "Character Info", + description, + color: 0xfefefe, + footer: { + text: footer, + }, + }, + ], + }); + }, +}); diff --git a/src/scripts/charinfo.py b/src/scripts/charinfo.py new file mode 100644 index 0000000..b97c626 --- /dev/null +++ b/src/scripts/charinfo.py @@ -0,0 +1,14 @@ +import json +import sys +import unicodedata + + +text = sys.stdin.read() + +names = [] +for char in text: + names.append(unicodedata.name(char, "Name not found.")) + +names = json.dumps(names) + +print(names)