mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 04:23:31 +01:00
fix(ApplicationCommandManager): explicitly allow passing builders to methods (#8209)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { isJSONEncodable } = require('@discordjs/builders');
|
||||||
const { Collection } = require('@discordjs/collection');
|
const { Collection } = require('@discordjs/collection');
|
||||||
const { makeURLSearchParams } = require('@discordjs/rest');
|
const { makeURLSearchParams } = require('@discordjs/rest');
|
||||||
const { Routes } = require('discord-api-types/v10');
|
const { Routes } = require('discord-api-types/v10');
|
||||||
@@ -65,6 +66,13 @@ class ApplicationCommandManager extends CachedManager {
|
|||||||
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
|
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* eslint-disable max-len */
|
||||||
|
/**
|
||||||
|
* Data that resolves to the data of an ApplicationCommand
|
||||||
|
* @typedef {ApplicationCommandData|APIApplicationCommand|JSONEncodable<APIApplicationCommand>} ApplicationCommandDataResolvable
|
||||||
|
*/
|
||||||
|
/* eslint-enable max-len */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Options used to fetch data from Discord
|
* Options used to fetch data from Discord
|
||||||
* @typedef {Object} BaseFetchOptions
|
* @typedef {Object} BaseFetchOptions
|
||||||
@@ -119,7 +127,7 @@ class ApplicationCommandManager extends CachedManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an application command.
|
* 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,
|
* @param {Snowflake} [guildId] The guild's id to create this command in,
|
||||||
* ignored when using a {@link GuildApplicationCommandManager}
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
* @returns {Promise<ApplicationCommand>}
|
* @returns {Promise<ApplicationCommand>}
|
||||||
@@ -141,7 +149,7 @@ class ApplicationCommandManager extends CachedManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets all the commands for this application or guild.
|
* 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,
|
* @param {Snowflake} [guildId] The guild's id to create the commands in,
|
||||||
* ignored when using a {@link GuildApplicationCommandManager}
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
|
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
|
||||||
@@ -171,7 +179,7 @@ class ApplicationCommandManager extends CachedManager {
|
|||||||
/**
|
/**
|
||||||
* Edits an application command.
|
* Edits an application command.
|
||||||
* @param {ApplicationCommandResolvable} command The command to edit
|
* @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,
|
* @param {Snowflake} [guildId] The guild's id where the command registered,
|
||||||
* ignored when using a {@link GuildApplicationCommandManager}
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
* @returns {Promise<ApplicationCommand>}
|
* @returns {Promise<ApplicationCommand>}
|
||||||
@@ -218,11 +226,13 @@ class ApplicationCommandManager extends CachedManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms an {@link ApplicationCommandData} object into something that can be used with the API.
|
* 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}
|
* @returns {APIApplicationCommand}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
static transformCommand(command) {
|
static transformCommand(command) {
|
||||||
|
if (isJSONEncodable(command)) return command.toJSON();
|
||||||
|
|
||||||
let default_member_permissions;
|
let default_member_permissions;
|
||||||
|
|
||||||
if ('default_member_permissions' in command) {
|
if ('default_member_permissions' in command) {
|
||||||
|
|||||||
11
packages/discord.js/typings/index.d.ts
vendored
11
packages/discord.js/typings/index.d.ts
vendored
@@ -3174,7 +3174,10 @@ 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;
|
private _add(data: unknown, cache?: boolean, { id, extras }?: { id: K; extras: unknown[] }): Holds;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ApplicationCommandDataResolvable = ApplicationCommandData | RESTPostAPIApplicationCommandsJSONBody;
|
export type ApplicationCommandDataResolvable =
|
||||||
|
| ApplicationCommandData
|
||||||
|
| RESTPostAPIApplicationCommandsJSONBody
|
||||||
|
| JSONEncodable<RESTPostAPIApplicationCommandsJSONBody>;
|
||||||
|
|
||||||
export class ApplicationCommandManager<
|
export class ApplicationCommandManager<
|
||||||
ApplicationCommandScope = ApplicationCommand<{ guild: GuildResolvable }>,
|
ApplicationCommandScope = ApplicationCommand<{ guild: GuildResolvable }>,
|
||||||
@@ -3215,9 +3218,7 @@ export class ApplicationCommandManager<
|
|||||||
commands: ApplicationCommandDataResolvable[],
|
commands: ApplicationCommandDataResolvable[],
|
||||||
guildId: Snowflake,
|
guildId: Snowflake,
|
||||||
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||||
private static transformCommand(
|
private static transformCommand(command: ApplicationCommandDataResolvable): RESTPostAPIApplicationCommandsJSONBody;
|
||||||
command: ApplicationCommandData,
|
|
||||||
): Omit<APIApplicationCommand, 'id' | 'application_id' | 'guild_id'>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ApplicationCommandPermissionsManager<
|
export class ApplicationCommandPermissionsManager<
|
||||||
@@ -3304,7 +3305,7 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager<Ap
|
|||||||
public delete(command: ApplicationCommandResolvable): Promise<ApplicationCommand | null>;
|
public delete(command: ApplicationCommandResolvable): Promise<ApplicationCommand | null>;
|
||||||
public edit(
|
public edit(
|
||||||
command: ApplicationCommandResolvable,
|
command: ApplicationCommandResolvable,
|
||||||
data: ApplicationCommandDataResolvable,
|
data: Partial<ApplicationCommandDataResolvable>,
|
||||||
): Promise<ApplicationCommand>;
|
): Promise<ApplicationCommand>;
|
||||||
public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise<ApplicationCommand>;
|
public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise<ApplicationCommand>;
|
||||||
public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
|
public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||||
|
|||||||
@@ -131,7 +131,13 @@ import {
|
|||||||
CollectedMessageInteraction,
|
CollectedMessageInteraction,
|
||||||
} from '.';
|
} from '.';
|
||||||
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
||||||
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';
|
import {
|
||||||
|
ContextMenuCommandBuilder,
|
||||||
|
SlashCommandBuilder,
|
||||||
|
UnsafeButtonBuilder,
|
||||||
|
UnsafeEmbedBuilder,
|
||||||
|
UnsafeSelectMenuBuilder,
|
||||||
|
} from '@discordjs/builders';
|
||||||
|
|
||||||
// Test type transformation:
|
// Test type transformation:
|
||||||
declare const serialize: <T>(value: T) => Serialized<T>;
|
declare const serialize: <T>(value: T) => Serialized<T>;
|
||||||
@@ -155,6 +161,9 @@ const testUserId = '987654321098765432'; // example id
|
|||||||
const globalCommandId = '123456789012345678'; // example id
|
const globalCommandId = '123456789012345678'; // example id
|
||||||
const guildCommandId = '234567890123456789'; // example id
|
const guildCommandId = '234567890123456789'; // example id
|
||||||
|
|
||||||
|
declare const slashCommandBuilder: SlashCommandBuilder;
|
||||||
|
declare const contextMenuCommandBuilder: ContextMenuCommandBuilder;
|
||||||
|
|
||||||
client.on('ready', async () => {
|
client.on('ready', async () => {
|
||||||
console.log(`Client is logged in as ${client.user!.tag} and ready!`);
|
console.log(`Client is logged in as ${client.user!.tag} and ready!`);
|
||||||
|
|
||||||
@@ -171,6 +180,16 @@ client.on('ready', async () => {
|
|||||||
const guildCommandFromGlobal = await client.application?.commands.fetch(guildCommandId, { guildId: testGuildId });
|
const guildCommandFromGlobal = await client.application?.commands.fetch(guildCommandId, { guildId: testGuildId });
|
||||||
const guildCommandFromGuild = await client.guilds.cache.get(testGuildId)?.commands.fetch(guildCommandId);
|
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 client.application?.commands.edit(globalCommandId, { defaultMemberPermissions: null });
|
||||||
await globalCommand?.edit({ defaultMemberPermissions: null });
|
await globalCommand?.edit({ defaultMemberPermissions: null });
|
||||||
await globalCommand?.setDefaultMemberPermissions(null);
|
await globalCommand?.setDefaultMemberPermissions(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user