Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for API command types in ApplicationCommandManager #6621

Merged
merged 3 commits into from Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/managers/ApplicationCommandManager.js
Expand Up @@ -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>}
Expand All @@ -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>>}
Expand Down Expand Up @@ -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>}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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
*/
Expand All @@ -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,
};
}
}
Expand Down
29 changes: 20 additions & 9 deletions typings/index.d.ts
Expand Up @@ -44,6 +44,7 @@ import {
APIUser,
GatewayVoiceServerUpdateDispatchData,
GatewayVoiceStateUpdateDispatchData,
RESTPostAPIApplicationCommandsJSONBody,
Snowflake,
} from 'discord-api-types/v9';
import { EventEmitter } from 'events';
Expand Down Expand Up @@ -235,6 +236,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;
Expand Down Expand Up @@ -2383,6 +2386,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 },
Expand All @@ -2397,13 +2402,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(
Expand All @@ -2415,9 +2423,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(
Expand Down Expand Up @@ -2485,12 +2493,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<
Expand Down