Skip to content

Commit f457cdd

Browse files
authored
fix(applicationcommandmanager): explicitly allow passing builders to methods (v13) (#8229)
1 parent f704b26 commit f457cdd

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/managers/ApplicationCommandManager.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const { isJSONEncodable } = require('@discordjs/builders');
34
const { Collection } = require('@discordjs/collection');
45
const ApplicationCommandPermissionsManager = require('./ApplicationCommandPermissionsManager');
56
const CachedManager = require('./CachedManager');
@@ -54,6 +55,13 @@ class ApplicationCommandManager extends CachedManager {
5455
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
5556
*/
5657

58+
/* eslint-disable max-len */
59+
/**
60+
* Data that resolves to the data of an ApplicationCommand
61+
* @typedef {ApplicationCommandData|APIApplicationCommand|SlashCommandBuilder|ContextMenuCommandBuilder} ApplicationCommandDataResolvable
62+
*/
63+
/* eslint-enable max-len */
64+
5765
/**
5866
* Options used to fetch data from Discord
5967
* @typedef {Object} BaseFetchOptions
@@ -108,7 +116,7 @@ class ApplicationCommandManager extends CachedManager {
108116

109117
/**
110118
* Creates an application command.
111-
* @param {ApplicationCommandData|APIApplicationCommand} command The command
119+
* @param {ApplicationCommandDataResolvable} command The command
112120
* @param {Snowflake} [guildId] The guild's id to create this command in,
113121
* ignored when using a {@link GuildApplicationCommandManager}
114122
* @returns {Promise<ApplicationCommand>}
@@ -130,7 +138,7 @@ class ApplicationCommandManager extends CachedManager {
130138

131139
/**
132140
* Sets all the commands for this application or guild.
133-
* @param {ApplicationCommandData[]|APIApplicationCommand[]} commands The commands
141+
* @param {ApplicationCommandDataResolvable[]} commands The commands
134142
* @param {Snowflake} [guildId] The guild's id to create the commands in,
135143
* ignored when using a {@link GuildApplicationCommandManager}
136144
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
@@ -160,7 +168,7 @@ class ApplicationCommandManager extends CachedManager {
160168
/**
161169
* Edits an application command.
162170
* @param {ApplicationCommandResolvable} command The command to edit
163-
* @param {Partial<ApplicationCommandData|APIApplicationCommand>} data The data to update the command with
171+
* @param {Partial<ApplicationCommandDataResolvable>} data The data to update the command with
164172
* @param {Snowflake} [guildId] The guild's id where the command registered,
165173
* ignored when using a {@link GuildApplicationCommandManager}
166174
* @returns {Promise<ApplicationCommand>}
@@ -207,11 +215,13 @@ class ApplicationCommandManager extends CachedManager {
207215

208216
/**
209217
* Transforms an {@link ApplicationCommandData} object into something that can be used with the API.
210-
* @param {ApplicationCommandData|APIApplicationCommand} command The command to transform
218+
* @param {ApplicationCommandDataResolvable} command The command to transform
211219
* @returns {APIApplicationCommand}
212220
* @private
213221
*/
214222
static transformCommand(command) {
223+
if (isJSONEncodable(command)) return command.toJSON();
224+
215225
let default_member_permissions;
216226

217227
if ('default_member_permissions' in command) {
@@ -241,3 +251,13 @@ class ApplicationCommandManager extends CachedManager {
241251
}
242252

243253
module.exports = ApplicationCommandManager;
254+
255+
/**
256+
* @external SlashCommandBuilder
257+
* @see {@link https://discord.js.org/#/docs/builders/main/class/SlashCommandBuilder}
258+
*/
259+
260+
/**
261+
* @external ContextMenuCommandBuilder
262+
* @see {@link https://discord.js.org/#/docs/builders/main/class/ContextMenuCommandBuilder}
263+
*/

typings/index.d.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import {
33
bold,
44
channelMention,
55
codeBlock,
6+
ContextMenuCommandBuilder,
67
formatEmoji,
78
hideLinkEmbed,
89
hyperlink,
910
inlineCode,
1011
italic,
12+
JSONEncodable,
1113
quote,
1214
roleMention,
15+
SlashCommandBuilder,
1316
spoiler,
1417
strikethrough,
1518
time,
@@ -3065,7 +3068,11 @@ export abstract class CachedManager<K, Holds, R> extends DataManager<K, Holds, R
30653068
private _add(data: unknown, cache?: boolean, { id, extras }?: { id: K; extras: unknown[] }): Holds;
30663069
}
30673070

3068-
export type ApplicationCommandDataResolvable = ApplicationCommandData | RESTPostAPIApplicationCommandsJSONBody;
3071+
export type ApplicationCommandDataResolvable =
3072+
| ApplicationCommandData
3073+
| RESTPostAPIApplicationCommandsJSONBody
3074+
| SlashCommandBuilder
3075+
| ContextMenuCommandBuilder;
30693076

30703077
export class ApplicationCommandManager<
30713078
ApplicationCommandScope = ApplicationCommand<{ guild: GuildResolvable }>,
@@ -3107,9 +3114,7 @@ export class ApplicationCommandManager<
31073114
commands: ApplicationCommandDataResolvable[],
31083115
guildId: Snowflake,
31093116
): Promise<Collection<Snowflake, ApplicationCommand>>;
3110-
private static transformCommand(
3111-
command: ApplicationCommandData,
3112-
): Omit<APIApplicationCommand, 'id' | 'application_id' | 'guild_id'>;
3117+
private static transformCommand(command: ApplicationCommandDataResolvable): RESTPostAPIApplicationCommandsJSONBody;
31133118
}
31143119

31153120
export class ApplicationCommandPermissionsManager<
@@ -3178,7 +3183,7 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager<Ap
31783183
public delete(command: ApplicationCommandResolvable): Promise<ApplicationCommand | null>;
31793184
public edit(
31803185
command: ApplicationCommandResolvable,
3181-
data: ApplicationCommandDataResolvable,
3186+
data: Partial<ApplicationCommandDataResolvable>,
31823187
): Promise<ApplicationCommand>;
31833188
public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise<ApplicationCommand>;
31843189
public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;

typings/index.test-d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import {
100100
} from '.';
101101
import type { ApplicationCommandOptionTypes } from './enums';
102102
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
103+
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
103104

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

132+
declare const slashCommandBuilder: SlashCommandBuilder;
133+
declare const contextMenuCommandBuilder: ContextMenuCommandBuilder;
134+
131135
client.on('ready', async () => {
132136
console.log(`Client is logged in as ${client.user!.tag} and ready!`);
133137

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

151+
await client.application?.commands.create(slashCommandBuilder);
152+
await client.application?.commands.create(contextMenuCommandBuilder);
153+
await guild.commands.create(slashCommandBuilder);
154+
await guild.commands.create(contextMenuCommandBuilder);
155+
156+
await client.application?.commands.edit(globalCommandId, slashCommandBuilder);
157+
await client.application?.commands.edit(globalCommandId, contextMenuCommandBuilder);
158+
await guild.commands.edit(guildCommandId, slashCommandBuilder);
159+
await guild.commands.edit(guildCommandId, contextMenuCommandBuilder);
160+
147161
await client.application?.commands.edit(globalCommandId, { defaultMemberPermissions: null });
148162
await globalCommand?.edit({ defaultMemberPermissions: null });
149163
await globalCommand?.setDefaultMemberPermissions(null);

0 commit comments

Comments
 (0)