Skip to content

Commit

Permalink
feat: Add support for API command types in ApplicationCommandManager (
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Sep 23, 2021
1 parent 66a90d3 commit ecd637f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
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 @@ -45,6 +45,7 @@ import {
APIUser,
GatewayVoiceServerUpdateDispatchData,
GatewayVoiceStateUpdateDispatchData,
RESTPostAPIApplicationCommandsJSONBody,
Snowflake,
} from 'discord-api-types/v9';
import { EventEmitter } from 'events';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 },
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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<
Expand Down

0 comments on commit ecd637f

Please sign in to comment.