Skip to content

Commit

Permalink
feat(anilist): add hide-description option (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet authored Oct 28, 2024
1 parent 8a13b57 commit 38fb7b2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
11 changes: 9 additions & 2 deletions src/commands/search/anime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ const Root = LanguageKeys.Commands.AniList;
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall)
.setContexts(InteractionContextType.Guild, InteractionContextType.BotDM, InteractionContextType.PrivateChannel)
.addIntegerOption((option) => applyLocalizedBuilder(option, Root.Anime.OptionsAnime).setRequired(true).setAutocomplete(true))
.addBooleanOption((option) => applyLocalizedBuilder(option, Root.OptionsHideDescription))
.addBooleanOption((option) => applyLocalizedBuilder(option, Root.OptionsHide))
)
export class UserCommand extends AnimeCommand<'anime'> {
public override async chatInputRun(interaction: Command.ChatInputInteraction, { anime, hide }: AnimeCommand.Arguments<'anime'>) {
return this.handleResult(interaction, await anilistAnimeGet(anime), 'anime', hide);
public override async chatInputRun(interaction: Command.ChatInputInteraction, options: AnimeCommand.Arguments<'anime'>) {
return this.handleResult({
interaction,
result: await anilistAnimeGet(options.anime),
kind: 'anime',
hideDescription: options['hide-description'],
hide: options.hide
});
}

protected override autocompleteFetch(options: AnimeCommand.AutocompleteArguments<'anime'>) {
Expand Down
11 changes: 9 additions & 2 deletions src/commands/search/manga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ const Root = LanguageKeys.Commands.AniList;
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall)
.setContexts(InteractionContextType.Guild, InteractionContextType.BotDM, InteractionContextType.PrivateChannel)
.addIntegerOption((option) => applyLocalizedBuilder(option, Root.Manga.OptionsManga).setRequired(true).setAutocomplete(true))
.addBooleanOption((option) => applyLocalizedBuilder(option, Root.OptionsHideDescription))
.addBooleanOption((option) => applyLocalizedBuilder(option, Root.OptionsHide))
)
export class UserCommand extends AnimeCommand<'manga'> {
public override async chatInputRun(interaction: Command.ChatInputInteraction, { manga, hide }: AnimeCommand.Arguments<'manga'>) {
return this.handleResult(interaction, await anilistMangaGet(manga), 'manga', hide);
public override async chatInputRun(interaction: Command.ChatInputInteraction, options: AnimeCommand.Arguments<'manga'>) {
return this.handleResult({
interaction,
result: await anilistMangaGet(options.manga),
kind: 'manga',
hideDescription: options['hide-description'],
hide: options.hide
});
}

protected override autocompleteFetch(options: AnimeCommand.AutocompleteArguments<'manga'>) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/LanguageKeys/Commands/AniList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { T } from '@skyra/http-framework-i18n';

export const OptionsHide = 'commands/anilist:optionsHide';
export const OptionsHideDescription = 'commands/anilist:optionsHideDescription';

export * as Anime from '#lib/i18n/LanguageKeys/Commands/AniList/Anime';
export * as Manga from '#lib/i18n/LanguageKeys/Commands/AniList/Manga';
Expand Down
36 changes: 22 additions & 14 deletions src/lib/structures/AnimeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,22 @@ export abstract class AnimeCommand<Kind extends 'anime' | 'manga'> extends Comma
return `${cutText(title, 100 - description.length)}${description}`;
}

protected handleResult(
interaction: Command.ChatInputInteraction,
result: Result<AnilistEntryTypeByKind<Kind> | null, FetchError>,
kind: Kind,
hide: boolean | null | undefined
) {
hide ??= false;
protected handleResult(options: HandlerOptions<Kind>) {
const { interaction, kind } = options;
const hide = options.hide ?? false;
const hideDescription = options.hideDescription ?? false;

const t = hide ? getSupportedUserLanguageT(interaction) : getSupportedLanguageT(interaction);
const response = result.match({
ok: (value) => (isNullishOrEmpty(value) ? this.createErrorResponse(interaction, kind) : this.createResponse(value, t, hide)),
const response = options.result.match({
ok: (value) =>
isNullishOrEmpty(value) ? this.createErrorResponse(interaction, kind) : this.createResponse(value, t, hideDescription, hide),
err: () => this.createErrorResponse(interaction, kind)
});
return interaction.reply(response);
}

protected createResponse(value: AnilistEntryTypeByKind<Kind>, t: TFunction, hide: boolean): MessageResponseOptions {
return { embeds: [this.createEmbed(value, t).toJSON()], flags: hide ? MessageFlags.Ephemeral : undefined };
protected createResponse(value: AnilistEntryTypeByKind<Kind>, t: TFunction, hideDescription: boolean, hide: boolean): MessageResponseOptions {
return { embeds: [this.createEmbed(value, t, hideDescription).toJSON()], flags: hide ? MessageFlags.Ephemeral : undefined };
}

protected createErrorResponse(interaction: Command.ChatInputInteraction, kind: Kind) {
Expand All @@ -84,7 +82,7 @@ export abstract class AnimeCommand<Kind extends 'anime' | 'manga'> extends Comma
options: AnimeCommand.AutocompleteArguments<Kind>
): Promise<Result<readonly AnilistEntryTypeByKind<Kind>[], FetchError>>;

private createEmbed(value: AnilistEntryTypeByKind<Kind>, t: TFunction) {
private createEmbed(value: AnilistEntryTypeByKind<Kind>, t: TFunction, hideDescription: boolean) {
const anilistTitles = t(Root.EmbedTitles);
const description = [
`**${anilistTitles.romajiName}**: ${value.title.romaji || t(LanguageKeys.Common.None)}`,
Expand Down Expand Up @@ -122,7 +120,7 @@ export abstract class AnimeCommand<Kind extends 'anime' | 'manga'> extends Comma
}
}

if (value.description) {
if (!hideDescription && value.description) {
description.push('', parseAniListDescription(value.description));
}

Expand Down Expand Up @@ -182,5 +180,15 @@ export namespace AnimeCommand {
export type AutocompleteArguments<Kind extends 'anime' | 'manga'> = AutocompleteInteractionArguments<MakeArguments<Kind, string>>;
}

export interface HandlerOptions<Kind extends 'anime' | 'manga'> {
interaction: Command.ChatInputInteraction;
result: Result<AnilistEntryTypeByKind<Kind> | null, FetchError>;
kind: Kind;
hideDescription: boolean | null | undefined;
hide: boolean | null | undefined;
}

type Pretty<Type extends object> = { [K in keyof Type]: Type[K] };
type MakeArguments<Kind extends 'anime' | 'manga', Value extends string | number> = Pretty<{ [key in Kind]: Value } & { hide?: boolean }>;
type MakeArguments<Kind extends 'anime' | 'manga', Value extends string | number> = Pretty<
{ [key in Kind]: Value } & { hide?: boolean; 'hide-description'?: boolean }
>;
2 changes: 2 additions & 0 deletions src/locales/en-US/commands/anilist.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"optionsHideName": "hide",
"optionsHideDescription": "Whether to hide the response (default: False)",
"optionsHideDescriptionName": "hide-description",
"optionsHideDescriptionDescription": "Whether to hide the description from the card (default: False)",
"embedTitles": {
"chapters": "Amount of chapters",
"countryOfOrigin": "Country of origin",
Expand Down

0 comments on commit 38fb7b2

Please # to comment.