/
StageInstanceManager.js
158 lines (135 loc) · 5.35 KB
/
StageInstanceManager.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
'use strict';
const CachedManager = require('./CachedManager');
const { TypeError, Error } = require('../errors');
const { StageInstance } = require('../structures/StageInstance');
const { PrivacyLevels } = require('../util/Constants');
/**
* Manages API methods for {@link StageInstance} objects and holds their cache.
* @extends {CachedManager}
*/
class StageInstanceManager extends CachedManager {
constructor(guild, iterable) {
super(guild.client, StageInstance, iterable);
/**
* The guild this manager belongs to
* @type {Guild}
*/
this.guild = guild;
}
/**
* The cache of this Manager
* @type {Collection<Snowflake, StageInstance>}
* @name StageInstanceManager#cache
*/
/**
* Options used to create a stage instance.
* @typedef {Object} StageInstanceCreateOptions
* @property {string} topic The topic of the stage instance
* @property {PrivacyLevel|number} [privacyLevel] The privacy level of the stage instance
* @property {boolean} [sendStartNotification] Whether to notify `@everyone` that the stage instance has started
*/
/**
* Data that can be resolved to a Stage Channel object. This can be:
* * A StageChannel
* * A Snowflake
* @typedef {StageChannel|Snowflake} StageChannelResolvable
*/
/**
* Creates a new stage instance.
* @param {StageChannelResolvable} channel The stage channel to associate the created stage instance to
* @param {StageInstanceCreateOptions} options The options to create the stage instance
* @returns {Promise<StageInstance>}
* @example
* // Create a stage instance
* guild.stageInstances.create('1234567890123456789', {
* topic: 'A very creative topic',
* privacyLevel: 'GUILD_ONLY'
* })
* .then(stageInstance => console.log(stageInstance))
* .catch(console.error);
*/
async create(channel, options) {
const channelId = this.guild.channels.resolveId(channel);
if (!channelId) throw new Error('STAGE_CHANNEL_RESOLVE');
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
let { topic, privacyLevel, sendStartNotification } = options;
privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
const data = await this.client.api['stage-instances'].post({
data: {
channel_id: channelId,
topic,
privacy_level: privacyLevel,
send_start_notification: sendStartNotification,
},
});
return this._add(data);
}
/**
* Fetches the stage instance associated with a stage channel, if it exists.
* @param {StageChannelResolvable} channel The stage channel whose associated stage instance is to be fetched
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @returns {Promise<StageInstance>}
* @example
* // Fetch a stage instance
* guild.stageInstances.fetch('1234567890123456789')
* .then(stageInstance => console.log(stageInstance))
* .catch(console.error);
*/
async fetch(channel, { cache = true, force = false } = {}) {
const channelId = this.guild.channels.resolveId(channel);
if (!channelId) throw new Error('STAGE_CHANNEL_RESOLVE');
if (!force) {
const existing = this.cache.find(stageInstance => stageInstance.channelId === channelId);
if (existing) return existing;
}
const data = await this.client.api('stage-instances', channelId).get();
return this._add(data, cache);
}
/**
* Options used to edit an existing stage instance.
* @typedef {Object} StageInstanceEditOptions
* @property {string} [topic] The new topic of the stage instance
* @property {PrivacyLevel|number} [privacyLevel] The new privacy level of the stage instance
*/
/**
* Edits an existing stage instance.
* @param {StageChannelResolvable} channel The stage channel whose associated stage instance is to be edited
* @param {StageInstanceEditOptions} options The options to edit the stage instance
* @returns {Promise<StageInstance>}
* @example
* // Edit a stage instance
* guild.stageInstances.edit('1234567890123456789', { topic: 'new topic' })
* .then(stageInstance => console.log(stageInstance))
* .catch(console.error);
*/
async edit(channel, options) {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
const channelId = this.guild.channels.resolveId(channel);
if (!channelId) throw new Error('STAGE_CHANNEL_RESOLVE');
let { topic, privacyLevel } = options;
privacyLevel &&= typeof privacyLevel === 'number' ? privacyLevel : PrivacyLevels[privacyLevel];
const data = await this.client.api('stage-instances', channelId).patch({
data: {
topic,
privacy_level: privacyLevel,
},
});
if (this.cache.has(data.id)) {
const clone = this.cache.get(data.id)._clone();
clone._patch(data);
return clone;
}
return this._add(data);
}
/**
* Deletes an existing stage instance.
* @param {StageChannelResolvable} channel The stage channel whose associated stage instance is to be deleted
* @returns {Promise<void>}
*/
async delete(channel) {
const channelId = this.guild.channels.resolveId(channel);
if (!channelId) throw new Error('STAGE_CHANNEL_RESOLVE');
await this.client.api('stage-instances', channelId).delete();
}
}
module.exports = StageInstanceManager;