From 2715e7fbad7a434f72b98f4c790d71081d4dcaaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Oszcz=C4=99da?= Date: Sun, 23 Jun 2024 11:35:34 +0100 Subject: [PATCH] feat: Add source notices for language recognition. --- assets/localisations/commands/eng-US.json | 1 + source/constants/contexts.ts | 3 ++ source/library/adapters/detectors/adapter.ts | 2 +- .../source-notices/recognition-notice.ts | 20 +++++++++++++ source/library/commands/handlers/recognise.ts | 28 +++++++++++++++++-- source/library/stores/adapters/detectors.ts | 2 +- 6 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 source/library/commands/components/source-notices/recognition-notice.ts diff --git a/assets/localisations/commands/eng-US.json b/assets/localisations/commands/eng-US.json index 29e713139..84d0ebac7 100644 --- a/assets/localisations/commands/eng-US.json +++ b/assets/localisations/commands/eng-US.json @@ -104,6 +104,7 @@ "recognise.strings.fields.possibleMatches.title": "Other options", "recognise.strings.fields.possibleMatches.description.single": "The language is less likely to be {language}.", "recognise.strings.fields.possibleMatches.description.multiple": "These choices are less likely, but the language could also be one of:", + "recognise.strings.recognitionsMadeBy": "Recognitions made by the above libraries.", "game.name": "game", "game.description": "Pick the correct word out of four to fit in the blank.", "game.strings.sentence": "Sentence", diff --git a/source/constants/contexts.ts b/source/constants/contexts.ts index 1d1a11553..21584e195 100644 --- a/source/constants/contexts.ts +++ b/source/constants/contexts.ts @@ -868,6 +868,9 @@ export default Object.freeze({ translationsSourcedFrom: ({ localise, locale }) => ({ sourcedFrom: localise("translate.strings.sourcedFrom", locale), }), + recognitionsMadeBy: ({ localise, locale }) => ({ + recognitionsMadeBy: localise("recognise.strings.recognitionsMadeBy", locale)(), + }), pardoned: ({ localise, locale }) => ({ title: localise("pardon.strings.pardoned.title", locale)(), description: localise("pardon.strings.pardoned.description", locale), diff --git a/source/library/adapters/detectors/adapter.ts b/source/library/adapters/detectors/adapter.ts index 73ff3d2e7..753a226f9 100644 --- a/source/library/adapters/detectors/adapter.ts +++ b/source/library/adapters/detectors/adapter.ts @@ -1,7 +1,7 @@ import type { DetectionLanguage } from "logos:constants/languages"; +import type { Licence } from "logos:constants/licences.ts"; import type { Client } from "logos/client"; import { Logger } from "logos/logger"; -import type { Licence } from "logos:constants/licences.ts"; interface SingleDetectionResult { readonly language: DetectionLanguage; diff --git a/source/library/commands/components/source-notices/recognition-notice.ts b/source/library/commands/components/source-notices/recognition-notice.ts new file mode 100644 index 000000000..02b6f2162 --- /dev/null +++ b/source/library/commands/components/source-notices/recognition-notice.ts @@ -0,0 +1,20 @@ +import type { Licence } from "logos:constants/licences.ts"; +import type { Client } from "logos/client.ts"; +import { SourceNotice } from "logos/commands/components/source-notices/source-notice.ts"; + +class RecognitionSourceNotice extends SourceNotice { + constructor(client: Client, { interaction, sources }: { interaction: Logos.Interaction; sources: Licence[] }) { + const strings = constants.contexts.recognitionsMadeBy({ + localise: client.localise.bind(client), + locale: interaction.displayLocale, + }); + + super(client, { + interaction, + sources: sources.map((source) => `[${source.name}](${source.link})`), + notice: strings.recognitionsMadeBy, + }); + } +} + +export { RecognitionSourceNotice }; diff --git a/source/library/commands/handlers/recognise.ts b/source/library/commands/handlers/recognise.ts index aaf9555d8..916597edb 100644 --- a/source/library/commands/handlers/recognise.ts +++ b/source/library/commands/handlers/recognise.ts @@ -1,6 +1,7 @@ import type { DetectionLanguage } from "logos:constants/languages"; import { list } from "logos:core/formatting"; import type { Client } from "logos/client"; +import { RecognitionSourceNotice } from "logos/commands/components/source-notices/recognition-notice.ts"; async function handleRecogniseLanguageChatInput( client: Client, @@ -69,6 +70,10 @@ async function handleRecogniseLanguage( return; } + const sourceNotice = new RecognitionSourceNotice(client, { interaction, sources: detectedLanguages.sources }); + + await sourceNotice.register(); + if (detectedLanguages.likely.length === 1 && detectedLanguages.possible.length === 0) { const language = detectedLanguages.likely.at(0) as DetectionLanguage | undefined; if (language === undefined) { @@ -79,8 +84,19 @@ async function handleRecogniseLanguage( ...constants.contexts.likelyMatch({ localise: client.localise.bind(client), locale: interaction.locale }), ...constants.contexts.language({ localise: client.localise.bind(client), locale: interaction.locale }), }; + await client.noticed(interaction, { - description: strings.description({ language: strings.language(language) }), + embeds: [ + { + description: strings.description({ language: strings.language(language) }), + }, + ], + components: [ + { + type: Discord.MessageComponentTypes.ActionRow, + components: [sourceNotice.button], + }, + ], }); return; } @@ -160,7 +176,15 @@ async function handleRecogniseLanguage( }); } - await client.noticed(interaction, { fields }); + await client.noticed(interaction, { + embeds: [{ fields }], + components: [ + { + type: Discord.MessageComponentTypes.ActionRow, + components: [sourceNotice.button], + }, + ], + }); } } diff --git a/source/library/stores/adapters/detectors.ts b/source/library/stores/adapters/detectors.ts index aa4ec1702..73fe5c33a 100644 --- a/source/library/stores/adapters/detectors.ts +++ b/source/library/stores/adapters/detectors.ts @@ -1,11 +1,11 @@ import type { DetectionLanguage, Detector } from "logos:constants/languages"; +import type { Licence } from "logos:constants/licences.ts"; import type { DetectorAdapter, SingleDetectionResult } from "logos/adapters/detectors/adapter"; import { CLDAdapter } from "logos/adapters/detectors/cld"; import { ELDAdapter } from "logos/adapters/detectors/eld"; import { FastTextAdapter } from "logos/adapters/detectors/fasttext"; import { TinyLDAdapter } from "logos/adapters/detectors/tinyld"; import type { Client } from "logos/client"; -import type { Licence } from "logos:constants/licences.ts"; interface DetectionResult { readonly likely: DetectionLanguage[];