This commit is contained in:
Elysia
2024-01-10 18:30:39 +07:00
parent b3cf4b7710
commit 59f254966c
14 changed files with 637 additions and 937 deletions

View File

@@ -1,5 +1,6 @@
'use strict';
const { isJSONEncodable } = require('@discordjs/builders');
const { Collection } = require('@discordjs/collection');
const ApplicationCommandPermissionsManager = require('./ApplicationCommandPermissionsManager');
const CachedManager = require('./CachedManager');
@@ -13,15 +14,14 @@ const Permissions = require('../util/Permissions');
* @extends {CachedManager}
*/
class ApplicationCommandManager extends CachedManager {
constructor(client, iterable, user) {
constructor(client, iterable) {
super(client, ApplicationCommand, iterable);
/**
* The manager for permissions of arbitrary commands on arbitrary guilds
* @type {ApplicationCommandPermissionsManager}
*/
this.permissions = new ApplicationCommandPermissionsManager(this, user);
this.user = user;
this.permissions = new ApplicationCommandPermissionsManager(this);
}
/**
@@ -43,7 +43,7 @@ class ApplicationCommandManager extends CachedManager {
* @private
*/
commandPath({ id, guildId } = {}) {
let path = this.client.api.applications(this.user.id);
let path = this.client.api.applications(this.client.application.id);
if (this.guild ?? guildId) path = path.guilds(this.guild?.id ?? guildId);
return id ? path.commands(id) : path.commands;
}
@@ -58,7 +58,7 @@ class ApplicationCommandManager extends CachedManager {
/* eslint-disable max-len */
/**
* Data that resolves to the data of an ApplicationCommand
* @typedef {ApplicationCommandDataResolvable|SlashCommandBuilder|ContextMenuCommandBuilder} ApplicationCommandDataResolvable
* @typedef {ApplicationCommandData|APIApplicationCommand|SlashCommandBuilder|ContextMenuCommandBuilder} ApplicationCommandDataResolvable
*/
/* eslint-enable max-len */
@@ -94,7 +94,6 @@ class ApplicationCommandManager extends CachedManager {
* .catch(console.error);
*/
async fetch(id, { guildId, cache = true, force = false, locale, withLocalizations } = {}) {
// Change from user.createDM to opcode (risky action)
if (typeof id === 'object') {
({ guildId, cache = true, locale, withLocalizations } = id);
} else if (id) {
@@ -102,11 +101,10 @@ class ApplicationCommandManager extends CachedManager {
const existing = this.cache.get(id);
if (existing) return existing;
}
await this.user.createDM().catch(() => {});
const command = await this.commandPath({ id, guildId }).get();
return this._add(command, cache);
}
await this.user.createDM().catch(() => {});
const data = await this.commandPath({ guildId }).get({
headers: {
'X-Discord-Locale': locale,
@@ -132,7 +130,6 @@ class ApplicationCommandManager extends CachedManager {
* .catch(console.error);
*/
async create(command, guildId) {
if (!this.client.user.bot) throw new Error('INVALID_USER_METHOD');
const data = await this.commandPath({ guildId }).post({
data: this.constructor.transformCommand(command),
});
@@ -162,7 +159,6 @@ class ApplicationCommandManager extends CachedManager {
* .catch(console.error);
*/
async set(commands, guildId) {
if (!this.client.user.bot) throw new Error('INVALID_USER_METHOD');
const data = await this.commandPath({ guildId }).put({
data: commands.map(c => this.constructor.transformCommand(c)),
});
@@ -185,7 +181,6 @@ class ApplicationCommandManager extends CachedManager {
* .catch(console.error);
*/
async edit(command, data, guildId) {
if (!this.client.user.bot) throw new Error('INVALID_USER_METHOD');
const id = this.resolveId(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
@@ -208,7 +203,6 @@ class ApplicationCommandManager extends CachedManager {
* .catch(console.error);
*/
async delete(command, guildId) {
if (!this.client.user.bot) throw new Error('INVALID_USER_METHOD');
const id = this.resolveId(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
@@ -226,6 +220,8 @@ class ApplicationCommandManager extends CachedManager {
* @private
*/
static transformCommand(command) {
if (isJSONEncodable(command)) return command.toJSON();
let default_member_permissions;
if ('default_member_permissions' in command) {
@@ -240,6 +236,7 @@ class ApplicationCommandManager extends CachedManager {
? new Permissions(command.defaultMemberPermissions).bitfield.toString()
: command.defaultMemberPermissions;
}
return {
name: command.name,
name_localizations: command.nameLocalizations ?? command.name_localizations,

View File

@@ -10,7 +10,7 @@ const { ApplicationCommandPermissionTypes, APIErrors } = require('../util/Consta
* @extends {BaseManager}
*/
class ApplicationCommandPermissionsManager extends BaseManager {
constructor(manager, user) {
constructor(manager) {
super(manager.client);
/**
@@ -37,8 +37,6 @@ class ApplicationCommandPermissionsManager extends BaseManager {
* @type {?Snowflake}
*/
this.commandId = manager.id ?? null;
this.user = user;
}
/**
@@ -49,10 +47,7 @@ class ApplicationCommandPermissionsManager extends BaseManager {
* @private
*/
permissionsPath(guildId, commandId) {
return this.client.api
.applications(typeof this.user === 'string' ? this.user : this.user.id)
.guilds(guildId)
.commands(commandId).permissions;
return this.client.api.applications(this.client.application.id).guilds(guildId).commands(commandId).permissions;
}
/**
@@ -164,7 +159,6 @@ class ApplicationCommandPermissionsManager extends BaseManager {
* .catch(console.error);
*/
async set({ guild, command, permissions, fullPermissions } = {}) {
if (!this.manager.client.user.bot) throw new Error('INVALID_USER_METHOD');
const { guildId, commandId } = this._validateOptions(guild, command);
if (commandId) {
@@ -226,7 +220,6 @@ class ApplicationCommandPermissionsManager extends BaseManager {
* .catch(console.error);
*/
async add({ guild, command, permissions }) {
if (!this.manager.client.user.bot) throw new Error('INVALID_USER_METHOD');
const { guildId, commandId } = this._validateOptions(guild, command);
if (!commandId) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
if (!Array.isArray(permissions)) {
@@ -278,13 +271,12 @@ class ApplicationCommandPermissionsManager extends BaseManager {
* .catch(console.error);
*/
async remove({ guild, command, users, roles }) {
if (!this.manager.client.user.bot) throw new Error('INVALID_USER_METHOD');
const { guildId, commandId } = this._validateOptions(guild, command);
if (!commandId) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
if (!users && !roles) throw new TypeError('INVALID_TYPE', 'users OR roles', 'Array or Resolvable', true);
const resolvedIds = [];
let resolvedIds = [];
if (Array.isArray(users)) {
users.forEach(user => {
const userId = this.client.users.resolveId(user);

View File

@@ -3,15 +3,15 @@
const { Collection } = require('@discordjs/collection');
const CachedManager = require('./CachedManager');
const { Error } = require('../errors');
const { lazy } = require('../util/Util');
const User = lazy(() => require('../structures/User'));
const User = require('../structures/User');
/**
* Manages API methods for users who reacted to a reaction and stores their cache.
* @extends {CachedManager}
*/
class ReactionUserManager extends CachedManager {
constructor(reaction, iterable) {
super(reaction.client, User(), iterable);
super(reaction.client, User, iterable);
/**
* The reaction that this manager belongs to
@@ -22,7 +22,7 @@ class ReactionUserManager extends CachedManager {
/**
* The cache of this manager
* @type {Collection<Snowflake, Discord.User>}
* @type {Collection<Snowflake, User>}
* @name ReactionUserManager#cache
*/
@@ -36,7 +36,7 @@ class ReactionUserManager extends CachedManager {
/**
* Fetches all the users that gave this reaction. Resolves with a collection of users, mapped by their ids.
* @param {FetchReactionUsersOptions} [options] Options for fetching the users
* @returns {Promise<Collection<Snowflake, Discord.User>>}
* @returns {Promise<Collection<Snowflake, User>>}
*/
async fetch({ limit = 100, after } = {}) {
const message = this.reaction.message;