Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | /** * @copyright nhcarrigan * @license Naomi's Public License * @author Naomi Carrigan */ import { MessageFlags, type MessageContextMenuCommandInteraction, } from "discord.js"; import { supportedLocales } from "../config/locales.js"; import { i18n } from "../utils/i18n.js"; import { getLocale } from "./getLocale.js"; /** * Translates a message to the user's locale. * @param interaction -- The interaction payload from Discord. */ // eslint-disable-next-line max-statements, max-lines-per-function -- This is a complex function. export const translate = async( interaction: MessageContextMenuCommandInteraction, ): Promise<void> => { await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] }); const targetLocale = getLocale(interaction); const isEntitled = interaction.entitlements.find((entitlement) => { return entitlement.userId === interaction.user.id && entitlement.isActive(); }); if (!isEntitled) { await interaction.editReply({ content: i18n("subscription-required", targetLocale), }); return; } if (!supportedLocales.includes(targetLocale)) { await interaction.editReply(i18n("unsupported-locale", targetLocale, { target: targetLocale, })); return; } const message = interaction.options.getMessage("message", true); if (message.content === "") { await interaction.editReply({ content: i18n("no-message-content", targetLocale), }); return; } const sourceLocaleRequestParameters = new URLSearchParams(); sourceLocaleRequestParameters.append("q", message.content); sourceLocaleRequestParameters.append( "api_key", process.env.TRANSLATE_TOKEN ?? "", ); const sourceLocaleRequest = await fetch( "https://trans.nhcarrigan.com/detect", { body: sourceLocaleRequestParameters, method: "POST", }, ); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- .json() doesn't accept a generic. const [ sourceLocale ] = (await sourceLocaleRequest.json()) as Array<{ confidence: number; language: string; }>; const translationRequestParameters = new URLSearchParams(); translationRequestParameters.append("q", message.content); translationRequestParameters.append("source", sourceLocale?.language ?? "en"); translationRequestParameters.append("target", targetLocale); translationRequestParameters.append( "api_key", process.env.TRANSLATE_TOKEN ?? "", ); const translationRequest = await fetch( "https://trans.nhcarrigan.com/translate", { body: translationRequestParameters, method: "POST" }, ); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- .json() doesn't accept a generic. const translation = (await translationRequest.json()) as { translatedText: string; }; await interaction.editReply({ content: i18n("translation", targetLocale, { confidence: sourceLocale?.confidence, language: sourceLocale?.language, lng: targetLocale, translation: translation.translatedText, }), }); }; |