feat: add user-installable apps support (#10348)

* feat(SlashCommandBuilder): `addContexts()` and `addIntegrationTypes()`

* Add methods to ContextMenuCommandbuilder

* Fix JSDoc

* Use `setX` over `addX`

* Fix tests

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
TÆMBØ 2024-06-27 11:56:47 -07:00 committed by GitHub
parent b8397b24e5
commit 9c76bbea17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 211 additions and 5 deletions

View file

@ -1,4 +1,4 @@
import { PermissionFlagsBits } from 'discord-api-types/v10';
import { ApplicationIntegrationType, InteractionContextType, PermissionFlagsBits } from 'discord-api-types/v10';
import { describe, test, expect } from 'vitest';
import { ContextMenuCommandAssertions, ContextMenuCommandBuilder } from '../../src/index.js';
@ -144,5 +144,51 @@ describe('Context Menu Commands', () => {
expect(() => getBuilder().setDefaultMemberPermissions(1.1)).toThrowError();
});
});
describe('contexts', () => {
test('GIVEN a builder with valid contexts THEN does not throw an error', () => {
expect(() =>
getBuilder().setContexts([InteractionContextType.Guild, InteractionContextType.BotDM]),
).not.toThrowError();
expect(() =>
getBuilder().setContexts(InteractionContextType.Guild, InteractionContextType.BotDM),
).not.toThrowError();
});
test('GIVEN a builder with invalid contexts THEN does throw an error', () => {
// @ts-expect-error: Invalid contexts
expect(() => getBuilder().setContexts(999)).toThrowError();
// @ts-expect-error: Invalid contexts
expect(() => getBuilder().setContexts([999, 998])).toThrowError();
});
});
describe('integration types', () => {
test('GIVEN a builder with valid integraton types THEN does not throw an error', () => {
expect(() =>
getBuilder().setIntegrationTypes([
ApplicationIntegrationType.GuildInstall,
ApplicationIntegrationType.UserInstall,
]),
).not.toThrowError();
expect(() =>
getBuilder().setIntegrationTypes(
ApplicationIntegrationType.GuildInstall,
ApplicationIntegrationType.UserInstall,
),
).not.toThrowError();
});
test('GIVEN a builder with invalid integration types THEN does throw an error', () => {
// @ts-expect-error: Invalid integration types
expect(() => getBuilder().setIntegrationTypes(999)).toThrowError();
// @ts-expect-error: Invalid integration types
expect(() => getBuilder().setIntegrationTypes([999, 998])).toThrowError();
});
});
});
});

View file

@ -1,4 +1,10 @@
import { ChannelType, PermissionFlagsBits, type APIApplicationCommandOptionChoice } from 'discord-api-types/v10';
import {
ApplicationIntegrationType,
ChannelType,
InteractionContextType,
PermissionFlagsBits,
type APIApplicationCommandOptionChoice,
} from 'discord-api-types/v10';
import { describe, test, expect } from 'vitest';
import {
SlashCommandAssertions,
@ -532,5 +538,51 @@ describe('Slash Commands', () => {
expect(() => getBuilder().addChannelOption(getChannelOption()).setDMPermission(false)).not.toThrowError();
});
});
describe('contexts', () => {
test('GIVEN a builder with valid contexts THEN does not throw an error', () => {
expect(() =>
getBuilder().setContexts([InteractionContextType.Guild, InteractionContextType.BotDM]),
).not.toThrowError();
expect(() =>
getBuilder().setContexts(InteractionContextType.Guild, InteractionContextType.BotDM),
).not.toThrowError();
});
test('GIVEN a builder with invalid contexts THEN does throw an error', () => {
// @ts-expect-error: Invalid contexts
expect(() => getBuilder().setContexts(999)).toThrowError();
// @ts-expect-error: Invalid contexts
expect(() => getBuilder().setContexts([999, 998])).toThrowError();
});
});
describe('integration types', () => {
test('GIVEN a builder with valid integraton types THEN does not throw an error', () => {
expect(() =>
getBuilder().setIntegrationTypes([
ApplicationIntegrationType.GuildInstall,
ApplicationIntegrationType.UserInstall,
]),
).not.toThrowError();
expect(() =>
getBuilder().setIntegrationTypes(
ApplicationIntegrationType.GuildInstall,
ApplicationIntegrationType.UserInstall,
),
).not.toThrowError();
});
test('GIVEN a builder with invalid integration types THEN does throw an error', () => {
// @ts-expect-error: Invalid integration types
expect(() => getBuilder().setIntegrationTypes(999)).toThrowError();
// @ts-expect-error: Invalid integration types
expect(() => getBuilder().setIntegrationTypes([999, 998])).toThrowError();
});
});
});
});

View file

