generated from nhcarrigan/template
feat: add logging with our new monitor (#3)
Some checks failed
Node.js CI / Lint and Test (push) Has been cancelled
Some checks failed
Node.js CI / Lint and Test (push) Has been cancelled
### 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 Minor - My pull request introduces a new non-breaking feature. Reviewed-on: #3 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit is contained in:
@ -13,6 +13,8 @@ import {
|
||||
} from "discord.js";
|
||||
import { supportedLocales } from "../config/locales.js";
|
||||
import { i18n } from "../utils/i18n.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
import { getLocale } from "./getLocale.js";
|
||||
|
||||
/**
|
||||
@ -23,82 +25,97 @@ import { getLocale } from "./getLocale.js";
|
||||
export const translate = async(
|
||||
interaction: MessageContextMenuCommandInteraction,
|
||||
): Promise<void> => {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const targetLocale = getLocale(interaction);
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
const targetLocale = getLocale(interaction);
|
||||
|
||||
const isEntitled = interaction.entitlements.find((entitlement) => {
|
||||
return entitlement.userId === interaction.user.id && entitlement.isActive();
|
||||
});
|
||||
const isEntitled = interaction.entitlements.find((entitlement) => {
|
||||
return (
|
||||
entitlement.userId === interaction.user.id && entitlement.isActive()
|
||||
);
|
||||
});
|
||||
|
||||
if (!isEntitled && interaction.user.id !== "465650873650118659") {
|
||||
const subscribeButton = new ButtonBuilder().
|
||||
setStyle(ButtonStyle.Premium).
|
||||
setSKUId("1338596712121499669");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||
subscribeButton,
|
||||
if (!isEntitled && interaction.user.id !== "465650873650118659") {
|
||||
const subscribeButton = new ButtonBuilder().
|
||||
setStyle(ButtonStyle.Premium).
|
||||
setSKUId("1338596712121499669");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||
subscribeButton,
|
||||
);
|
||||
await interaction.editReply({
|
||||
components: [ row ],
|
||||
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({
|
||||
components: [ row ],
|
||||
content: i18n("subscription-required", targetLocale),
|
||||
content: i18n("translation", targetLocale, {
|
||||
confidence: sourceLocale?.confidence,
|
||||
language: sourceLocale?.language,
|
||||
lng: targetLocale,
|
||||
translation: translation.translatedText,
|
||||
}),
|
||||
});
|
||||
return;
|
||||
} catch (error) {
|
||||
const targetLocale = getLocale(interaction);
|
||||
await replyToError(interaction, targetLocale);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("translate command", error);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user