GuildScheduledEventManager.js
11.6 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
'use strict';
const { Collection } = require('@discordjs/collection');
const CachedManager = require('./CachedManager');
const { TypeError, Error } = require('../errors');
const { GuildScheduledEvent } = require('../structures/GuildScheduledEvent');
const { PrivacyLevels, GuildScheduledEventEntityTypes, GuildScheduledEventStatuses } = require('../util/Constants');
/**
* Manages API methods for GuildScheduledEvents and stores their cache.
* @extends {CachedManager}
*/
class GuildScheduledEventManager extends CachedManager {
constructor(guild, iterable) {
super(guild.client, GuildScheduledEvent, iterable);
/**
* The guild this manager belongs to
* @type {Guild}
*/
this.guild = guild;
}
/**
* The cache of this manager
* @type {Collection<Snowflake, GuildScheduledEvent>}
* @name GuildScheduledEventManager#cache
*/
/**
* Data that resolves to give a GuildScheduledEvent object. This can be:
* * A Snowflake
* * A GuildScheduledEvent object
* @typedef {Snowflake|GuildScheduledEvent} GuildScheduledEventResolvable
*/
/**
* Options used to create a guild scheduled event.
* @typedef {Object} GuildScheduledEventCreateOptions
* @property {string} name The name of the guild scheduled event
* @property {DateResolvable} scheduledStartTime The time to schedule the event at
* @property {DateResolvable} [scheduledEndTime] The time to end the event at
* <warn>This is required if `entityType` is 'EXTERNAL'</warn>
* @property {PrivacyLevel|number} privacyLevel The privacy level of the guild scheduled event
* @property {GuildScheduledEventEntityType|number} entityType The scheduled entity type of the event
* @property {string} [description] The description of the guild scheduled event
* @property {GuildVoiceChannelResolvable} [channel] The channel of the guild scheduled event
* <warn>This is required if `entityType` is 'STAGE_INSTANCE' or `VOICE`</warn>
* @property {GuildScheduledEventEntityMetadataOptions} [entityMetadata] The entity metadata of the
* guild scheduled event
* <warn>This is required if `entityType` is 'EXTERNAL'</warn>
* @property {string} [reason] The reason for creating the guild scheduled event
*/
/**
* Options used to set entity metadata of a guild scheduled event.
* @typedef {Object} GuildScheduledEventEntityMetadataOptions
* @property {string} [location] The location of the guild scheduled event
* <warn>This is required if `entityType` is 'EXTERNAL'</warn>
*/
/**
* Creates a new guild scheduled event.
* @param {GuildScheduledEventCreateOptions} options Options for creating the guild scheduled event
* @returns {Promise<GuildScheduledEvent>}
*/
async create(options) {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
let {
privacyLevel,
entityType,
channel,
name,
scheduledStartTime,
description,
scheduledEndTime,
entityMetadata,
reason,
} = options;
if (typeof privacyLevel === 'string') privacyLevel = PrivacyLevels[privacyLevel];
if (typeof entityType === 'string') entityType = GuildScheduledEventEntityTypes[entityType];
let entity_metadata, channel_id;
if (entityType === GuildScheduledEventEntityTypes.EXTERNAL) {
channel_id = typeof channel === 'undefined' ? channel : null;
entity_metadata = { location: entityMetadata?.location };
} else {
channel_id = this.guild.channels.resolveId(channel);
if (!channel_id) throw new Error('GUILD_VOICE_CHANNEL_RESOLVE');
entity_metadata = typeof entityMetadata === 'undefined' ? entityMetadata : null;
}
const data = await this.client.api.guilds(this.guild.id, 'scheduled-events').post({
data: {
channel_id,
name,
privacy_level: privacyLevel,
scheduled_start_time: new Date(scheduledStartTime).toISOString(),
scheduled_end_time: scheduledEndTime ? new Date(scheduledEndTime).toISOString() : scheduledEndTime,
description,
entity_type: entityType,
entity_metadata,
},
reason,
});
return this._add(data);
}
/**
* Options used to fetch a single guild scheduled event from a guild.
* @typedef {BaseFetchOptions} FetchGuildScheduledEventOptions
* @property {GuildScheduledEventResolvable} guildScheduledEvent The guild scheduled event to fetch
* @property {boolean} [withUserCount=true] Whether to fetch the number of users subscribed to the scheduled event
*/
/**
* Options used to fetch multiple guild scheduled events from a guild.
* @typedef {Object} FetchGuildScheduledEventsOptions
* @property {boolean} [cache] Whether or not to cache the fetched guild scheduled events
* @property {boolean} [withUserCount=true] Whether to fetch the number of users subscribed to each scheduled event
* should be returned
*/
/**
* Obtains one or more guild scheduled events from Discord, or the guild cache if it's already available.
* @param {GuildScheduledEventResolvable|FetchGuildScheduledEventOptions|FetchGuildScheduledEventsOptions} [options]
* The id of the guild scheduled event or options
* @returns {Promise<GuildScheduledEvent|Collection<Snowflake, GuildScheduledEvent>>}
*/
async fetch(options = {}) {
const id = this.resolveId(options.guildScheduledEvent ?? options);
if (id) {
if (!options.force) {
const existing = this.cache.get(id);
if (existing) return existing;
}
const data = await this.client.api
.guilds(this.guild.id, 'scheduled-events', id)
.get({ query: { with_user_count: options.withUserCount ?? true } });
return this._add(data, options.cache);
}
const data = await this.client.api
.guilds(this.guild.id, 'scheduled-events')
.get({ query: { with_user_count: options.withUserCount ?? true } });
return data.reduce(
(coll, rawGuildScheduledEventData) =>
coll.set(
rawGuildScheduledEventData.id,
this.guild.scheduledEvents._add(rawGuildScheduledEventData, options.cache),
),
new Collection(),
);
}
/**
* Options used to edit a guild scheduled event.
* @typedef {Object} GuildScheduledEventEditOptions
* @property {string} [name] The name of the guild scheduled event
* @property {DateResolvable} [scheduledStartTime] The time to schedule the event at
* @property {DateResolvable} [scheduledEndTime] The time to end the event at
* @property {PrivacyLevel|number} [privacyLevel] The privacy level of the guild scheduled event
* @property {GuildScheduledEventEntityType|number} [entityType] The scheduled entity type of the event
* @property {string} [description] The description of the guild scheduled event
* @property {?GuildVoiceChannelResolvable} [channel] The channel of the guild scheduled event
* @property {GuildScheduledEventStatus|number} [status] The status of the guild scheduled event
* @property {GuildScheduledEventEntityMetadataOptions} [entityMetadata] The entity metadata of the
* guild scheduled event
* <warn>This can be modified only if `entityType` of the `GuildScheduledEvent` to be edited is 'EXTERNAL'</warn>
* @property {string} [reason] The reason for editing the guild scheduled event
*/
/**
* Edits a guild scheduled event.
* @param {GuildScheduledEventResolvable} guildScheduledEvent The guild scheduled event to edit
* @param {GuildScheduledEventEditOptions} options Options to edit the guild scheduled event
* @returns {Promise<GuildScheduledEvent>}
*/
async edit(guildScheduledEvent, options) {
const guildScheduledEventId = this.resolveId(guildScheduledEvent);
if (!guildScheduledEventId) throw new Error('GUILD_SCHEDULED_EVENT_RESOLVE');
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
let {
privacyLevel,
entityType,
channel,
status,
name,
scheduledStartTime,
description,
scheduledEndTime,
entityMetadata,
reason,
} = options;
if (typeof privacyLevel === 'string') privacyLevel = PrivacyLevels[privacyLevel];
if (typeof entityType === 'string') entityType = GuildScheduledEventEntityTypes[entityType];
if (typeof status === 'string') status = GuildScheduledEventStatuses[status];
let entity_metadata;
if (entityMetadata) {
entity_metadata = {
location: entityMetadata.location,
};
}
const data = await this.client.api.guilds(this.guild.id, 'scheduled-events', guildScheduledEventId).patch({
data: {
channel_id: typeof channel === 'undefined' ? channel : this.guild.channels.resolveId(channel),
name,
privacy_level: privacyLevel,
scheduled_start_time: scheduledStartTime ? new Date(scheduledStartTime).toISOString() : undefined,
scheduled_end_time: scheduledEndTime ? new Date(scheduledEndTime).toISOString() : scheduledEndTime,
description,
entity_type: entityType,
status,
entity_metadata,
},
reason,
});
return this._add(data);
}
/**
* Deletes a guild scheduled event.
* @param {GuildScheduledEventResolvable} guildScheduledEvent The guild scheduled event to delete
* @returns {Promise<void>}
*/
async delete(guildScheduledEvent) {
const guildScheduledEventId = this.resolveId(guildScheduledEvent);
if (!guildScheduledEventId) throw new Error('GUILD_SCHEDULED_EVENT_RESOLVE');
await this.client.api.guilds(this.guild.id, 'scheduled-events', guildScheduledEventId).delete();
}
/**
* Options used to fetch subscribers of a guild scheduled event
* @typedef {Object} FetchGuildScheduledEventSubscribersOptions
* @property {number} [limit] The maximum numbers of users to fetch
* @property {boolean} [withMember] Whether to fetch guild member data of the users
* @property {Snowflake} [before] Consider only users before this user id
* @property {Snowflake} [after] Consider only users after this user id
* <warn>If both `before` and `after` are provided, only `before` is respected</warn>
*/
/**
* Represents a subscriber of a {@link GuildScheduledEvent}
* @typedef {Object} GuildScheduledEventUser
* @property {Snowflake} guildScheduledEventId The id of the guild scheduled event which the user subscribed to
* @property {User} user The user that subscribed to the guild scheduled event
* @property {?GuildMember} member The guild member associated with the user, if any
*/
/**
* Fetches subscribers of a guild scheduled event.
* @param {GuildScheduledEventResolvable} guildScheduledEvent The guild scheduled event to fetch subscribers of
* @param {FetchGuildScheduledEventSubscribersOptions} [options={}] Options for fetching the subscribers
* @returns {Promise<Collection<Snowflake, GuildScheduledEventUser>>}
*/
async fetchSubscribers(guildScheduledEvent, options = {}) {
const guildScheduledEventId = this.resolveId(guildScheduledEvent);
if (!guildScheduledEventId) throw new Error('GUILD_SCHEDULED_EVENT_RESOLVE');
let { limit, withMember, before, after } = options;
const data = await this.client.api.guilds(this.guild.id, 'scheduled-events', guildScheduledEventId).users.get({
query: { limit, with_member: withMember, before, after },
});
return data.reduce(
(coll, rawData) =>
coll.set(rawData.user.id, {
guildScheduledEventId: rawData.guild_scheduled_event_id,
user: this.client.users._add(rawData.user),
member: rawData.member ? this.guild.members._add({ ...rawData.member, user: rawData.user }) : null,
}),
new Collection(),
);
}
}
module.exports = GuildScheduledEventManager;