Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
fix(SlashCommandBuilder): allow subcommands and groups to coexist at …
Browse files Browse the repository at this point in the history
…the root level (#26)

Co-authored-by: Keenser1 <36800359+Keenser1@users.noreply.github.com>
  • Loading branch information
Khasms and Khasms committed Aug 21, 2021
1 parent 2aecbe4 commit 0be4daf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 23 deletions.
17 changes: 2 additions & 15 deletions src/interactions/slashCommands/SlashCommandBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@ export class SlashCommandBuilder {
input:
| SlashCommandSubcommandGroupBuilder
| ((subcommandGroup: SlashCommandSubcommandGroupBuilder) => SlashCommandSubcommandGroupBuilder),
): SlashCommandSubcommandGroupsOnlyBuilder {
): SlashCommandSubcommandsOnlyBuilder {
const { options } = this;

// First, assert options conditions - we cannot have more than 25 options
validateMaxOptionsLength(options);

// Make sure there is no subcommand at the root level - if there is, throw
const hasSubcommands = options.some((item) => item instanceof SlashCommandSubcommandBuilder);
if (hasSubcommands) throw new RangeError(`You cannot mix subcommands and subcommand groups at the root level.`);

// Get the final result
const result = typeof input === 'function' ? input(new SlashCommandSubcommandGroupBuilder()) : input;

Expand All @@ -79,11 +75,6 @@ export class SlashCommandBuilder {
// First, assert options conditions - we cannot have more than 25 options
validateMaxOptionsLength(options);

// Make sure there is no subcommand at the root level - if there is, throw
const hasSubcommandGroups = options.some((item) => item instanceof SlashCommandSubcommandGroupBuilder);
if (hasSubcommandGroups)
throw new RangeError(`You cannot mix subcommands and subcommand groups at the root level.`);

// Get the final result
const result = typeof input === 'function' ? input(new SlashCommandSubcommandBuilder()) : input;

Expand All @@ -100,11 +91,7 @@ export interface SlashCommandBuilder extends SharedNameAndDescription, SharedSla

export interface SlashCommandSubcommandsOnlyBuilder
extends SharedNameAndDescription,
Pick<SlashCommandBuilder, 'toJSON' | 'addSubcommand'> {}

export interface SlashCommandSubcommandGroupsOnlyBuilder
extends SharedNameAndDescription,
Pick<SlashCommandBuilder, 'toJSON' | 'addSubcommandGroup'> {}
Pick<SlashCommandBuilder, 'toJSON' | 'addSubcommand' | 'addSubcommandGroup'> {}

export interface SlashCommandOptionsOnlyBuilder
extends SharedNameAndDescription,
Expand Down
9 changes: 1 addition & 8 deletions tests/SlashCommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,10 @@ describe('Slash Commands', () => {
).not.toThrowError();
});

test('GIVEN builder with a subcommand group that tries to add a subcommand THEN throw error', () => {
expect(() =>
// @ts-expect-error Checking if check works JS-side too
getNamedBuilder().addSubcommandGroup(getSubcommandGroup()).addSubcommand(getSubcommand()),
).toThrowError();
});

test('GIVEN builder with a subcommand that tries to add an invalid result THEN throw error', () => {
expect(() =>
// @ts-expect-error Checking if check works JS-side too
getNamedBuilder().addSubcommand(getSubcommand()).addSubcommandGroup(getSubcommandGroup()),
getNamedBuilder().addSubcommand(getSubcommand()).addInteger(getInteger()),
).toThrowError();
});

Expand Down

0 comments on commit 0be4daf

Please sign in to comment.