mirror of
https://github.com/artiemis/artemis.js.git
synced 2026-02-14 10:21:54 +00:00
add translate modals and fuzzy search
This commit is contained in:
parent
0a866b17f3
commit
7cb09bc93a
15
bun.lock
15
bun.lock
@ -9,26 +9,27 @@
|
|||||||
"cheerio": "^1.0.0",
|
"cheerio": "^1.0.0",
|
||||||
"chrome-lens-ocr": "^4.0.4",
|
"chrome-lens-ocr": "^4.0.4",
|
||||||
"deepl-node": "^1.16.0",
|
"deepl-node": "^1.16.0",
|
||||||
"discord.js": "^14.17.3",
|
"discord.js": "^14.18.0",
|
||||||
"execa": "^9.5.2",
|
"execa": "^9.5.2",
|
||||||
"file-type": "^20.1.0",
|
"file-type": "^20.1.0",
|
||||||
"ky": "^1.7.4",
|
"fuse.js": "^7.1.0",
|
||||||
|
"ky": "^1.7.5",
|
||||||
"lru-cache": "^11.0.2",
|
"lru-cache": "^11.0.2",
|
||||||
"nanoid": "^5.0.9",
|
"nanoid": "^5.0.9",
|
||||||
"pino": "^9.6.0",
|
"pino": "^9.6.0",
|
||||||
"pino-pretty": "^13.0.0",
|
"pino-pretty": "^13.0.0",
|
||||||
"sharp": "^0.33.5",
|
"sharp": "^0.33.5",
|
||||||
"ya-ocr": "^1.1.0",
|
"ya-ocr": "^1.1.0",
|
||||||
"zod": "^3.24.1",
|
"zod": "^3.24.2",
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^9.20.0",
|
"@eslint/js": "^9.20.0",
|
||||||
"@types/bun": "latest",
|
"@types/bun": "latest",
|
||||||
"eslint": "^9.20.0",
|
"eslint": "^9.20.1",
|
||||||
"typescript-eslint": "^8.23.0",
|
"typescript-eslint": "^8.24.0",
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": "^5.0.0",
|
"typescript": "^5.7.3",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -315,6 +316,8 @@
|
|||||||
|
|
||||||
"form-data": ["form-data@3.0.2", "", { "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", "mime-types": "^2.1.12" } }, "sha512-sJe+TQb2vIaIyO783qN6BlMYWMw3WBOHA1Ay2qxsnjuafEOQFJ2JakedOQirT6D5XPRxDvS7AHYyem9fTpb4LQ=="],
|
"form-data": ["form-data@3.0.2", "", { "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", "mime-types": "^2.1.12" } }, "sha512-sJe+TQb2vIaIyO783qN6BlMYWMw3WBOHA1Ay2qxsnjuafEOQFJ2JakedOQirT6D5XPRxDvS7AHYyem9fTpb4LQ=="],
|
||||||
|
|
||||||
|
"fuse.js": ["fuse.js@7.1.0", "", {}, "sha512-trLf4SzuuUxfusZADLINj+dE8clK1frKdmqiJNb1Es75fmI5oY6X2mxLVUciLLjxqw/xr72Dhy+lER6dGd02FQ=="],
|
||||||
|
|
||||||
"get-stream": ["get-stream@9.0.1", "", { "dependencies": { "@sec-ant/readable-stream": "^0.4.1", "is-stream": "^4.0.1" } }, "sha512-kVCxPF3vQM/N0B1PmoqVUqgHP+EeVjmZSQn+1oCRPxd2P21P2F19lIgbR3HBosbB1PUhOAoctJnfEn2GbN2eZA=="],
|
"get-stream": ["get-stream@9.0.1", "", { "dependencies": { "@sec-ant/readable-stream": "^0.4.1", "is-stream": "^4.0.1" } }, "sha512-kVCxPF3vQM/N0B1PmoqVUqgHP+EeVjmZSQn+1oCRPxd2P21P2F19lIgbR3HBosbB1PUhOAoctJnfEn2GbN2eZA=="],
|
||||||
|
|
||||||
"glob-parent": ["glob-parent@6.0.2", "", { "dependencies": { "is-glob": "^4.0.3" } }, "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A=="],
|
"glob-parent": ["glob-parent@6.0.2", "", { "dependencies": { "is-glob": "^4.0.3" } }, "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A=="],
|
||||||
|
|||||||
@ -19,6 +19,7 @@
|
|||||||
"discord.js": "^14.18.0",
|
"discord.js": "^14.18.0",
|
||||||
"execa": "^9.5.2",
|
"execa": "^9.5.2",
|
||||||
"file-type": "^20.1.0",
|
"file-type": "^20.1.0",
|
||||||
|
"fuse.js": "^7.1.0",
|
||||||
"ky": "^1.7.5",
|
"ky": "^1.7.5",
|
||||||
"lru-cache": "^11.0.2",
|
"lru-cache": "^11.0.2",
|
||||||
"nanoid": "^5.0.9",
|
"nanoid": "^5.0.9",
|
||||||
|
|||||||
@ -1,4 +1,10 @@
|
|||||||
import { Client, Collection, GatewayIntentBits, Partials } from "discord.js";
|
import {
|
||||||
|
ActivityType,
|
||||||
|
Client,
|
||||||
|
Collection,
|
||||||
|
GatewayIntentBits,
|
||||||
|
Partials,
|
||||||
|
} from "discord.js";
|
||||||
import { REST } from "@discordjs/rest";
|
import { REST } from "@discordjs/rest";
|
||||||
import { env } from "./env";
|
import { env } from "./env";
|
||||||
import { API } from "@discordjs/core";
|
import { API } from "@discordjs/core";
|
||||||
@ -27,6 +33,9 @@ export class ArtemisClient extends Client {
|
|||||||
parse: [],
|
parse: [],
|
||||||
},
|
},
|
||||||
partials: [Partials.Channel],
|
partials: [Partials.Channel],
|
||||||
|
presence: {
|
||||||
|
activities: [{ name: "hecho con ❤️", type: ActivityType.Custom }],
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const rest = new REST().setToken(env.DISCORD_TOKEN);
|
const rest = new REST().setToken(env.DISCORD_TOKEN);
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import {
|
|||||||
hyperlink,
|
hyperlink,
|
||||||
inlineCode,
|
inlineCode,
|
||||||
SlashCommandBuilder,
|
SlashCommandBuilder,
|
||||||
type InteractionEditReplyOptions,
|
type InteractionReplyOptions,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { defineCommand } from "..";
|
import { defineCommand } from "..";
|
||||||
import {
|
import {
|
||||||
@ -44,7 +44,7 @@ export async function translateImpl(
|
|||||||
target: string,
|
target: string,
|
||||||
ocrModel?: OCRResult["model"],
|
ocrModel?: OCRResult["model"],
|
||||||
imageUrl?: string
|
imageUrl?: string
|
||||||
): Promise<InteractionEditReplyOptions> {
|
) {
|
||||||
let { translatedText, detectedSourceLang, model } = await translateDeepl(
|
let { translatedText, detectedSourceLang, model } = await translateDeepl(
|
||||||
text,
|
text,
|
||||||
source,
|
source,
|
||||||
@ -74,7 +74,7 @@ export async function translateImpl(
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
} satisfies InteractionReplyOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -98,7 +98,7 @@ export async function translateImpl(
|
|||||||
: {}),
|
: {}),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
} satisfies InteractionReplyOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
@ -136,10 +136,10 @@ export default defineCommand({
|
|||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
if (source && !(await isSourceLanguage(source))) {
|
if (source && !(await isSourceLanguage(source))) {
|
||||||
abort("Source language not supported");
|
abort("Source language not found");
|
||||||
}
|
}
|
||||||
if (target && !(await isTargetLanguage(target))) {
|
if (target && !(await isTargetLanguage(target))) {
|
||||||
abort("Target language not supported");
|
abort("Target language not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = await translateImpl(text, source, target);
|
const payload = await translateImpl(text, source, target);
|
||||||
|
|||||||
22
src/commands/language/translateEnglishMenu.ts
Normal file
22
src/commands/language/translateEnglishMenu.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js";
|
||||||
|
import { defineCommand } from "..";
|
||||||
|
import { translateImpl } from "./translate";
|
||||||
|
import { abort } from "../../utils/error";
|
||||||
|
|
||||||
|
export default defineCommand({
|
||||||
|
data: new ContextMenuCommandBuilder()
|
||||||
|
.setName("Translate to English")
|
||||||
|
.setType(ApplicationCommandType.Message),
|
||||||
|
|
||||||
|
async execute(interaction) {
|
||||||
|
if (!interaction.isMessageContextMenuCommand()) return;
|
||||||
|
|
||||||
|
const text = interaction.targetMessage.content;
|
||||||
|
if (!text) abort("No text to translate");
|
||||||
|
|
||||||
|
await interaction.deferReply();
|
||||||
|
|
||||||
|
const payload = await translateImpl(text, null, "en-US");
|
||||||
|
await interaction.editReply(payload);
|
||||||
|
},
|
||||||
|
});
|
||||||
@ -1,11 +1,53 @@
|
|||||||
import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js";
|
import {
|
||||||
|
ActionRowBuilder,
|
||||||
|
ApplicationCommandType,
|
||||||
|
ContextMenuCommandBuilder,
|
||||||
|
type ModalActionRowComponentBuilder,
|
||||||
|
ModalBuilder,
|
||||||
|
TextInputBuilder,
|
||||||
|
TextInputStyle,
|
||||||
|
} from "discord.js";
|
||||||
import { defineCommand } from "..";
|
import { defineCommand } from "..";
|
||||||
import { translateImpl } from "./translate";
|
|
||||||
import { abort } from "../../utils/error";
|
import { abort } from "../../utils/error";
|
||||||
|
import { translateImpl } from "./translate";
|
||||||
|
import { findFuzzyLanguage } from "../../utils/deepl";
|
||||||
|
|
||||||
|
export function buildTranslateModal() {
|
||||||
|
const modal = new ModalBuilder()
|
||||||
|
.setTitle("Translate")
|
||||||
|
.setCustomId("translate-modal");
|
||||||
|
|
||||||
|
const sourceInput = new TextInputBuilder()
|
||||||
|
.setLabel("Source language")
|
||||||
|
.setCustomId("source")
|
||||||
|
.setStyle(TextInputStyle.Short)
|
||||||
|
.setMaxLength(20)
|
||||||
|
.setPlaceholder("en, pl, hungarian, japanese...")
|
||||||
|
.setRequired(false);
|
||||||
|
|
||||||
|
const targetInput = new TextInputBuilder()
|
||||||
|
.setLabel("Target language")
|
||||||
|
.setCustomId("target")
|
||||||
|
.setStyle(TextInputStyle.Short)
|
||||||
|
.setMaxLength(20)
|
||||||
|
.setPlaceholder("en, pl, hungarian, japanese...")
|
||||||
|
.setRequired(false);
|
||||||
|
|
||||||
|
const sourceRow =
|
||||||
|
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(
|
||||||
|
sourceInput
|
||||||
|
);
|
||||||
|
const targetRow =
|
||||||
|
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(
|
||||||
|
targetInput
|
||||||
|
);
|
||||||
|
|
||||||
|
return modal.addComponents(sourceRow, targetRow);
|
||||||
|
}
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
data: new ContextMenuCommandBuilder()
|
data: new ContextMenuCommandBuilder()
|
||||||
.setName("Translate to English")
|
.setName("Translate...")
|
||||||
.setType(ApplicationCommandType.Message),
|
.setType(ApplicationCommandType.Message),
|
||||||
|
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
@ -14,9 +56,44 @@ export default defineCommand({
|
|||||||
const text = interaction.targetMessage.content;
|
const text = interaction.targetMessage.content;
|
||||||
if (!text) abort("No text to translate");
|
if (!text) abort("No text to translate");
|
||||||
|
|
||||||
|
const modal = buildTranslateModal();
|
||||||
|
await interaction.showModal(modal);
|
||||||
|
|
||||||
|
await interaction
|
||||||
|
.awaitModalSubmit({
|
||||||
|
filter: (i) => i.customId === "translate-modal",
|
||||||
|
time: 60000 * 5,
|
||||||
|
})
|
||||||
|
.then(async (interaction) => {
|
||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
const payload = await translateImpl(text, null, "en-US");
|
const sourceField =
|
||||||
|
interaction.fields.getTextInputValue("source") || "auto";
|
||||||
|
const targetField =
|
||||||
|
interaction.fields.getTextInputValue("target") || "en-US";
|
||||||
|
|
||||||
|
const source =
|
||||||
|
sourceField === "auto"
|
||||||
|
? null
|
||||||
|
: await findFuzzyLanguage(sourceField, "source").then(
|
||||||
|
(l) => l?.code
|
||||||
|
);
|
||||||
|
const target =
|
||||||
|
targetField === "en-US"
|
||||||
|
? targetField
|
||||||
|
: await findFuzzyLanguage(targetField, "target").then(
|
||||||
|
(l) => l?.code
|
||||||
|
);
|
||||||
|
|
||||||
|
if (source === undefined) {
|
||||||
|
abort("Source language not found");
|
||||||
|
}
|
||||||
|
if (!target) {
|
||||||
|
abort("Target language not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = await translateImpl(text, source, target);
|
||||||
await interaction.editReply(payload);
|
await interaction.editReply(payload);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import {
|
|||||||
hyperlink,
|
hyperlink,
|
||||||
inlineCode,
|
inlineCode,
|
||||||
SlashCommandBuilder,
|
SlashCommandBuilder,
|
||||||
type InteractionEditReplyOptions,
|
type InteractionReplyOptions,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { defineCommand } from "..";
|
import { defineCommand } from "..";
|
||||||
import { downloadFile } from "../../utils/http";
|
import { downloadFile } from "../../utils/http";
|
||||||
@ -23,7 +23,7 @@ export function buildOcrPayload(
|
|||||||
language: string,
|
language: string,
|
||||||
model: OCRResult["model"],
|
model: OCRResult["model"],
|
||||||
imageUrl?: string
|
imageUrl?: string
|
||||||
): InteractionEditReplyOptions {
|
) {
|
||||||
const languageName = languageCodeToName(language) ?? "Unknown";
|
const languageName = languageCodeToName(language) ?? "Unknown";
|
||||||
|
|
||||||
if (text.length > 4096) {
|
if (text.length > 4096) {
|
||||||
@ -38,7 +38,7 @@ export function buildOcrPayload(
|
|||||||
attachment: Buffer.from(text),
|
attachment: Buffer.from(text),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
} satisfies InteractionReplyOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -62,7 +62,7 @@ export function buildOcrPayload(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
} satisfies InteractionReplyOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function ocrImpl(url: string) {
|
export async function ocrImpl(url: string) {
|
||||||
|
|||||||
@ -48,10 +48,10 @@ export default defineCommand({
|
|||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
if (source && !(await isSourceLanguage(source))) {
|
if (source && !(await isSourceLanguage(source))) {
|
||||||
abort("Source language not supported");
|
abort("Source language not found");
|
||||||
}
|
}
|
||||||
if (target && !(await isTargetLanguage(target))) {
|
if (target && !(await isTargetLanguage(target))) {
|
||||||
abort("Target language not supported");
|
abort("Target language not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const { text, model } = await ocrImpl(imageUrl);
|
const { text, model } = await ocrImpl(imageUrl);
|
||||||
|
|||||||
27
src/commands/ocr/ocrTranslateEnglishMenu.ts
Normal file
27
src/commands/ocr/ocrTranslateEnglishMenu.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js";
|
||||||
|
import { defineCommand } from "..";
|
||||||
|
import { translateImpl } from "../language/translate";
|
||||||
|
import { ocrImpl } from "./ocr";
|
||||||
|
import { getImageFromAttachmentOrString } from "../../utils/functions";
|
||||||
|
|
||||||
|
export default defineCommand({
|
||||||
|
data: new ContextMenuCommandBuilder()
|
||||||
|
.setName("OCR and translate to English")
|
||||||
|
.setType(ApplicationCommandType.Message),
|
||||||
|
|
||||||
|
async execute(interaction) {
|
||||||
|
if (!interaction.isMessageContextMenuCommand()) return;
|
||||||
|
|
||||||
|
const attachment = interaction.targetMessage.attachments.first();
|
||||||
|
const imageUrl = getImageFromAttachmentOrString(
|
||||||
|
attachment,
|
||||||
|
interaction.targetMessage.content
|
||||||
|
);
|
||||||
|
|
||||||
|
await interaction.deferReply();
|
||||||
|
|
||||||
|
const { text, model } = await ocrImpl(imageUrl);
|
||||||
|
const payload = await translateImpl(text, null, "en-US", model);
|
||||||
|
await interaction.editReply(payload);
|
||||||
|
},
|
||||||
|
});
|
||||||
@ -3,10 +3,13 @@ import { defineCommand } from "..";
|
|||||||
import { translateImpl } from "../language/translate";
|
import { translateImpl } from "../language/translate";
|
||||||
import { ocrImpl } from "./ocr";
|
import { ocrImpl } from "./ocr";
|
||||||
import { getImageFromAttachmentOrString } from "../../utils/functions";
|
import { getImageFromAttachmentOrString } from "../../utils/functions";
|
||||||
|
import { buildTranslateModal } from "../language/translateMenu";
|
||||||
|
import { abort } from "../../utils/error";
|
||||||
|
import { findFuzzyLanguage } from "../../utils/deepl";
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
data: new ContextMenuCommandBuilder()
|
data: new ContextMenuCommandBuilder()
|
||||||
.setName("OCR and translate to English")
|
.setName("OCR and translate...")
|
||||||
.setType(ApplicationCommandType.Message),
|
.setType(ApplicationCommandType.Message),
|
||||||
|
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
@ -18,10 +21,45 @@ export default defineCommand({
|
|||||||
interaction.targetMessage.content
|
interaction.targetMessage.content
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const modal = buildTranslateModal();
|
||||||
|
await interaction.showModal(modal);
|
||||||
|
|
||||||
|
await interaction
|
||||||
|
.awaitModalSubmit({
|
||||||
|
filter: (i) => i.customId === "translate-modal",
|
||||||
|
time: 60000 * 5,
|
||||||
|
})
|
||||||
|
.then(async (interaction) => {
|
||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
|
const sourceField =
|
||||||
|
interaction.fields.getTextInputValue("source") || null;
|
||||||
|
const targetField =
|
||||||
|
interaction.fields.getTextInputValue("target") || "en-US";
|
||||||
|
|
||||||
|
const source =
|
||||||
|
sourceField === null
|
||||||
|
? sourceField
|
||||||
|
: await findFuzzyLanguage(sourceField, "source").then(
|
||||||
|
(l) => l?.code
|
||||||
|
);
|
||||||
|
const target =
|
||||||
|
targetField === "en-US"
|
||||||
|
? targetField
|
||||||
|
: await findFuzzyLanguage(targetField, "target").then(
|
||||||
|
(l) => l?.code
|
||||||
|
);
|
||||||
|
|
||||||
|
if (source === undefined) {
|
||||||
|
abort("Source language not found");
|
||||||
|
}
|
||||||
|
if (!target) {
|
||||||
|
abort("Target language not found");
|
||||||
|
}
|
||||||
|
|
||||||
const { text, model } = await ocrImpl(imageUrl);
|
const { text, model } = await ocrImpl(imageUrl);
|
||||||
const payload = await translateImpl(text, null, "en-US", model);
|
const payload = await translateImpl(text, source, target, model);
|
||||||
await interaction.editReply(payload);
|
await interaction.editReply(payload);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,6 +1,14 @@
|
|||||||
import { SlashCommandBuilder } from "discord.js";
|
import { SlashCommandBuilder } from "discord.js";
|
||||||
import { defineCommand } from "..";
|
import { defineCommand } from "..";
|
||||||
import { STATUS_CODES } from "http";
|
import { STATUS_CODES } from "http";
|
||||||
|
import Fuse from "fuse.js";
|
||||||
|
|
||||||
|
const codes = Object.entries(STATUS_CODES).map(([code, reason]) => ({
|
||||||
|
code,
|
||||||
|
reason,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const fuzzyCodes = new Fuse(codes, { keys: ["code", "reason"] });
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@ -15,14 +23,31 @@ export default defineCommand({
|
|||||||
),
|
),
|
||||||
|
|
||||||
async autocomplete(interaction) {
|
async autocomplete(interaction) {
|
||||||
|
const value = interaction.options.getFocused();
|
||||||
|
if (!value) {
|
||||||
await interaction.respond(
|
await interaction.respond(
|
||||||
Object.keys(STATUS_CODES)
|
codes
|
||||||
.filter((code) =>
|
.map(({ code, reason }) => ({
|
||||||
code.startsWith(
|
name: `${code} - ${reason}`,
|
||||||
interaction.options.getInteger("code", true).toString()
|
value: +code,
|
||||||
)
|
}))
|
||||||
)
|
.slice(0, 25)
|
||||||
.map((code) => ({ name: code, value: +code }))
|
);
|
||||||
|
return;
|
||||||
|
} else if (STATUS_CODES[value]) {
|
||||||
|
await interaction.respond([
|
||||||
|
{ name: `${value} - ${STATUS_CODES[value]}`, value: +value },
|
||||||
|
]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await interaction.respond(
|
||||||
|
fuzzyCodes
|
||||||
|
.search(interaction.options.getFocused())
|
||||||
|
.map(({ item }) => ({
|
||||||
|
name: `${item.code} - ${item.reason}`,
|
||||||
|
value: +item.code,
|
||||||
|
}))
|
||||||
.slice(0, 25)
|
.slice(0, 25)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,16 +1,43 @@
|
|||||||
import {
|
import {
|
||||||
Translator,
|
Translator,
|
||||||
|
type Language,
|
||||||
type SourceLanguageCode,
|
type SourceLanguageCode,
|
||||||
type TargetLanguageCode,
|
type TargetLanguageCode,
|
||||||
} from "deepl-node";
|
} from "deepl-node";
|
||||||
import { env } from "../env";
|
import { env } from "../env";
|
||||||
import { lazy } from "./functions";
|
import { lazy } from "./functions";
|
||||||
import type { TranslateResult } from "../types/translate";
|
import type { TranslateResult } from "../types/translate";
|
||||||
|
import Fuse from "fuse.js";
|
||||||
|
|
||||||
const translator = new Translator(env.DEEPL_API_KEY);
|
const translator = new Translator(env.DEEPL_API_KEY);
|
||||||
export const getSourceLanguages = lazy(() => translator.getSourceLanguages());
|
export const getSourceLanguages = lazy(() => translator.getSourceLanguages());
|
||||||
export const getTargetLanguages = lazy(() => translator.getTargetLanguages());
|
export const getTargetLanguages = lazy(() => translator.getTargetLanguages());
|
||||||
|
|
||||||
|
const fuzzyLanguages = lazy(async () => {
|
||||||
|
const keys = ["name", "code"];
|
||||||
|
return {
|
||||||
|
source: new Fuse(await getSourceLanguages(), { keys }),
|
||||||
|
target: new Fuse(await getTargetLanguages(), { keys }),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function searchFuzzyLanguages(
|
||||||
|
query: string,
|
||||||
|
type: "source" | "target"
|
||||||
|
) {
|
||||||
|
const { source, target } = await fuzzyLanguages();
|
||||||
|
const fuse = type === "source" ? source : target;
|
||||||
|
return fuse.search(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function findFuzzyLanguage(
|
||||||
|
query: string,
|
||||||
|
type: "source" | "target"
|
||||||
|
): Promise<Language | undefined> {
|
||||||
|
const results = await searchFuzzyLanguages(query, type);
|
||||||
|
return results[0]?.item;
|
||||||
|
}
|
||||||
|
|
||||||
export async function translate(
|
export async function translate(
|
||||||
text: string,
|
text: string,
|
||||||
source: string | null = null,
|
source: string | null = null,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user