mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 04:53:30 +01:00
fix(ApplicationCommands): allow managing commands for uncached guilds (#5729)
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
@@ -110,7 +110,8 @@ const Messages = {
|
|||||||
MEMBER_FETCH_NONCE_LENGTH: 'Nonce length must not exceed 32 characters.',
|
MEMBER_FETCH_NONCE_LENGTH: 'Nonce length must not exceed 32 characters.',
|
||||||
|
|
||||||
GLOBAL_COMMAND_PERMISSIONS:
|
GLOBAL_COMMAND_PERMISSIONS:
|
||||||
"Permissions for global commands may only be fetched or modified from a guild's application command manager.",
|
'Permissions for global commands may only be fetched or modified by providing a guildID' +
|
||||||
|
"or from a guild's application command manager.",
|
||||||
|
|
||||||
INTERACTION_ALREADY_REPLIED: 'This interaction has already been deferred or replied to.',
|
INTERACTION_ALREADY_REPLIED: 'This interaction has already been deferred or replied to.',
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const BaseManager = require('./BaseManager');
|
const BaseManager = require('./BaseManager');
|
||||||
const { TypeError } = require('../errors');
|
const { Error, TypeError } = require('../errors');
|
||||||
const ApplicationCommand = require('../structures/ApplicationCommand');
|
const ApplicationCommand = require('../structures/ApplicationCommand');
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
|
const { ApplicationCommandPermissionTypes } = require('../util/Constants');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages API methods for application commands and stores their cache.
|
* Manages API methods for application commands and stores their cache.
|
||||||
@@ -26,14 +27,16 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The APIRouter path to the commands
|
* The APIRouter path to the commands
|
||||||
* @type {Object}
|
* @param {Snowflake} [options.id] ID of the application command
|
||||||
* @readonly
|
* @param {Snowflake} [options.guildID] ID of the guild to use in the path,
|
||||||
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
|
* @returns {Object}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
get commandPath() {
|
commandPath({ id, guildID } = {}) {
|
||||||
let path = this.client.api.applications(this.client.application.id);
|
let path = this.client.api.applications(this.client.application.id);
|
||||||
if (this.guild) path = path.guilds(this.guild.id);
|
if (this.guild || guildID) path = path.guilds(this.guild?.id ?? guildID);
|
||||||
return path.commands;
|
return id ? path.commands(id) : path.commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,11 +46,23 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
|
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options used to fetch data from discord
|
||||||
|
* @typedef {Object} BaseFetchOptions
|
||||||
|
* @property {boolean} [cache=true] Whether to cache the fetched data if it wasn't already
|
||||||
|
* @property {boolean} [force=false] Whether to skip the cache check and request the API
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options used to fetch Application Commands from discord
|
||||||
|
* @typedef {BaseFetchOptions} FetchApplicationCommandOptions
|
||||||
|
* @property {Snowflake} [guildID] ID of the guild to fetch commands for, for when the guild is not cached
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtains one or multiple application commands from Discord, or the cache if it's already available.
|
* Obtains one or multiple application commands from Discord, or the cache if it's already available.
|
||||||
* @param {Snowflake} [id] ID of the application command
|
* @param {Snowflake} [id] ID of the application command
|
||||||
* @param {boolean} [cache=true] Whether to cache the new application commands if they weren't already
|
* @param {FetchApplicationCommandOptions} [options] Additional options for this fetch
|
||||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
|
||||||
* @returns {Promise<ApplicationCommand|Collection<Snowflake, ApplicationCommand>>}
|
* @returns {Promise<ApplicationCommand|Collection<Snowflake, ApplicationCommand>>}
|
||||||
* @example
|
* @example
|
||||||
* // Fetch a single command
|
* // Fetch a single command
|
||||||
@@ -60,23 +75,29 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
* .then(commands => console.log(`Fetched ${commands.size} commands`))
|
* .then(commands => console.log(`Fetched ${commands.size} commands`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async fetch(id, cache = true, force = false) {
|
async fetch(id, { guildID, cache = true, force = false } = {}) {
|
||||||
|
if (typeof id === 'object') {
|
||||||
|
({ guildID, cache = true, force = false } = id);
|
||||||
|
id = undefined;
|
||||||
|
}
|
||||||
if (id) {
|
if (id) {
|
||||||
if (!force) {
|
if (!force) {
|
||||||
const existing = this.cache.get(id);
|
const existing = this.cache.get(id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
}
|
}
|
||||||
const command = await this.commandPath(id).get();
|
const command = await this.commandPath({ id, guildID }).get();
|
||||||
return this.add(command, cache);
|
return this.add(command, cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await this.commandPath.get();
|
const data = await this.commandPath({ guildID }).get();
|
||||||
return data.reduce((coll, command) => coll.set(command.id, this.add(command, cache)), new Collection());
|
return data.reduce((coll, command) => coll.set(command.id, this.add(command, cache)), new Collection());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an application command.
|
* Creates an application command.
|
||||||
* @param {ApplicationCommandData} command The command
|
* @param {ApplicationCommandData} command The command
|
||||||
|
* @param {Snowflake} [guildID] ID of the guild to create this command in,
|
||||||
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
* @returns {Promise<ApplicationCommand>}
|
* @returns {Promise<ApplicationCommand>}
|
||||||
* @example
|
* @example
|
||||||
* // Create a new command
|
* // Create a new command
|
||||||
@@ -87,8 +108,8 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async create(command) {
|
async create(command, guildID) {
|
||||||
const data = await this.commandPath.post({
|
const data = await this.commandPath({ guildID }).post({
|
||||||
data: this.constructor.transformCommand(command),
|
data: this.constructor.transformCommand(command),
|
||||||
});
|
});
|
||||||
return this.add(data);
|
return this.add(data);
|
||||||
@@ -97,6 +118,8 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
/**
|
/**
|
||||||
* Sets all the commands for this application or guild.
|
* Sets all the commands for this application or guild.
|
||||||
* @param {ApplicationCommandData[]} commands The commands
|
* @param {ApplicationCommandData[]} commands The commands
|
||||||
|
* @param {Snowflake} [guildID] ID of the guild to create the commands in,
|
||||||
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
|
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
|
||||||
* @example
|
* @example
|
||||||
* // Set all commands to just this one
|
* // Set all commands to just this one
|
||||||
@@ -114,8 +137,8 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async set(commands) {
|
async set(commands, guildID) {
|
||||||
const data = await this.commandPath.put({
|
const data = await this.commandPath({ guildID }).put({
|
||||||
data: commands.map(c => this.constructor.transformCommand(c)),
|
data: commands.map(c => this.constructor.transformCommand(c)),
|
||||||
});
|
});
|
||||||
return data.reduce((coll, command) => coll.set(command.id, this.add(command)), new Collection());
|
return data.reduce((coll, command) => coll.set(command.id, this.add(command)), new Collection());
|
||||||
@@ -125,6 +148,8 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
* Edits an application command.
|
* Edits an application command.
|
||||||
* @param {ApplicationCommandResolvable} command The command to edit
|
* @param {ApplicationCommandResolvable} command The command to edit
|
||||||
* @param {ApplicationCommandData} data The data to update the command with
|
* @param {ApplicationCommandData} data The data to update the command with
|
||||||
|
* @param {Snowflake} [guildID] ID of the guild where the command registered,
|
||||||
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
* @returns {Promise<ApplicationCommand>}
|
* @returns {Promise<ApplicationCommand>}
|
||||||
* @example
|
* @example
|
||||||
* // Edit an existing command
|
* // Edit an existing command
|
||||||
@@ -134,17 +159,19 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async edit(command, data) {
|
async edit(command, data, guildID) {
|
||||||
const id = this.resolveID(command);
|
const id = this.resolveID(command);
|
||||||
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
|
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
|
||||||
|
|
||||||
const patched = await this.commandPath(id).patch({ data: this.constructor.transformCommand(data) });
|
const patched = await this.commandPath({ id, guildID }).patch({ data: this.constructor.transformCommand(data) });
|
||||||
return this.add(patched);
|
return this.add(patched);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes an application command.
|
* Deletes an application command.
|
||||||
* @param {ApplicationCommandResolvable} command The command to delete
|
* @param {ApplicationCommandResolvable} command The command to delete
|
||||||
|
* @param {Snowflake} [guildID] ID of the guild where the command is registered,
|
||||||
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
* @returns {Promise<?ApplicationCommand>}
|
* @returns {Promise<?ApplicationCommand>}
|
||||||
* @example
|
* @example
|
||||||
* // Delete a command
|
* // Delete a command
|
||||||
@@ -152,11 +179,11 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async delete(command) {
|
async delete(command, guildID) {
|
||||||
const id = this.resolveID(command);
|
const id = this.resolveID(command);
|
||||||
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
|
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
|
||||||
|
|
||||||
await this.commandPath(id).delete();
|
await this.commandPath({ id, guildID }).delete();
|
||||||
|
|
||||||
const cached = this.cache.get(id);
|
const cached = this.cache.get(id);
|
||||||
this.cache.delete(id);
|
this.cache.delete(id);
|
||||||
@@ -177,6 +204,141 @@ class ApplicationCommandManager extends BaseManager {
|
|||||||
default_permission: command.defaultPermission,
|
default_permission: command.defaultPermission,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the permissions for one or multiple commands.
|
||||||
|
* <warn>When calling this on ApplicationCommandManager, guildID is required.
|
||||||
|
* To fetch all permissions for an uncached guild use `fetchPermissions(undefined, '123456789012345678')`</warn>
|
||||||
|
* @param {ApplicationCommandResolvable} [command] The command to get the permissions from
|
||||||
|
* @param {Snowflake} [guildID] ID of the guild to get the permissions for,
|
||||||
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
|
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
|
||||||
|
* @example
|
||||||
|
* // Fetch permissions for one command
|
||||||
|
* guild.commands.fetchPermissions('123456789012345678')
|
||||||
|
* .then(perms => console.log(`Fetched permissions for ${perms.length} users`))
|
||||||
|
* .catch(console.error);
|
||||||
|
* @example
|
||||||
|
* // Fetch permissions for all commands
|
||||||
|
* client.application.commands.fetchPermissions()
|
||||||
|
* .then(perms => console.log(`Fetched permissions for ${perms.size} commands`))
|
||||||
|
* .catch(console.error);
|
||||||
|
*/
|
||||||
|
async fetchPermissions(command, guildID) {
|
||||||
|
if (!this.guild && !guildID) throw new Error('GLOBAL_COMMAND_PERMISSIONS');
|
||||||
|
if (command) {
|
||||||
|
const id = this.resolveID(command);
|
||||||
|
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
|
||||||
|
|
||||||
|
const data = await this.commandPath({ id, guildID }).permissions.get();
|
||||||
|
return data.permissions.map(perm => this.constructor.transformPermissions(perm, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await this.commandPath({ guildID }).permissions.get();
|
||||||
|
return data.reduce(
|
||||||
|
(coll, perm) =>
|
||||||
|
coll.set(
|
||||||
|
perm.id,
|
||||||
|
perm.permissions.map(p => this.constructor.transformPermissions(p, true)),
|
||||||
|
),
|
||||||
|
new Collection(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data used for overwriting the permissions for all application commands in a guild.
|
||||||
|
* @typedef {Object} GuildApplicationCommandPermissionData
|
||||||
|
* @prop {Snowflake} id The ID of the command
|
||||||
|
* @prop {ApplicationCommandPermissionData[]} permissions The permissions for this command
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the permissions for a command.
|
||||||
|
* <warn>When calling this on ApplicationCommandManager, guildID is required.
|
||||||
|
* To set multiple permissions for an uncached guild use `setPermissions(permissions, '123456789012345678')`</warn>
|
||||||
|
* @param {ApplicationCommandResolvable|GuildApplicationCommandPermissionData[]} command The command to edit the
|
||||||
|
* permissions for, or an array of guild application command permissions to set the permissions of all commands
|
||||||
|
* @param {ApplicationCommandPermissionData[]} [permissions] The new permissions for the command
|
||||||
|
* @param {Snowflake} [guildID] ID of the guild to get the permissions for,
|
||||||
|
* ignored when using a {@link GuildApplicationCommandManager}
|
||||||
|
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
|
||||||
|
* @example
|
||||||
|
* // Set the permissions for one command
|
||||||
|
* client.application.commands.setPermissions('123456789012345678', [
|
||||||
|
* {
|
||||||
|
* id: '876543210987654321',
|
||||||
|
* type: 'USER',
|
||||||
|
* permission: false,
|
||||||
|
* },
|
||||||
|
* ])
|
||||||
|
* .then(console.log)
|
||||||
|
* .catch(console.error);
|
||||||
|
* @example
|
||||||
|
* // Set the permissions for all commands
|
||||||
|
* guild.commands.setPermissions([
|
||||||
|
* {
|
||||||
|
* id: '123456789012345678',
|
||||||
|
* permissions: [{
|
||||||
|
* id: '876543210987654321',
|
||||||
|
* type: 'USER',
|
||||||
|
* permission: false,
|
||||||
|
* }],
|
||||||
|
* },
|
||||||
|
* ])
|
||||||
|
* .then(console.log)
|
||||||
|
* .catch(console.error);
|
||||||
|
*/
|
||||||
|
async setPermissions(command, permissions, guildID) {
|
||||||
|
const id = this.resolveID(command);
|
||||||
|
|
||||||
|
if (id) {
|
||||||
|
if (!this.guild && !guildID) throw new Error('GLOBAL_COMMAND_PERMISSIONS');
|
||||||
|
const data = await this.commandPath({ id, guildID }).permissions.put({
|
||||||
|
data: { permissions: permissions.map(perm => this.constructor.transformPermissions(perm)) },
|
||||||
|
});
|
||||||
|
return data.permissions.map(perm => this.constructor.transformPermissions(perm, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof permissions === 'string') {
|
||||||
|
guildID = permissions;
|
||||||
|
permissions = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.guild && !guildID) throw new Error('GLOBAL_COMMAND_PERMISSIONS');
|
||||||
|
|
||||||
|
const data = await this.commandPath({ guildID }).permissions.put({
|
||||||
|
data: command.map(perm => ({
|
||||||
|
id: perm.id,
|
||||||
|
permissions: perm.permissions.map(p => this.constructor.transformPermissions(p)),
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
return data.reduce(
|
||||||
|
(coll, perm) =>
|
||||||
|
coll.set(
|
||||||
|
perm.id,
|
||||||
|
perm.permissions.map(p => this.constructor.transformPermissions(p, true)),
|
||||||
|
),
|
||||||
|
new Collection(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms an {@link ApplicationCommandPermissionData} object into something that can be used with the API.
|
||||||
|
* @param {ApplicationCommandPermissionData} permissions The permissions to transform
|
||||||
|
* @param {boolean} [received] Whether these permissions have been received from Discord
|
||||||
|
* @returns {Object}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
static transformPermissions(permissions, received) {
|
||||||
|
return {
|
||||||
|
id: permissions.id,
|
||||||
|
permission: permissions.permission,
|
||||||
|
type:
|
||||||
|
typeof permissions.type === 'number' && !received
|
||||||
|
? permissions.type
|
||||||
|
: ApplicationCommandPermissionTypes[permissions.type],
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ApplicationCommandManager;
|
module.exports = ApplicationCommandManager;
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const ApplicationCommandManager = require('./ApplicationCommandManager');
|
const ApplicationCommandManager = require('./ApplicationCommandManager');
|
||||||
const { TypeError } = require('../errors');
|
|
||||||
const Collection = require('../util/Collection');
|
|
||||||
const { ApplicationCommandPermissionTypes } = require('../util/Constants');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An extension for guild-specific application commands.
|
* An extension for guild-specific application commands.
|
||||||
@@ -19,124 +16,6 @@ class GuildApplicationCommandManager extends ApplicationCommandManager {
|
|||||||
*/
|
*/
|
||||||
this.guild = guild;
|
this.guild = guild;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches the permissions for one or multiple commands.
|
|
||||||
* @param {ApplicationCommandResolvable} [command] The command to get the permissions from
|
|
||||||
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
|
|
||||||
* @example
|
|
||||||
* // Fetch permissions for one command
|
|
||||||
* guild.commands.fetchPermissions('123456789012345678')
|
|
||||||
* .then(perms => console.log(`Fetched permissions for ${perms.length} users`))
|
|
||||||
* .catch(console.error);
|
|
||||||
* @example
|
|
||||||
* // Fetch permissions for all commands
|
|
||||||
* client.application.commands.fetchPermissions()
|
|
||||||
* .then(perms => console.log(`Fetched permissions for ${perms.size} commands`))
|
|
||||||
* .catch(console.error);
|
|
||||||
*/
|
|
||||||
async fetchPermissions(command) {
|
|
||||||
if (command) {
|
|
||||||
const id = this.resolveID(command);
|
|
||||||
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
|
|
||||||
|
|
||||||
const data = await this.commandPath(id).permissions.get();
|
|
||||||
return data.permissions.map(perm => this.constructor.transformPermissions(perm, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await this.commandPath.permissions.get();
|
|
||||||
return data.reduce(
|
|
||||||
(coll, perm) =>
|
|
||||||
coll.set(
|
|
||||||
perm.id,
|
|
||||||
perm.permissions.map(p => this.constructor.transformPermissions(p, true)),
|
|
||||||
),
|
|
||||||
new Collection(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Data used for overwriting the permissions for all application commands in a guild.
|
|
||||||
* @typedef {Object} GuildApplicationCommandPermissionData
|
|
||||||
* @prop {Snowflake} command The ID of the command
|
|
||||||
* @prop {ApplicationCommandPermissionData[]} permissions The permissions for this command
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the permissions for a command.
|
|
||||||
* @param {ApplicationCommandResolvable|GuildApplicationCommandPermissionData[]} command The command to edit the
|
|
||||||
* permissions for, or an array of guild application command permissions to set the permissions of all commands
|
|
||||||
* @param {ApplicationCommandPermissionData[]} permissions The new permissions for the command
|
|
||||||
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
|
|
||||||
* @example
|
|
||||||
* // Set the permissions for one command
|
|
||||||
* client.application.commands.setPermissions('123456789012345678', [
|
|
||||||
* {
|
|
||||||
* id: '876543210987654321',
|
|
||||||
* type: 'USER',
|
|
||||||
* permission: false,
|
|
||||||
* },
|
|
||||||
* ])
|
|
||||||
* .then(console.log)
|
|
||||||
* .catch(console.error);
|
|
||||||
* @example
|
|
||||||
* // Set the permissions for all commands
|
|
||||||
* guild.commands.setPermissions([
|
|
||||||
* {
|
|
||||||
* id: '123456789012345678',
|
|
||||||
* permissions: [{
|
|
||||||
* id: '876543210987654321',
|
|
||||||
* type: 'USER',
|
|
||||||
* permission: false,
|
|
||||||
* }],
|
|
||||||
* },
|
|
||||||
* ])
|
|
||||||
* .then(console.log)
|
|
||||||
* .catch(console.error);
|
|
||||||
*/
|
|
||||||
async setPermissions(command, permissions) {
|
|
||||||
const id = this.resolveID(command);
|
|
||||||
|
|
||||||
if (id) {
|
|
||||||
const data = await this.commandPath(id).permissions.put({
|
|
||||||
data: { permissions: permissions.map(perm => this.constructor.transformPermissions(perm)) },
|
|
||||||
});
|
|
||||||
return data.permissions.map(perm => this.constructor.transformPermissions(perm, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await this.commandPath.permissions.put({
|
|
||||||
data: command.map(perm => ({
|
|
||||||
id: perm.id,
|
|
||||||
permissions: perm.permissions.map(p => this.constructor.transformPermissions(p)),
|
|
||||||
})),
|
|
||||||
});
|
|
||||||
return data.reduce(
|
|
||||||
(coll, perm) =>
|
|
||||||
coll.set(
|
|
||||||
perm.id,
|
|
||||||
perm.permissions.map(p => this.constructor.transformPermissions(p, true)),
|
|
||||||
),
|
|
||||||
new Collection(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transforms an {@link ApplicationCommandPermissionData} object into something that can be used with the API.
|
|
||||||
* @param {ApplicationCommandPermissionData} permissions The permissions to transform
|
|
||||||
* @param {boolean} [received] Whether these permissions have been received from Discord
|
|
||||||
* @returns {Object}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
static transformPermissions(permissions, received) {
|
|
||||||
return {
|
|
||||||
id: permissions.id,
|
|
||||||
permission: permissions.permission,
|
|
||||||
type:
|
|
||||||
typeof permissions.type === 'number' && !received
|
|
||||||
? permissions.type
|
|
||||||
: ApplicationCommandPermissionTypes[permissions.type],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = GuildApplicationCommandManager;
|
module.exports = GuildApplicationCommandManager;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Base = require('./Base');
|
const Base = require('./Base');
|
||||||
const { Error } = require('../errors');
|
|
||||||
const { ApplicationCommandOptionTypes } = require('../util/Constants');
|
const { ApplicationCommandOptionTypes } = require('../util/Constants');
|
||||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||||
|
|
||||||
@@ -148,7 +147,9 @@ class ApplicationCommand extends Base {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the permissions for this command.
|
* Fetches the permissions for this command.
|
||||||
* <warn>This is only available for guild application commands.</warn>
|
* <warn>You must specify guildID if this command is handled by a {@link ApplicationCommandManager},
|
||||||
|
* including commands fetched for arbitrary guilds from it, otherwise it is ignored.</warn>
|
||||||
|
* @param {Snowflake} [guildID] ID for the guild to fetch permissions for if this is a global command
|
||||||
* @returns {Promise<ApplicationCommandPermissions[]>}
|
* @returns {Promise<ApplicationCommandPermissions[]>}
|
||||||
* @example
|
* @example
|
||||||
* // Fetch permissions for this command
|
* // Fetch permissions for this command
|
||||||
@@ -156,15 +157,16 @@ class ApplicationCommand extends Base {
|
|||||||
* .then(perms => console.log(`Fetched permissions for ${perms.length} users`))
|
* .then(perms => console.log(`Fetched permissions for ${perms.length} users`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
fetchPermissions() {
|
fetchPermissions(guildID) {
|
||||||
if (!this.guild) throw new Error('GLOBAL_COMMAND_PERMISSIONS');
|
return this.manager.fetchPermissions(this, guildID);
|
||||||
return this.manager.fetchPermissions(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the permissions for this command.
|
* Sets the permissions for this command.
|
||||||
* <warn>This is only available for guild application commands.</warn>
|
* <warn>You must specify guildID if this command is handled by a {@link ApplicationCommandManager},
|
||||||
|
* including commands fetched for arbitrary guilds from it, otherwise it is ignored.</warn>
|
||||||
* @param {ApplicationCommandPermissionData[]} permissions The new permissions for the command
|
* @param {ApplicationCommandPermissionData[]} permissions The new permissions for the command
|
||||||
|
* @param {Snowflake} [guildID] ID for the guild to fetch permissions for if this is a global command
|
||||||
* @returns {Promise<ApplicationCommandPermissions[]>}
|
* @returns {Promise<ApplicationCommandPermissions[]>}
|
||||||
* @example
|
* @example
|
||||||
* // Set the permissions for this command
|
* // Set the permissions for this command
|
||||||
@@ -178,9 +180,8 @@ class ApplicationCommand extends Base {
|
|||||||
* .then(console.log)
|
* .then(console.log)
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
setPermissions(permissions) {
|
setPermissions(permissions, guildID) {
|
||||||
if (!this.guild) throw new Error('GLOBAL_COMMAND_PERMISSIONS');
|
return this.manager.setPermissions(this, permissions, guildID);
|
||||||
return this.manager.setPermissions(this, permissions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
67
typings/index.d.ts
vendored
67
typings/index.d.ts
vendored
@@ -189,8 +189,11 @@ declare module 'discord.js' {
|
|||||||
public options: ApplicationCommandOption[];
|
public options: ApplicationCommandOption[];
|
||||||
public delete(): Promise<ApplicationCommand>;
|
public delete(): Promise<ApplicationCommand>;
|
||||||
public edit(data: ApplicationCommandData): Promise<ApplicationCommand>;
|
public edit(data: ApplicationCommandData): Promise<ApplicationCommand>;
|
||||||
public fetchPermissions(): Promise<ApplicationCommandPermissions[]>;
|
public fetchPermissions(guildID?: Snowflake): Promise<ApplicationCommandPermissions[]>;
|
||||||
public setPermissions(permissions: ApplicationCommandPermissionData[]): Promise<ApplicationCommandPermissions[]>;
|
public setPermissions(
|
||||||
|
permissions: ApplicationCommandPermissionData[],
|
||||||
|
guildID?: Snowflake,
|
||||||
|
): Promise<ApplicationCommandPermissions[]>;
|
||||||
private static transformOption(option: ApplicationCommandOptionData, received?: boolean): unknown;
|
private static transformOption(option: ApplicationCommandOptionData, received?: boolean): unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2059,14 +2062,42 @@ declare module 'discord.js' {
|
|||||||
ApplicationCommandResolvable
|
ApplicationCommandResolvable
|
||||||
> {
|
> {
|
||||||
constructor(client: Client, iterable?: Iterable<any>);
|
constructor(client: Client, iterable?: Iterable<any>);
|
||||||
private readonly commandPath: unknown;
|
private commandPath({ id, guildID }: { id?: Snowflake; guildID?: Snowflake }): unknown;
|
||||||
public create(command: ApplicationCommandData): Promise<ApplicationCommand>;
|
public create(command: ApplicationCommandData, guildID?: Snowflake): Promise<ApplicationCommand>;
|
||||||
public delete(command: ApplicationCommandResolvable): Promise<ApplicationCommand | null>;
|
public delete(command: ApplicationCommandResolvable, guildID?: Snowflake): Promise<ApplicationCommand | null>;
|
||||||
public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise<ApplicationCommand>;
|
public edit(
|
||||||
public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise<ApplicationCommand>;
|
command: ApplicationCommandResolvable,
|
||||||
public fetch(id?: Snowflake, cache?: boolean, force?: boolean): Promise<Collection<Snowflake, ApplicationCommand>>;
|
data: ApplicationCommandData,
|
||||||
public set(commands: ApplicationCommandData[]): Promise<Collection<Snowflake, ApplicationCommand>>;
|
guildID?: Snowflake,
|
||||||
|
): Promise<ApplicationCommand>;
|
||||||
|
public fetch(id: Snowflake, options?: FetchApplicationCommandOptions): Promise<ApplicationCommand>;
|
||||||
|
public fetch(
|
||||||
|
id?: Snowflake,
|
||||||
|
options?: FetchApplicationCommandOptions,
|
||||||
|
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||||
|
public fetchPermissions(
|
||||||
|
command: undefined,
|
||||||
|
guildID: Snowflake,
|
||||||
|
): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>;
|
||||||
|
public fetchPermissions(
|
||||||
|
command: ApplicationCommandResolvable,
|
||||||
|
guildID: Snowflake,
|
||||||
|
): Promise<ApplicationCommandPermissions[]>;
|
||||||
|
public set(
|
||||||
|
commands: ApplicationCommandData[],
|
||||||
|
guildID?: Snowflake,
|
||||||
|
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||||
|
public setPermissions(
|
||||||
|
command: ApplicationCommandResolvable,
|
||||||
|
permissions: ApplicationCommandPermissionData[],
|
||||||
|
guildID: Snowflake,
|
||||||
|
): Promise<ApplicationCommandPermissions[]>;
|
||||||
|
public setPermissions(
|
||||||
|
permissions: GuildApplicationCommandPermissionData[],
|
||||||
|
guildID: Snowflake,
|
||||||
|
): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>;
|
||||||
private static transformCommand(command: ApplicationCommandData): unknown;
|
private static transformCommand(command: ApplicationCommandData): unknown;
|
||||||
|
private static transformPermissions(permissions: ApplicationCommandPermissionData, received?: boolean): unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class BaseGuildEmojiManager extends BaseManager<Snowflake, GuildEmoji, EmojiResolvable> {
|
export class BaseGuildEmojiManager extends BaseManager<Snowflake, GuildEmoji, EmojiResolvable> {
|
||||||
@@ -2082,8 +2113,14 @@ declare module 'discord.js' {
|
|||||||
export class GuildApplicationCommandManager extends ApplicationCommandManager {
|
export class GuildApplicationCommandManager extends ApplicationCommandManager {
|
||||||
constructor(guild: Guild, iterable?: Iterable<any>);
|
constructor(guild: Guild, iterable?: Iterable<any>);
|
||||||
public guild: Guild;
|
public guild: Guild;
|
||||||
public fetchPermissions(): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>;
|
public create(command: ApplicationCommandData): Promise<ApplicationCommand>;
|
||||||
|
public delete(command: ApplicationCommandResolvable): Promise<ApplicationCommand | null>;
|
||||||
|
public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise<ApplicationCommand>;
|
||||||
|
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<ApplicationCommand>;
|
||||||
|
public fetch(id?: Snowflake, options?: BaseFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||||
|
public fetchPermissions(command: undefined): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>;
|
||||||
public fetchPermissions(command: ApplicationCommandResolvable): Promise<ApplicationCommandPermissions[]>;
|
public fetchPermissions(command: ApplicationCommandResolvable): Promise<ApplicationCommandPermissions[]>;
|
||||||
|
public set(commands: ApplicationCommandData[]): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||||
public setPermissions(
|
public setPermissions(
|
||||||
command: ApplicationCommandResolvable,
|
command: ApplicationCommandResolvable,
|
||||||
permissions: ApplicationCommandPermissionData[],
|
permissions: ApplicationCommandPermissionData[],
|
||||||
@@ -2091,7 +2128,6 @@ declare module 'discord.js' {
|
|||||||
public setPermissions(
|
public setPermissions(
|
||||||
permissions: GuildApplicationCommandPermissionData[],
|
permissions: GuildApplicationCommandPermissionData[],
|
||||||
): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>;
|
): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>;
|
||||||
private static transformPermissions(permissions: ApplicationCommandPermissionData, received?: boolean): unknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GuildChannelManager extends BaseManager<Snowflake, GuildChannel, GuildChannelResolvable> {
|
export class GuildChannelManager extends BaseManager<Snowflake, GuildChannel, GuildChannelResolvable> {
|
||||||
@@ -2516,6 +2552,11 @@ declare module 'discord.js' {
|
|||||||
|
|
||||||
type Base64String = string;
|
type Base64String = string;
|
||||||
|
|
||||||
|
interface BaseFetchOptions {
|
||||||
|
cache?: boolean;
|
||||||
|
force?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
interface BaseMessageComponentOptions {
|
interface BaseMessageComponentOptions {
|
||||||
type?: MessageComponentType | MessageComponentTypes;
|
type?: MessageComponentType | MessageComponentTypes;
|
||||||
}
|
}
|
||||||
@@ -2807,6 +2848,10 @@ declare module 'discord.js' {
|
|||||||
ButtonInteraction: typeof ButtonInteraction;
|
ButtonInteraction: typeof ButtonInteraction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface FetchApplicationCommandOptions extends BaseFetchOptions {
|
||||||
|
guildID?: Snowflake;
|
||||||
|
}
|
||||||
|
|
||||||
interface FetchBanOptions {
|
interface FetchBanOptions {
|
||||||
user: UserResolvable;
|
user: UserResolvable;
|
||||||
cache?: boolean;
|
cache?: boolean;
|
||||||
|
|||||||
Reference in New Issue
Block a user