Skip to content

Commit 5119cd8

Browse files
committed
feat: context menu name enhancements
1 parent 888db59 commit 5119cd8

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

apps/test-bot/src/app/commands/(general)/avatar.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ChatInputCommand,
44
UserContextMenuCommand,
55
MessageContextMenuCommand,
6+
CommandMetadata,
67
} from 'commandkit';
78
import { ApplicationCommandOptionType } from 'discord.js';
89

@@ -18,6 +19,13 @@ export const command = {
1819
],
1920
};
2021

22+
export const metadata: CommandMetadata = {
23+
nameAliases: {
24+
user: 'View Avatar',
25+
message: "View Author's Avatar",
26+
},
27+
};
28+
2129
export const userContextMenu: UserContextMenuCommand = async (ctx) => {
2230
const target = ctx.interaction.targetUser;
2331

packages/commandkit/src/app/handlers/AppCommandHandler.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { CommandRegistrar } from '../register/CommandRegistrar';
2929
import { Command, Middleware } from '../router';
3030
import { getConfig } from '../../config/config';
3131
import { beforeExecute, middlewareId } from '../middlewares/permissions';
32+
import { isInteractionSource } from '../commands/helpers';
3233

3334
const KNOWN_NON_HANDLER_KEYS = [
3435
'command',
@@ -516,7 +517,14 @@ export class AppCommandHandler {
516517
}
517518

518519
// Find the command by name
519-
const loadedCommand = this.findCommandByName(cmdName);
520+
const hint = isInteractionSource(source)
521+
? source.isUserContextMenuCommand()
522+
? 'user'
523+
: source.isMessageContextMenuCommand()
524+
? 'message'
525+
: undefined
526+
: undefined;
527+
const loadedCommand = this.findCommandByName(cmdName, hint);
520528
if (!loadedCommand) return null;
521529

522530
// If this is a guild specific command, check if we're in the right guild
@@ -570,10 +578,22 @@ export class AppCommandHandler {
570578
/**
571579
* Finds a command by name.
572580
* @param name - The command name to search for
581+
* @param hint - The hint for the command type (user or message)
573582
* @returns The loaded command or null if not found
574583
*/
575-
private findCommandByName(name: string): LoadedCommand | null {
584+
private findCommandByName(
585+
name: string,
586+
hint?: 'user' | 'message',
587+
): LoadedCommand | null {
576588
for (const [, loadedCommand] of this.loadedCommands) {
589+
if (hint) {
590+
const nameAliases = loadedCommand.data.metadata?.nameAliases;
591+
592+
if (nameAliases && nameAliases[hint] === name) {
593+
return loadedCommand;
594+
}
595+
}
596+
577597
if (loadedCommand.data.command.name === name) {
578598
return loadedCommand;
579599
}
@@ -584,6 +604,7 @@ export class AppCommandHandler {
584604
return loadedCommand;
585605
}
586606
}
607+
587608
return null;
588609
}
589610

@@ -870,10 +891,14 @@ export class AppCommandHandler {
870891
/**
871892
* Gets the metadata for a command.
872893
* @param command - The command name to get metadata for
894+
* @param hint - The hint for the command type (user or message)
873895
* @returns The command metadata or null if not found
874896
*/
875-
public getMetadataFor(command: string): CommandMetadata | null {
876-
const loadedCommand = this.findCommandByName(command);
897+
public getMetadataFor(
898+
command: string,
899+
hint?: 'user' | 'message',
900+
): CommandMetadata | null {
901+
const loadedCommand = this.findCommandByName(command, hint);
877902
if (!loadedCommand) return null;
878903

879904
return (loadedCommand.metadata ??= {

packages/i18n/src/i18n.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ export class I18nPlugin extends RuntimePlugin<LocalizationPluginOptions> {
428428
command.name_localizations ??= {};
429429
command.name_localizations[locale] = translationBasic.name;
430430
}
431+
432+
// context menu commands don't have a description
433+
delete command.description;
434+
delete command.description_localizations;
431435
}
432436
}
433437
}

0 commit comments

Comments
 (0)