Skip to content

Commit

Permalink
refactor: Reduce duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern committed Oct 7, 2024
1 parent e67cccf commit c61a961
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 54 deletions.
4 changes: 2 additions & 2 deletions source/library/commands/fragments/autocomplete/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { trim } from "logos:constants/formatting";
import type { Client } from "logos/client";
import { handleSimpleAutocomplete } from "logos/commands/fragments/autocomplete/simple";

async function autocompleteLanguage(
async function handleAutocompleteLanguage(
client: Client,
interaction: Logos.Interaction<any, { language: string | undefined }>,
): Promise<void> {
Expand All @@ -26,4 +26,4 @@ async function autocompleteLanguage(
});
}

export { autocompleteLanguage };
export { handleAutocompleteLanguage };
23 changes: 23 additions & 0 deletions source/library/commands/fragments/autocomplete/timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { trim } from "logos:constants/formatting.ts";
import type { Client } from "logos/client.ts";
import { parseTimeExpression } from "logos/commands/interactions.ts";

async function handleAutocompleteTimestamp(
client: Client,
interaction: Logos.Interaction<any, { timestamp: string }>,
): Promise<void> {
const timestamp = parseTimeExpression(client, interaction, interaction.parameters.timestamp);
if (timestamp === undefined) {
const strings = constants.contexts.autocompleteTimestamp({
localise: client.localise,
locale: interaction.locale,
});
client.respond(interaction, [{ name: trim(strings.autocomplete, 100), value: "" }]).ignore();

return;
}

client.respond(interaction, [{ name: timestamp[0], value: timestamp[1].toString() }]).ignore();
}

export { handleAutocompleteTimestamp };
4 changes: 2 additions & 2 deletions source/library/commands/handlers/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { getLocaleByLearningLanguage } from "logos:constants/languages/learning"
import { isLocalisationLanguage } from "logos:constants/languages/localisation";
import { shuffle } from "ioredis/built/utils";
import type { Client } from "logos/client";
import { autocompleteLanguage } from "logos/commands/fragments/autocomplete/language";
import { handleAutocompleteLanguage } from "logos/commands/fragments/autocomplete/language";
import type { SentencePair } from "logos/stores/volatile";

async function handleFindInContextAutocomplete(
client: Client,
interaction: Logos.Interaction<any, { language: string | undefined }>,
): Promise<void> {
await autocompleteLanguage(client, interaction);
await handleAutocompleteLanguage(client, interaction);
}

