-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ApplicationCommandManager.js
201 lines (177 loc) · 6.71 KB
/
ApplicationCommandManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
const BaseManager = require('./BaseManager');
const { TypeError } = require('../errors');
const ApplicationCommand = require('../structures/ApplicationCommand');
const Collection = require('../util/Collection');
const { ApplicationCommandPermissionTypes } = require('../util/Constants');
/**
* Manages API methods for application commands and stores their cache.
* @extends {BaseManager}
*/
class ApplicationCommandManager extends BaseManager {
constructor(client, iterable) {
super(client, iterable, ApplicationCommand);
}
/**
* The cache of this manager
* @type {Collection<Snowflake, ApplicationCommand>}
* @name ApplicationCommandManager#cache
*/
add(data, cache) {
return super.add(data, cache, { extras: [this.guild] });
}
/**
* The APIRouter path to the commands
* @type {Object}
* @readonly
* @private
*/
get commandPath() {
let path = this.client.api.applications(this.client.application.id);
if (this.guild) path = path.guilds(this.guild.id);
return path.commands;
}
/**
* Data that resolves to give an ApplicationCommand object. This can be:
* * An ApplicationCommand object
* * A Snowflake
* @typedef {ApplicationCommand|Snowflake} ApplicationCommandResolvable
*/
/**
* 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 {boolean} [cache=true] Whether to cache the new application commands if they weren't already
* @param {boolean} [force=false] Whether to skip the cache check and request the API
* @returns {Promise<ApplicationCommand|Collection<Snowflake, ApplicationCommand>>}
*/
async fetch(id, cache = true, force = false) {
if (id) {
if (!force) {
const existing = this.cache.get(id);
if (existing) return existing;
}
const command = await this.commandPath(id).get();
return this.add(command, cache);
}
const data = await this.commandPath.get();
return data.reduce((coll, command) => coll.set(command.id, this.add(command, cache)), new Collection());
}
/**
* Creates an application command.
* @param {ApplicationCommandData} command The command
* @returns {Promise<ApplicationCommand>}
*/
async create(command) {
const data = await this.commandPath.post({
data: this.constructor.transformCommand(command),
});
return this.add(data);
}
/**
* Sets all the commands for this application or guild.
* @param {ApplicationCommandData[]} commands The commands
* @returns {Promise<Collection<Snowflake, ApplicationCommand>>}
*/
async set(commands) {
const data = await this.commandPath.put({
data: commands.map(c => this.constructor.transformCommand(c)),
});
return data.reduce((coll, command) => coll.set(command.id, this.add(command)), new Collection());
}
/**
* Edits an application command.
* @param {ApplicationCommandResolvable} command The command to edit
* @param {ApplicationCommandData} data The data to update the command with
* @returns {Promise<ApplicationCommand>}
*/
async edit(command, data) {
const id = this.resolveID(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
const raw = {};
if (data.name) raw.name = data.name;
if (data.description) raw.description = data.description;
if (data.options) raw.options = data.options.map(o => ApplicationCommand.transformOption(o));
if (data.defaultPermission) raw.default_permission = data.defaultPermission;
const patched = await this.commandPath(id).patch({ data: raw });
return this.add(patched);
}
/**
* Deletes an application command.
* @param {ApplicationCommandResolvable} command The command to delete
* @returns {Promise<?ApplicationCommand>}
*/
async delete(command) {
const id = this.resolveID(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
await this.commandPath(id).delete();
const cached = this.cache.get(id);
this.cache.delete(id);
return cached ?? null;
}
/**
* Fetches the permissions for one or multiple commands.
* @param {ApplicationCommandResolvable} [command] The command to get the permissions from
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
*/
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(o => this.constructor.transformPermissions(o, true));
}
const commands = await this.commandPath.permissions.get();
return commands.reduce(
(coll, data) =>
coll.set(
data.id,
data.permissions.map(o => this.constructor.transformPermissions(o, true)),
),
new Collection(),
);
}
/**
* Sets the permissions for a command.
* @param {ApplicationCommandResolvable} command The command to edit the permissions for
* @param {ApplicationCommandPermissions[]} permissions The new permissions for the command
* @returns {Promise<?ApplicationCommand>}
*/
async setPermissions(command, permissions) {
const id = this.resolveID(command);
if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable');
await this.commandPath(id).permissions.put({
data: { permissions: permissions.map(p => this.constructor.transformPermissions(p)) },
});
return this.cache.get(id) ?? null;
}
/**
* Transforms an {@link ApplicationCommandData} object into something that can be used with the API.
* @param {ApplicationCommandData} command The command to transform
* @returns {Object}
* @private
*/
static transformCommand(command) {
return {
...command,
options: command.options?.map(o => ApplicationCommand.transformOption(o)),
default_permission: command.defaultPermission,
};
}
/**
* 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 {
...permissions,
type:
permissions.type === 'number' && !received
? permissions.type
: ApplicationCommandPermissionTypes[permissions.type],
};
}
}
module.exports = ApplicationCommandManager;