Skip to content

Commit

Permalink
feat(unicode): add message context command
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Nov 2, 2024
1 parent e00f93e commit ae204dc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/commands/unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) =>
Expand Down Expand Up @@ -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<SearchOptions>) {
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/i18n/LanguageKeys/Commands/Unicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
1 change: 1 addition & 0 deletions src/locales/en-US/commands/unicode.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit ae204dc

Please sign in to comment.