Skip to content

Commit

Permalink
feat(commandManager): support multiple application commands from a si…
Browse files Browse the repository at this point in the history
…ngle command class
  • Loading branch information
virtual-designer committed Aug 3, 2023
1 parent b6b8a8b commit 92abe80
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions scripts/deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function makeContextMenuCommandBuilder(command: any) {
if (!command.supportsInteractions) continue;

commands.push(
...(command.otherApplicationCommandBuilders ?? []),
command.applicationCommandType === ApplicationCommandType.ChatInput
? makeSlashCommandBuilder(command)
: makeContextMenuCommandBuilder(command)
Expand Down
2 changes: 2 additions & 0 deletions src/core/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ApplicationCommandType,
CacheType,
ChatInputCommandInteraction,
ContextMenuCommandBuilder,
ContextMenuCommandInteraction,
GuildMember,
InteractionDeferReplyOptions,
Expand Down Expand Up @@ -103,6 +104,7 @@ export default abstract class Command {
| Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">;

public readonly applicationCommandType: ApplicationCommandType = ApplicationCommandType.ChatInput;
public readonly otherApplicationCommandBuilders: (ContextMenuCommandBuilder | SlashCommandBuilder)[] = [];

public readonly subcommands: string[] = [];

Expand Down
9 changes: 7 additions & 2 deletions src/services/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface CommandContext {

export interface LegacyCommandContext extends CommandContext {
isLegacy: true;
isContextMenu: false;
argv: string[];
args: string[];
parsedArgs: any[];
Expand All @@ -41,11 +42,13 @@ export interface LegacyCommandContext extends CommandContext {

export interface ChatInputCommandContext extends CommandContext {
isLegacy: false;
isContextMenu: false;
options: ChatInputCommandInteraction["options"];
}

export interface ContextMenuCommandContext extends CommandContext {
isLegacy: false;
isContextMenu: true;
options: ContextMenuCommandInteraction["options"];
}

Expand Down Expand Up @@ -80,6 +83,7 @@ export default class CommandManager extends Service {
config,
parsedArgs: [],
parsedNamedArgs: {},
isContextMenu: false,
has(arg: string) {
return this.args.includes(arg);
}
Expand Down Expand Up @@ -113,8 +117,9 @@ export default class CommandManager extends Service {
.run(interaction, {
isLegacy: false,
config,
options: interaction.options
} as ContextMenuCommandContext)
options: interaction.options,
isContextMenu: interaction.isContextMenuCommand()
} as ContextMenuCommandContext | ChatInputCommandContext)
.then(result => {
if (result && typeof result === "object" && "__reply" in result && result.__reply === true) {
interaction.reply(result as any).catch(console.error);
Expand Down

0 comments on commit 92abe80

Please sign in to comment.