From ae204dc5abdf911dc906a9a86122380c4de830fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aura=20Rom=C3=A1n?= Date: Sat, 2 Nov 2024 09:29:08 +0100 Subject: [PATCH] feat(unicode): add message context command --- src/commands/unicode.ts | 37 +++++++++++++------ src/lib/i18n/LanguageKeys/Commands/Unicode.ts | 2 + src/locales/en-US/commands/unicode.json | 1 + 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/commands/unicode.ts b/src/commands/unicode.ts index 232007ae5..81f60fc05 100644 --- a/src/commands/unicode.ts +++ b/src/commands/unicode.ts @@ -9,8 +9,8 @@ import { getUnicodeInformationEmbeds, searchUnicode } from '#lib/unicode'; -import { Command, RegisterCommand, RegisterSubcommand } from '@skyra/http-framework'; -import { applyLocalizedBuilder, createSelectMenuChoiceName, getSupportedUserLanguageT } from '@skyra/http-framework-i18n'; +import { Command, RegisterCommand, RegisterMessageCommand, RegisterSubcommand, type TransformedArguments } from '@skyra/http-framework'; +import { applyLocalizedBuilder, applyNameLocalizedBuilder, createSelectMenuChoiceName, getSupportedUserLanguageT } from '@skyra/http-framework-i18n'; import { ApplicationIntegrationType, InteractionContextType, MessageFlags } from 'discord-api-types/v10'; const Root = LanguageKeys.Commands.Unicode; @@ -21,19 +21,21 @@ const Root = LanguageKeys.Commands.Unicode; .setContexts(InteractionContextType.Guild, InteractionContextType.BotDM, InteractionContextType.PrivateChannel) ) export class UserCommand extends Command { + @RegisterMessageCommand((builder) => + applyNameLocalizedBuilder(builder, Root.InspectMessageCharactersName) // + .setIntegrationTypes(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall) + .setContexts(InteractionContextType.Guild, InteractionContextType.BotDM, InteractionContextType.PrivateChannel) + ) + public message(interaction: Command.MessageInteraction, options: TransformedArguments.Message) { + return this.shared(interaction, options.message.content); + } + @RegisterSubcommand((builder) => applyLocalizedBuilder(builder, Root.Inspect) // .addStringOption((builder) => applyLocalizedBuilder(builder, Root.OptionsCharacter).setRequired(true)) ) - public async inspect(interaction: Command.ChatInputInteraction, options: InspectOptions) { - const ids = [...options.character]; - const t = getSupportedUserLanguageT(interaction); - const content = ids.length > 250 ? t(Root.TooManyCharacters) : ''; - const characters = ids.slice(0, 250); - - const components = characters.length > 10 ? getSelectMenuComponents(t, characters) : undefined; - const embeds = getUnicodeInformationEmbeds(t, characters.slice(0, 10)); - return interaction.reply({ content, embeds, components, flags: MessageFlags.Ephemeral }); + public inspect(interaction: Command.ChatInputInteraction, options: InspectOptions) { + return this.shared(interaction, options.character); } @RegisterSubcommand((builder) => @@ -76,7 +78,7 @@ export class UserCommand extends Command { ) ) public search(interaction: Command.ChatInputInteraction, options: SearchOptions) { - return this.inspect(interaction, { character: options.character }); + return this.shared(interaction, options.character); } public override async autocompleteRun(interaction: Command.AutocompleteInteraction, options: Command.AutocompleteArguments) { @@ -94,6 +96,17 @@ export class UserCommand extends Command { })) }); } + + private shared(interaction: Command.ChatInputInteraction | Command.MessageInteraction, input: string) { + const ids = [...input]; + const t = getSupportedUserLanguageT(interaction); + const content = ids.length > 250 ? t(Root.TooManyCharacters) : ''; + const characters = ids.slice(0, 250); + + const components = characters.length > 10 ? getSelectMenuComponents(t, characters) : undefined; + const embeds = getUnicodeInformationEmbeds(t, characters.slice(0, 10)); + return interaction.reply({ content, embeds, components, flags: MessageFlags.Ephemeral }); + } } interface InspectOptions { diff --git a/src/lib/i18n/LanguageKeys/Commands/Unicode.ts b/src/lib/i18n/LanguageKeys/Commands/Unicode.ts index 1878be086..8339e0c52 100644 --- a/src/lib/i18n/LanguageKeys/Commands/Unicode.ts +++ b/src/lib/i18n/LanguageKeys/Commands/Unicode.ts @@ -4,6 +4,8 @@ import { FT, T } from '@skyra/http-framework-i18n'; export const RootName = T('commands/unicode:name'); export const RootDescription = T('commands/unicode:description'); +export const InspectMessageCharactersName = T('commands/unicode:inspectMessageCharactersName'); + export const Inspect = 'commands/unicode:inspect'; export const Search = 'commands/unicode:search'; diff --git a/src/locales/en-US/commands/unicode.json b/src/locales/en-US/commands/unicode.json index f9f817345..cd3bda98f 100644 --- a/src/locales/en-US/commands/unicode.json +++ b/src/locales/en-US/commands/unicode.json @@ -1,6 +1,7 @@ { "name": "unicode", "description": "Get the unicode information for the given character", + "inspectMessageCharactersName": "Inspect Message Characters", "inspectName": "inspect", "inspectDescription": "Get the unicode information for the given character", "searchName": "search",