add charinfo

This commit is contained in:
artie 2025-02-09 20:03:28 +01:00
parent 4f5b2e54a7
commit a65c324426
4 changed files with 88 additions and 2 deletions

View File

@ -59,7 +59,9 @@ export async function translateImpl(
files: [ files: [
{ {
name: `${displaySource}-${displayTarget}.txt`, name: `${displaySource}-${displayTarget}.txt`,
attachment: `--- From ${displaySource} to ${displayTarget} ---\n${translatedText}`, attachment: Buffer.from(
`--- From ${displaySource} to ${displayTarget} ---\n${translatedText}`
),
}, },
...(attachment ? [attachment] : []), ...(attachment ? [attachment] : []),
], ],

View File

@ -30,7 +30,7 @@ export function buildOcrPayload(
files: [ files: [
{ {
name: "ocr.txt", name: "ocr.txt",
attachment: text, attachment: Buffer.from(text),
}, },
...(attachment ? [attachment] : []), ...(attachment ? [attachment] : []),
], ],

View File

@ -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,
},
},
],
});
},
});

14
src/scripts/charinfo.py Normal file
View File

@ -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)