mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: Add support for API command types in ApplicationCommandManager (#6621)
This commit is contained in:
@@ -100,7 +100,7 @@ class ApplicationCommandManager extends CachedManager {
|
||||
|
||||
/**
|
||||
* Creates an application command.
|
||||
* @param {ApplicationCommandData} command The command
|
||||
* @param {ApplicationCommandData|APIApplicationCommand} command The command
|
||||
* @param {Snowflake} [guildId] The guild's id to create this command in,
|
||||
* ignored when using a {@link GuildApplicationCommandManager}
|
||||
* @returns {Promise<ApplicationCommand>}
|
||||
@@ -122,7 +122,7 @@ class ApplicationCommandManager extends CachedManager {
|
||||
|
||||
/**
|
||||
* Sets all the commands for this application or guild.
|
||||
* @param {ApplicationCommandData[]} commands The commands
|
||||
* @param {ApplicationCommandData[]|APIApplicationCommand[]} commands The commands
|
||||
* @param {Snowflake} [guildId] The guild's id to create the commands in,
|
||||
* ignored when using a {@link GuildApplicationCommandManager}
|
||||
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
|
||||
@@ -155,7 +155,7 @@ class ApplicationCommandManager extends CachedManager {
|
||||
/**
|
||||
* Edits an application command.
|
||||
* @param {ApplicationCommandResolvable} command The command to edit
|
||||
* @param {ApplicationCommandData} data The data to update the command with
|
||||
* @param {ApplicationCommandData|APIApplicationCommand} data The data to update the command with
|
||||
* @param {Snowflake} [guildId] The guild's id where the command registered,
|
||||
* ignored when using a {@link GuildApplicationCommandManager}
|
||||
* @returns {Promise<ApplicationCommand>}
|
||||
@@ -171,7 +171,9 @@ class ApplicationCommandManager extends CachedManager {
|
||||
const id = this.resolveId(command);
|
||||
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
|
||||
|
||||
const patched = await this.commandPath({ id, guildId }).patch({ data: this.constructor.transformCommand(data) });
|
||||
const patched = await this.commandPath({ id, guildId }).patch({
|
||||
data: this.constructor.transformCommand(data),
|
||||
});
|
||||
return this._add(patched, !guildId, guildId);
|
||||
}
|
||||
|
||||
@@ -200,7 +202,7 @@ class ApplicationCommandManager extends CachedManager {
|
||||
|
||||
/**
|
||||
* Transforms an {@link ApplicationCommandData} object into something that can be used with the API.
|
||||
* @param {ApplicationCommandData} command The command to transform
|
||||
* @param {ApplicationCommandData|APIApplicationCommand} command The command to transform
|
||||
* @returns {APIApplicationCommand}
|
||||
* @private
|
||||
*/
|
||||
@@ -210,7 +212,7 @@ class ApplicationCommandManager extends CachedManager {
|
||||
description: command.description,
|
||||
type: typeof command.type === 'number' ? command.type : ApplicationCommandTypes[command.type],
|
||||
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
|
||||
default_permission: command.defaultPermission,
|
||||
default_permission: command.defaultPermission ?? command.default_permission,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
29
typings/index.d.ts
vendored
29
typings/index.d.ts
vendored
@@ -45,6 +45,7 @@ import {
|
||||
APIUser,
|
||||
GatewayVoiceServerUpdateDispatchData,
|
||||
GatewayVoiceStateUpdateDispatchData,
|
||||
RESTPostAPIApplicationCommandsJSONBody,
|
||||
Snowflake,
|
||||
} from 'discord-api-types/v9';
|
||||
import { EventEmitter } from 'events';
|
||||
@@ -237,6 +238,8 @@ export class ApplicationCommand<PermissionsFetchType = {}> extends Base {
|
||||
enforceOptionorder?: boolean,
|
||||
): boolean;
|
||||
private static transformOption(option: ApplicationCommandOptionData, received?: boolean): unknown;
|
||||
private static transformCommand(command: ApplicationCommandData): RESTPostAPIApplicationCommandsJSONBody;
|
||||
private static isAPICommandData(command: object): command is RESTPostAPIApplicationCommandsJSONBody;
|
||||
}
|
||||
|
||||
export type ApplicationResolvable = Application | Activity | Snowflake;
|
||||
@@ -2400,6 +2403,8 @@ export abstract class CachedManager<K, Holds, R> extends DataManager<K, Holds, R
|
||||
private _add(data: unknown, cache?: boolean, { id, extras }?: { id: K; extras: unknown[] }): Holds;
|
||||
}
|
||||
|
||||
export type ApplicationCommandDataResolvable = ApplicationCommandData | RESTPostAPIApplicationCommandsJSONBody;
|
||||
|
||||
export class ApplicationCommandManager<
|
||||
ApplicationCommandScope = ApplicationCommand<{ guild: GuildResolvable }>,
|
||||
PermissionsOptionsExtras = { guild: GuildResolvable },
|
||||
@@ -2414,13 +2419,16 @@ export class ApplicationCommandManager<
|
||||
null
|
||||
>;
|
||||
private commandPath({ id, guildId }: { id?: Snowflake; guildId?: Snowflake }): unknown;
|
||||
public create(command: ApplicationCommandData): Promise<ApplicationCommandScope>;
|
||||
public create(command: ApplicationCommandData, guildId: Snowflake): Promise<ApplicationCommand>;
|
||||
public create(command: ApplicationCommandDataResolvable): Promise<ApplicationCommandScope>;
|
||||
public create(command: ApplicationCommandDataResolvable, guildId: Snowflake): Promise<ApplicationCommand>;
|
||||
public delete(command: ApplicationCommandResolvable, guildId?: Snowflake): Promise<ApplicationCommandScope | null>;
|
||||
public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise<ApplicationCommandScope>;
|
||||
public edit(
|
||||
command: ApplicationCommandResolvable,
|
||||
data: ApplicationCommandData,
|
||||
data: ApplicationCommandDataResolvable,
|
||||
): Promise<ApplicationCommandScope>;
|
||||
public edit(
|
||||
command: ApplicationCommandResolvable,
|
||||
data: ApplicationCommandDataResolvable,
|
||||
guildId: Snowflake,
|
||||
): Promise<ApplicationCommand>;
|
||||
public fetch(
|
||||
@@ -2432,9 +2440,9 @@ export class ApplicationCommandManager<
|
||||
id?: Snowflake,
|
||||
options?: FetchApplicationCommandOptions,
|
||||
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||
public set(commands: ApplicationCommandData[]): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||
public set(commands: ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||
public set(
|
||||
commands: ApplicationCommandData[],
|
||||
commands: ApplicationCommandDataResolvable[],
|
||||
guildId: Snowflake,
|
||||
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||
private static transformCommand(
|
||||
@@ -2502,12 +2510,15 @@ export class ChannelManager extends CachedManager<Snowflake, Channel, ChannelRes
|
||||
export class GuildApplicationCommandManager extends ApplicationCommandManager<ApplicationCommand, {}, Guild> {
|
||||
public constructor(guild: Guild, iterable?: Iterable<RawApplicationCommandData>);
|
||||
public guild: Guild;
|
||||
public create(command: ApplicationCommandData): Promise<ApplicationCommand>;
|
||||
public create(command: ApplicationCommandDataResolvable): Promise<ApplicationCommand>;
|
||||
public delete(command: ApplicationCommandResolvable): Promise<ApplicationCommand | null>;
|
||||
public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise<ApplicationCommand>;
|
||||
public edit(
|
||||
command: ApplicationCommandResolvable,
|
||||
data: ApplicationCommandDataResolvable,
|
||||
): Promise<ApplicationCommand>;
|
||||
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<ApplicationCommand>;
|
||||
public fetch(id?: undefined, options?: BaseFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||
public set(commands: ApplicationCommandData[]): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||
public set(commands: ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||
}
|
||||
|
||||
export class GuildChannelManager extends CachedManager<
|
||||
|
||||
Reference in New Issue
Block a user