mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
refactor: builders (#10448)
BREAKING CHANGE: formatters export removed (prev. deprecated) BREAKING CHANGE: `SelectMenuBuilder` and `SelectMenuOptionBuilder` have been removed (prev. deprecated) BREAKING CHANGE: `EmbedBuilder` no longer takes camalCase options BREAKING CHANGE: `ActionRowBuilder` now has specialized `[add/set]X` methods as opposed to the current `[add/set]Components` BREAKING CHANGE: Removed `equals` methods BREAKING CHANGE: Sapphire -> zod for validation BREAKING CHANGE: Removed the ability to pass `null`/`undefined` to clear fields, use `clearX()` instead BREAKING CHANGE: Renamed all "slash command" symbols to instead use "chat input command" BREAKING CHANGE: Removed `ContextMenuCommandBuilder` in favor of `MessageCommandBuilder` and `UserCommandBuilder` BREAKING CHANGE: Removed support for passing the "string key"s of enums BREAKING CHANGE: Removed `Button` class in favor for specialized classes depending on the style BREAKING CHANGE: Removed nested `addX` styled-methods in favor of plural `addXs` Co-authored-by: Vlad Frangu <me@vladfrangu.dev> Co-authored-by: Almeida <github@almeidx.dev>
This commit is contained in:
83
packages/builders/src/interactions/commands/Command.ts
Normal file
83
packages/builders/src/interactions/commands/Command.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import type { JSONEncodable } from '@discordjs/util';
|
||||
import type {
|
||||
ApplicationIntegrationType,
|
||||
InteractionContextType,
|
||||
Permissions,
|
||||
RESTPostAPIApplicationCommandsJSONBody,
|
||||
} from 'discord-api-types/v10';
|
||||
import type { RestOrArray } from '../../util/normalizeArray.js';
|
||||
import { normalizeArray } from '../../util/normalizeArray.js';
|
||||
|
||||
export interface CommandData
|
||||
extends Partial<
|
||||
Pick<
|
||||
RESTPostAPIApplicationCommandsJSONBody,
|
||||
'contexts' | 'default_member_permissions' | 'integration_types' | 'nsfw'
|
||||
>
|
||||
> {}
|
||||
|
||||
export abstract class CommandBuilder<Command extends RESTPostAPIApplicationCommandsJSONBody>
|
||||
implements JSONEncodable<Command>
|
||||
{
|
||||
protected declare readonly data: CommandData;
|
||||
|
||||
/**
|
||||
* Sets the contexts of this command.
|
||||
*
|
||||
* @param contexts - The contexts
|
||||
*/
|
||||
public setContexts(...contexts: RestOrArray<InteractionContextType>) {
|
||||
this.data.contexts = normalizeArray(contexts);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the integration types of this command.
|
||||
*
|
||||
* @param integrationTypes - The integration types
|
||||
*/
|
||||
public setIntegrationTypes(...integrationTypes: RestOrArray<ApplicationIntegrationType>) {
|
||||
this.data.integration_types = normalizeArray(integrationTypes);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default permissions a member should have in order to run the command.
|
||||
*
|
||||
* @remarks
|
||||
* You can set this to `'0'` to disable the command by default.
|
||||
* @param permissions - The permissions bit field to set
|
||||
* @see {@link https://discord.com/developers/docs/interactions/application-commands#permissions}
|
||||
*/
|
||||
public setDefaultMemberPermissions(permissions: Permissions | bigint | number) {
|
||||
this.data.default_member_permissions = typeof permissions === 'string' ? permissions : permissions.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the default permissions a member should have in order to run the command.
|
||||
*/
|
||||
public clearDefaultMemberPermissions() {
|
||||
this.data.default_member_permissions = undefined;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether this command is NSFW.
|
||||
*
|
||||
* @param nsfw - Whether this command is NSFW
|
||||
*/
|
||||
public setNSFW(nsfw = true) {
|
||||
this.data.nsfw = nsfw;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes this builder to API-compatible JSON data.
|
||||
*
|
||||
* Note that by disabling validation, there is no guarantee that the resulting object will be valid.
|
||||
*
|
||||
* @param validationOverride - Force validation to run/not run regardless of your global preference
|
||||
*/
|
||||
public abstract toJSON(validationOverride?: boolean): Command;
|
||||
}
|
||||
Reference in New Issue
Block a user