aria-iuvo/src/modules/translate.ts
Naomi Carrigan 7e5a0ada2a
Some checks failed
Node.js CI / Lint and Test (push) Has been cancelled
feat: initial project prototype (#1)
### Explanation

_No response_

### Issue

_No response_

### Attestations

- [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/)
- [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/).
- [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/).

### Dependencies

- [x] I have pinned the dependencies to a specific patch version.

### Style

- [x] I have run the linter and resolved any errors.
- [x] My pull request uses an appropriate title, matching the conventional commit standards.
- [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request.

### Tests

- [ ] My contribution adds new code, and I have added tests to cover it.
- [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes.
- [ ] All new and existing tests pass locally with my changes.
- [ ] Code coverage remains at or above the configured threshold.

### Documentation

_No response_

### Versioning

Major - My pull request introduces a breaking change.

Reviewed-on: #1
Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com>
Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
2025-02-10 14:33:27 -08:00

95 lines
3.0 KiB
TypeScript

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