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

fix(applicationcommandmanager): explicitly allow passing builders to methods (v13) #8229

Merged
merged 1 commit into from Jul 5, 2022
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
28 changes: 24 additions & 4 deletions src/managers/ApplicationCommandManager.js
@@ -1,5 +1,6 @@
'use strict';

const { isJSONEncodable } = require('@discordjs/builders');
const { Collection } = require('@discordjs/collection');
const ApplicationCommandPermissionsManager = require('./ApplicationCommandPermissionsManager');
const CachedManager = require('./CachedManager');
Expand Down Expand Up @@ -54,6 +55,13 @@ class ApplicationCommandManager extends CachedManager {
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
*/

/* eslint-disable max-len */
/**
* Data that resolves to the data of an ApplicationCommand
* @typedef {ApplicationCommandData|APIApplicationCommand|SlashCommandBuilder|ContextMenuCommandBuilder} ApplicationCommandDataResolvable
*/
/* eslint-enable max-len */

/**
* Options used to fetch data from Discord
* @typedef {Object} BaseFetchOptions
Expand Down Expand Up @@ -108,7 +116,7 @@ class ApplicationCommandManager extends CachedManager {

/**
* Creates an application command.
* @param {ApplicationCommandData|APIApplicationCommand} command The command
* @param {ApplicationCommandDataResolvable} 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 @@ -130,7 +138,7 @@ class ApplicationCommandManager extends CachedManager {

/**
* Sets all the commands for this application or guild.
* @param {ApplicationCommandData[]|APIApplicationCommand[]} commands The commands
* @param {ApplicationCommandDataResolvable[]} 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 @@ -160,7 +168,7 @@ class ApplicationCommandManager extends CachedManager {
/**
* Edits an application command.
* @param {ApplicationCommandResolvable} command The command to edit
* @param {Partial<ApplicationCommandData|APIApplicationCommand>} data The data to update the command with
* @param {Partial<ApplicationCommandDataResolvable>} 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 Down Expand Up @@ -207,11 +215,13 @@ class ApplicationCommandManager extends CachedManager {

/**
* Transforms an {@link ApplicationCommandData} object into something that can be used with the API.
* @param {ApplicationCommandData|APIApplicationCommand} command The command to transform
* @param {ApplicationCommandDataResolvable} command The command to transform
* @returns {APIApplicationCommand}
* @private
*/
static transformCommand(command) {
if (isJSONEncodable(command)) return command.toJSON();

let default_member_permissions;

if ('default_member_permissions' in command) {
Expand Down Expand Up @@ -241,3 +251,13 @@ class ApplicationCommandManager extends CachedManager {
}

module.exports = ApplicationCommandManager;

/**
* @external SlashCommandBuilder
* @see {@link https://discord.js.org/#/docs/builders/main/class/SlashCommandBuilder}
*/

/**
* @external ContextMenuCommandBuilder
* @see {@link https://discord.js.org/#/docs/builders/main/class/ContextMenuCommandBuilder}
*/
15 changes: 10 additions & 5 deletions typings/index.d.ts
Expand Up @@ -3,13 +3,16 @@ import {
bold,
channelMention,
codeBlock,
ContextMenuCommandBuilder,
formatEmoji,
hideLinkEmbed,
hyperlink,
inlineCode,
italic,
JSONEncodable,
quote,
roleMention,
SlashCommandBuilder,
spoiler,
strikethrough,
time,
Expand Down Expand Up @@ -3065,7 +3068,11 @@ 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 type ApplicationCommandDataResolvable =
| ApplicationCommandData
| RESTPostAPIApplicationCommandsJSONBody
| SlashCommandBuilder
| ContextMenuCommandBuilder;

export class ApplicationCommandManager<
ApplicationCommandScope = ApplicationCommand<{ guild: GuildResolvable }>,
Expand Down Expand Up @@ -3107,9 +3114,7 @@ export class ApplicationCommandManager<
commands: ApplicationCommandDataResolvable[],
guildId: Snowflake,
): Promise<Collection<Snowflake, ApplicationCommand>>;
private static transformCommand(
command: ApplicationCommandData,
): Omit<APIApplicationCommand, 'id' | 'application_id' | 'guild_id'>;
private static transformCommand(command: ApplicationCommandDataResolvable): RESTPostAPIApplicationCommandsJSONBody;
}

export class ApplicationCommandPermissionsManager<
Expand Down Expand Up @@ -3178,7 +3183,7 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager<Ap
public delete(command: ApplicationCommandResolvable): Promise<ApplicationCommand | null>;
public edit(
command: ApplicationCommandResolvable,
data: ApplicationCommandDataResolvable,
data: Partial<ApplicationCommandDataResolvable>,
): Promise<ApplicationCommand>;
public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise<ApplicationCommand>;
public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
Expand Down
14 changes: 14 additions & 0 deletions typings/index.test-d.ts
Expand Up @@ -100,6 +100,7 @@ import {
} from '.';
import type { ApplicationCommandOptionTypes } from './enums';
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';

// Test type transformation:
declare const serialize: <T>(value: T) => Serialized<T>;
Expand Down Expand Up @@ -128,6 +129,9 @@ const testUserId = '987654321098765432'; // example id
const globalCommandId = '123456789012345678'; // example id
const guildCommandId = '234567890123456789'; // example id

declare const slashCommandBuilder: SlashCommandBuilder;
declare const contextMenuCommandBuilder: ContextMenuCommandBuilder;

client.on('ready', async () => {
console.log(`Client is logged in as ${client.user!.tag} and ready!`);

Expand All @@ -144,6 +148,16 @@ client.on('ready', async () => {
const guildCommandFromGlobal = await client.application?.commands.fetch(guildCommandId, { guildId: testGuildId });
const guildCommandFromGuild = await client.guilds.cache.get(testGuildId)?.commands.fetch(guildCommandId);

await client.application?.commands.create(slashCommandBuilder);
await client.application?.commands.create(contextMenuCommandBuilder);
await guild.commands.create(slashCommandBuilder);
await guild.commands.create(contextMenuCommandBuilder);

await client.application?.commands.edit(globalCommandId, slashCommandBuilder);
await client.application?.commands.edit(globalCommandId, contextMenuCommandBuilder);
await guild.commands.edit(guildCommandId, slashCommandBuilder);
await guild.commands.edit(guildCommandId, contextMenuCommandBuilder);

await client.application?.commands.edit(globalCommandId, { defaultMemberPermissions: null });
await globalCommand?.edit({ defaultMemberPermissions: null });
await globalCommand?.setDefaultMemberPermissions(null);
Expand Down