Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(unicode): add message context command #273

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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