feat(builders): add new command permissions v2 (#7861)

This commit is contained in:
Vlad Frangu
2022-05-13 12:33:40 +03:00
committed by GitHub
parent aed687b09f
commit de3f1573f0
4 changed files with 147 additions and 18 deletions

View File

@@ -2,9 +2,17 @@ import type {
ApplicationCommandType,
LocaleString,
LocalizationMap,
Permissions,
RESTPostAPIApplicationCommandsJSONBody,
} from 'discord-api-types/v10';
import { validateRequiredParameters, validateName, validateType, validateDefaultPermission } from './Assertions';
import {
validateRequiredParameters,
validateName,
validateType,
validateDefaultPermission,
validateDefaultMemberPermissions,
validateDMPermission,
} from './Assertions';
import { validateLocale, validateLocalizationMap } from '../slashCommands/Assertions';
export class ContextMenuCommandBuilder {
@@ -26,9 +34,21 @@ export class ContextMenuCommandBuilder {
/**
* Whether the command is enabled by default when the app is added to a guild
*
* @default true
* @deprecated This property is deprecated and will be removed in the future.
* You should use `setDefaultMemberPermissions` or `setDMPermission` instead.
*/
public readonly defaultPermission: boolean | undefined = undefined;
public readonly default_permission: boolean | undefined = undefined;
/**
* Set of permissions represented as a bit set for the command
*/
public readonly default_member_permissions: Permissions | null | undefined = undefined;
/**
* Indicates whether the command is available in DMs with the application, only for globally-scoped commands.
* By default, commands are visible.
*/
public readonly dm_permission: boolean | null | undefined = undefined;
/**
* Sets the name
@@ -66,12 +86,48 @@ export class ContextMenuCommandBuilder {
* @param value Whether or not to enable this command by default
*
* @see https://discord.com/developers/docs/interactions/application-commands#permissions
* @deprecated Use `setDefaultMemberPermissions` or `setDMPermission` instead.
*/
public setDefaultPermission(value: boolean) {
// Assert the value matches the conditions
validateDefaultPermission(value);
Reflect.set(this, 'defaultPermission', value);
Reflect.set(this, 'default_permission', value);
return this;
}
/**
* Sets the default permissions a member should have in order to run the command.
*
* **Note:** You can set this to `'0'` to disable the command by default.
*
* @param permissions The permissions bit field to set
*
* @see https://discord.com/developers/docs/interactions/application-commands#permissions
*/
public setDefaultMemberPermissions(permissions: Permissions | null | undefined) {
// Assert the value and parse it
const permissionValue = validateDefaultMemberPermissions(permissions);
Reflect.set(this, 'default_member_permissions', permissionValue);
return this;
}
/**
* Sets if the command is available in DMs with the application, only for globally-scoped commands.
* By default, commands are visible.
*
* @param enabled If the command should be enabled in DMs
*
* @see https://discord.com/developers/docs/interactions/application-commands#permissions
*/
public setDMPermission(enabled: boolean | null | undefined) {
// Assert the value matches the conditions
validateDMPermission(enabled);
Reflect.set(this, 'dm_permission', enabled);
return this;
}
@@ -129,12 +185,7 @@ export class ContextMenuCommandBuilder {
validateLocalizationMap(this.name_localizations);
return {
name: this.name,
name_localizations: this.name_localizations,
type: this.type,
default_permission: this.defaultPermission,
};
return { ...this };
}
}