async function handleFindInContext(
Expand Down
16 changes: 2 additions & 14 deletions source/library/commands/handlers/music/fast-forward.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { trim } from "logos:constants/formatting";
import type { Client } from "logos/client";
import { parseTimeExpression } from "logos/commands/interactions";
import { handleAutocompleteTimestamp } from "logos/commands/fragments/autocomplete/timestamp";

async function handleFastForwardAutocomplete(
client: Client,
interaction: Logos.Interaction<any, { timestamp: string }>,
): Promise<void> {
const timestamp = parseTimeExpression(client, interaction, interaction.parameters.timestamp);
if (timestamp === undefined) {
const strings = constants.contexts.autocompleteTimestamp({
localise: client.localise,
locale: interaction.locale,
});
client.respond(interaction, [{ name: trim(strings.autocomplete, 100), value: "" }]).ignore();

return;
}

client.respond(interaction, [{ name: timestamp[0], value: timestamp[1].toString() }]).ignore();
await handleAutocompleteTimestamp(client, interaction);
}

async function handleFastForward(
Expand Down
16 changes: 2 additions & 14 deletions source/library/commands/handlers/music/rewind.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { trim } from "logos:constants/formatting";
import type { Client } from "logos/client";
import { parseTimeExpression } from "logos/commands/interactions";
import { handleAutocompleteTimestamp } from "logos/commands/fragments/autocomplete/timestamp";

async function handleRewindAutocomplete(
client: Client,
interaction: Logos.Interaction<any, { timestamp: string }>,
): Promise<void> {
const timestamp = parseTimeExpression(client, interaction, interaction.parameters.timestamp);
if (timestamp === undefined) {
const strings = constants.contexts.autocompleteTimestamp({
localise: client.localise,
locale: interaction.locale,
});
client.respond(interaction, [{ name: trim(strings.autocomplete, 100), value: "" }]).ignore();

return;
}

client.respond(interaction, [{ name: timestamp[0], value: timestamp[1].toString() }]).ignore();
await handleAutocompleteTimestamp(client, interaction);
}

async function handleRewind(client: Client, interaction: Logos.Interaction<any, { timestamp: string }>): Promise<void> {
Expand Down
16 changes: 2 additions & 14 deletions source/library/commands/handlers/music/skip-to.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { trim } from "logos:constants/formatting";
import type { Client } from "logos/client";
import { parseTimeExpression } from "logos/commands/interactions";
import { handleAutocompleteTimestamp } from "logos/commands/fragments/autocomplete/timestamp";

async function handleSkipToTimestampAutocomplete(
client: Client,
interaction: Logos.Interaction<any, { timestamp: string }>,
): Promise<void> {
const timestamp = parseTimeExpression(client, interaction, interaction.parameters.timestamp);
if (timestamp === undefined) {
const strings = constants.contexts.autocompleteTimestamp({
localise: client.localise,
locale: interaction.locale,
});
client.respond(interaction, [{ name: trim(strings.autocomplete, 100), value: "" }]).ignore();

return;
}

client.respond(interaction, [{ name: timestamp[0], value: timestamp[1].toString() }]).ignore();
await handleAutocompleteTimestamp(client, interaction);
}

async function handleSkipToTimestamp(
Expand Down
12 changes: 6 additions & 6 deletions source/library/commands/handlers/slowmode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { timestamp } from "logos:constants/formatting";
import { getSlowmodeDelayByLevel, getSlowmodeLevelByDelay, isValidSlowmodeLevel } from "logos:constants/slowmode";
import type { Client } from "logos/client";
import { handleSimpleAutocomplete } from "logos/commands/fragments/autocomplete/simple.ts";
import { Guild } from "logos/models/guild";

const lastUseByGuildId = new Map<bigint, number>();
Expand All @@ -10,12 +11,11 @@ async function handleToggleSlowmodeAutocomplete(
interaction: Logos.Interaction<any, { level: string }>,
): Promise<void> {
const strings = constants.contexts.slowmodeLevel({ localise: client.localise, locale: interaction.locale });
const levelLowercase = interaction.parameters.level.trim().toLowerCase();
const choices = constants.slowmode.levels
.map((level) => ({ name: strings.level(level), value: level }))
.filter((choice) => choice.name.toLowerCase().includes(levelLowercase));

client.respond(interaction, choices).ignore();
await handleSimpleAutocomplete(client, interaction, {
query: interaction.parameters.level,
elements: Object.entries(constants.slowmode.levels),
getOption: ([_, level]) => ({ name: strings.level(level), value: level }),
});
}

async function handleToggleSlowmode(
Expand Down
4 changes: 2 additions & 2 deletions source/library/commands/handlers/word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import type { DefinitionField, DictionaryEntry, ExpressionField } from "logos/ad
import type { Client } from "logos/client";
import { InteractionCollector } from "logos/collectors";
import { WordSourceNotice } from "logos/commands/components/source-notices/word-source-notice";
import { autocompleteLanguage } from "logos/commands/fragments/autocomplete/language";
import { handleAutocompleteLanguage } from "logos/commands/fragments/autocomplete/language";

async function handleFindWordAutocomplete(
client: Client,
interaction: Logos.Interaction<any, { language: string | undefined }>,
): Promise<void> {
await autocompleteLanguage(client, interaction);
await handleAutocompleteLanguage(client, interaction);
}

/** Allows the user to look up a word and get information about it. */
Expand Down

0 comments on commit c61a961

Please sign in to comment.