Skip to content

Commit

Permalink
feat: Add source notices for language recognition.
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern committed Jun 23, 2024
1 parent bd14523 commit 2715e7f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions assets/localisations/commands/eng-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions source/constants/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion source/library/adapters/detectors/adapter.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
28 changes: 26 additions & 2 deletions source/library/commands/handlers/recognise.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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],
},
],
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/library/stores/adapters/detectors.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand Down

0 comments on commit 2715e7f

Please sign in to comment.