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

Commit

Permalink
feat(SlashCommandBuilder): create setDefaultPermission function (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
  • Loading branch information
Fyko and vladfrangu committed Aug 24, 2021
1 parent 1563991 commit 5d53759
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/interactions/slashCommands/Assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export function validateDescription(description: unknown): asserts description i
ow(description, 'description', descriptionPredicate);
}

const defaultPermissionPredicate = ow.boolean;

export function validateDefaultPermission(value: unknown): asserts value is boolean {
ow(value, 'default_permission', defaultPermissionPredicate);
}

const maxArrayLengthPredicate = ow.array.maxLength(25);

export function validateMaxOptionsLength(options: unknown): asserts options is ToAPIApplicationCommandOptions[] {
Expand Down
33 changes: 31 additions & 2 deletions src/interactions/slashCommands/SlashCommandBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { APIApplicationCommandOption } from 'discord-api-types/v9';
import { mix } from 'ts-mixer';
import { assertReturnOfBuilder, validateMaxOptionsLength, validateRequiredParameters } from './Assertions';
import { SharedNameAndDescription } from './mixins/NameAndDescription';
import {
assertReturnOfBuilder,
validateDefaultPermission,
validateMaxOptionsLength,
validateRequiredParameters,
} from './Assertions';
import { SharedSlashCommandOptions } from './mixins/CommandOptions';
import { SharedNameAndDescription } from './mixins/NameAndDescription';
import { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from './SlashCommandSubcommands';

@mix(SharedSlashCommandOptions, SharedNameAndDescription)
Expand All @@ -22,6 +27,12 @@ export class SlashCommandBuilder {
*/
public readonly options: ToAPIApplicationCommandOptions[] = [];

/**
* Whether the command is enabled by default when the app is added to a guild
* @default true
*/
public readonly defaultPermission: boolean | undefined = undefined;

/**
* Returns the final data that should be sent to Discord.
*
Expand All @@ -33,9 +44,27 @@ export class SlashCommandBuilder {
name: this.name,
description: this.description,
options: this.options.map((option) => option.toJSON()),
default_permission: this.defaultPermission,
};
}

/**
* Sets whether the command is enabled by default when the application is added to a guild.
*
* **Note**: If set to `false`, you will have to later have to `PUT` the permissions for this command.
* @param value Whether or not to enable this command by default
*
* @see https://discord.com/developers/docs/interactions/slash-commands#permissions
*/
public setDefaultPermission(value: boolean) {
// Assert the value matches the conditions
validateDefaultPermission(value);

Reflect.set(this, 'defaultPermission', value);

return this;
}

/**
* Adds a new subcommand group to this command
* @param input A function that returns a subcommand group builder, or an already built builder
Expand Down
12 changes: 12 additions & 0 deletions tests/SlashCommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ describe('Slash Commands', () => {
).toThrowError();
});

test('GIVEN valid default_permission THEN does not throw error', () => {
expect(() => SlashCommandAssertions.validateDefaultPermission(true)).not.toThrowError();
});

test('GIVEN invalid default_permission THEN throw error', () => {
expect(() => SlashCommandAssertions.validateDefaultPermission(null)).toThrowError();
});

test('GIVEN valid array of options or choices THEN does not throw error', () => {
expect(() => SlashCommandAssertions.validateMaxOptionsLength([])).not.toThrowError();

Expand Down Expand Up @@ -206,6 +214,10 @@ describe('Slash Commands', () => {
// @ts-expect-error Checking if not providing anything, or an invalid return type causes an error
expect(() => getBuilder().addBooleanOption(() => new Collection())).toThrowError();
});

test('GIVEN valid builder with defaultPermission false THEN does not throw error', () => {
expect(() => getBuilder().setName('foo').setDescription('foo').setDefaultPermission(false)).not.toThrowError();
});
});

describe('Builder with subcommand (group) options', () => {
Expand Down

0 comments on commit 5d53759

Please sign in to comment.