@ -1,5 +1,5 @@
import { s } from '@sapphire/shapeshift';
import { ApplicationCommandType } from 'discord-api-types/v10';
import { ApplicationCommandType, ApplicationIntegrationType, InteractionContextType } from 'discord-api-types/v10';
import { isValidationEnabled } from '../../util/validation.js';
import type { ContextMenuCommandType } from './ContextMenuCommandBuilder.js';
@ -49,3 +49,11 @@ const memberPermissionPredicate = s.union(
export function validateDefaultMemberPermissions(permissions: unknown) {
return memberPermissionPredicate.parse(permissions);
}
export const contextsPredicate = s.array(
s.nativeEnum(InteractionContextType).setValidationEnabled(isValidationEnabled),
);
export const integrationTypesPredicate = s.array(
s.nativeEnum(ApplicationIntegrationType).setValidationEnabled(isValidationEnabled),
);

View file

@ -1,10 +1,14 @@
import type {
ApplicationCommandType,
ApplicationIntegrationType,
InteractionContextType,
LocaleString,
LocalizationMap,
Permissions,
RESTPostAPIContextMenuApplicationCommandsJSONBody,
} from 'discord-api-types/v10';
import type { RestOrArray } from '../../util/normalizeArray.js';
import { normalizeArray } from '../../util/normalizeArray.js';
import { validateLocale, validateLocalizationMap } from '../slashCommands/Assertions.js';
import {
validateRequiredParameters,
@ -13,6 +17,8 @@ import {
validateDefaultPermission,
validateDefaultMemberPermissions,
validateDMPermission,
contextsPredicate,
integrationTypesPredicate,
} from './Assertions.js';
/**
@ -39,6 +45,11 @@ export class ContextMenuCommandBuilder {
*/
public readonly type: ContextMenuCommandType = undefined!;
/**
* The contexts for this command.
*/
public readonly contexts?: InteractionContextType[];
/**
* Whether this command is enabled by default when the application is added to a guild.
*
@ -59,6 +70,33 @@ export class ContextMenuCommandBuilder {
*/
public readonly dm_permission: boolean | undefined = undefined;
/**
* The integration types for this command.
*/
public readonly integration_types?: ApplicationIntegrationType[];
/**
* Sets the contexts of this command.
*
* @param contexts - The contexts
*/
public setContexts(...contexts: RestOrArray<InteractionContextType>) {
Reflect.set(this, 'contexts', contextsPredicate.parse(normalizeArray(contexts)));
return this;
}
/**
* Sets integration types of this command.
*
* @param integrationTypes - The integration types
*/
public setIntegrationTypes(...integrationTypes: RestOrArray<ApplicationIntegrationType>) {
Reflect.set(this, 'integration_types', integrationTypesPredicate.parse(normalizeArray(integrationTypes)));
return this;
}
/**
* Sets the name of this command.
*

View file

@ -1,5 +1,11 @@
import { s } from '@sapphire/shapeshift';
import { Locale, type APIApplicationCommandOptionChoice, type LocalizationMap } from 'discord-api-types/v10';
import {
ApplicationIntegrationType,
InteractionContextType,
Locale,
type APIApplicationCommandOptionChoice,
type LocalizationMap,
} from 'discord-api-types/v10';
import { isValidationEnabled } from '../../util/validation.js';
import type { ToAPIApplicationCommandOptions } from './SlashCommandBuilder.js';
import type { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from './SlashCommandSubcommands.js';
@ -98,3 +104,11 @@ export function validateDefaultMemberPermissions(permissions: unknown) {
export function validateNSFW(value: unknown): asserts value is boolean {
booleanPredicate.parse(value);
}
export const contextsPredicate = s.array(
s.nativeEnum(InteractionContextType).setValidationEnabled(isValidationEnabled),
);
export const integrationTypesPredicate = s.array(
s.nativeEnum(ApplicationIntegrationType).setValidationEnabled(isValidationEnabled),
);

View file

@ -1,4 +1,10 @@
import type { APIApplicationCommandOption, LocalizationMap, Permissions } from 'discord-api-types/v10';
import type {
APIApplicationCommandOption,
ApplicationIntegrationType,
InteractionContextType,
LocalizationMap,
Permissions,
} from 'discord-api-types/v10';
import { mix } from 'ts-mixer';
import { SharedNameAndDescription } from './mixins/NameAndDescription.js';
import { SharedSlashCommand } from './mixins/SharedSlashCommand.js';
@ -35,6 +41,11 @@ export class SlashCommandBuilder {
*/
public readonly options: ToAPIApplicationCommandOptions[] = [];
/**
* The contexts for this command.
*/
public readonly contexts?: InteractionContextType[];
/**
* Whether this command is enabled by default when the application is added to a guild.
*
@ -55,6 +66,11 @@ export class SlashCommandBuilder {
*/
public readonly dm_permission: boolean | undefined = undefined;
/**
* The integration types for this command.
*/
public readonly integration_types?: ApplicationIntegrationType[];
/**
* Whether this command is NSFW.
*/

View file

@ -1,9 +1,15 @@
import type {
ApplicationIntegrationType,
InteractionContextType,
LocalizationMap,
Permissions,
RESTPostAPIChatInputApplicationCommandsJSONBody,
} from 'discord-api-types/v10';
import type { RestOrArray } from '../../../util/normalizeArray.js';
import { normalizeArray } from '../../../util/normalizeArray.js';
import {
contextsPredicate,
integrationTypesPredicate,
validateDMPermission,
validateDefaultMemberPermissions,
validateDefaultPermission,
@ -27,6 +33,8 @@ export class SharedSlashCommand {
public readonly options: ToAPIApplicationCommandOptions[] = [];
public readonly contexts?: InteractionContextType[];
/**
* @deprecated Use {@link SharedSlashCommand.setDefaultMemberPermissions} or {@link SharedSlashCommand.setDMPermission} instead.
*/
@ -36,8 +44,32 @@ export class SharedSlashCommand {
public readonly dm_permission: boolean | undefined = undefined;
public readonly integration_types?: ApplicationIntegrationType[];
public readonly nsfw: boolean | undefined = undefined;
/**
* Sets the contexts of this command.
*
* @param contexts - The contexts
*/
public setContexts(...contexts: RestOrArray<InteractionContextType>) {
Reflect.set(this, 'contexts', contextsPredicate.parse(normalizeArray(contexts)));
return this;
}
/**
* Sets the integration types of this command.
*
* @param integrationTypes - The integration types
*/
public setIntegrationTypes(...integrationTypes: RestOrArray<ApplicationIntegrationType>) {
Reflect.set(this, 'integration_types', integrationTypesPredicate.parse(normalizeArray(integrationTypes)));
return this;
}
/**
* Sets whether the command is enabled by default when the application is added to a guild.
*