Skip to content
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
8 changes: 8 additions & 0 deletions apps/test-bot/src/app/commands/(general)/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ChatInputCommand,
UserContextMenuCommand,
MessageContextMenuCommand,
CommandMetadata,
} from 'commandkit';
import { ApplicationCommandOptionType } from 'discord.js';

Expand All @@ -18,6 +19,13 @@ export const command = {
],
};

export const metadata: CommandMetadata = {
nameAliases: {
user: 'View Avatar',
message: "View Author's Avatar",
},
};

export const userContextMenu: UserContextMenuCommand = async (ctx) => {
const target = ctx.interaction.targetUser;

Expand Down
33 changes: 29 additions & 4 deletions packages/commandkit/src/app/handlers/AppCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { CommandRegistrar } from '../register/CommandRegistrar';
import { Command, Middleware } from '../router';
import { getConfig } from '../../config/config';
import { beforeExecute, middlewareId } from '../middlewares/permissions';
import { isInteractionSource } from '../commands/helpers';

const KNOWN_NON_HANDLER_KEYS = [
'command',
Expand Down Expand Up @@ -516,7 +517,14 @@ export class AppCommandHandler {
}

// Find the command by name
const loadedCommand = this.findCommandByName(cmdName);
const hint = isInteractionSource(source)
? source.isUserContextMenuCommand()
? 'user'
: source.isMessageContextMenuCommand()
? 'message'
: undefined
: undefined;
const loadedCommand = this.findCommandByName(cmdName, hint);
if (!loadedCommand) return null;

// If this is a guild specific command, check if we're in the right guild
Expand Down Expand Up @@ -570,10 +578,22 @@ export class AppCommandHandler {
/**
* Finds a command by name.
* @param name - The command name to search for
* @param hint - The hint for the command type (user or message)
* @returns The loaded command or null if not found
*/
private findCommandByName(name: string): LoadedCommand | null {
private findCommandByName(
name: string,
hint?: 'user' | 'message',
): LoadedCommand | null {
for (const [, loadedCommand] of this.loadedCommands) {
if (hint) {
const nameAliases = loadedCommand.data.metadata?.nameAliases;

if (nameAliases && nameAliases[hint] === name) {
return loadedCommand;
}
}

if (loadedCommand.data.command.name === name) {
return loadedCommand;
}
Expand All @@ -584,6 +604,7 @@ export class AppCommandHandler {
return loadedCommand;
}
}

return null;
}

Expand Down Expand Up @@ -870,10 +891,14 @@ export class AppCommandHandler {
/**
* Gets the metadata for a command.
* @param command - The command name to get metadata for
* @param hint - The hint for the command type (user or message)
* @returns The command metadata or null if not found
*/
public getMetadataFor(command: string): CommandMetadata | null {
const loadedCommand = this.findCommandByName(command);
public getMetadataFor(
command: string,
hint?: 'user' | 'message',
): CommandMetadata | null {
const loadedCommand = this.findCommandByName(command, hint);
if (!loadedCommand) return null;

return (loadedCommand.metadata ??= {
Expand Down
4 changes: 4 additions & 0 deletions packages/i18n/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ export class I18nPlugin extends RuntimePlugin<LocalizationPluginOptions> {
command.name_localizations ??= {};
command.name_localizations[locale] = translationBasic.name;
}

// context menu commands don't have a description
delete command.description;
delete command.description_localizations;
}
}
}
Expand Down