/
StageInstance.js
208 lines (184 loc) · 5.09 KB
/
StageInstance.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
202
203
204
205
206
207
208
'use strict';
const process = require('node:process');
const Base = require('./Base');
const { PrivacyLevels } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* @type {WeakSet<StageInstance>}
* @private
* @internal
*/
const deletedStageInstances = new WeakSet();
let deprecationEmittedForDeleted = false;
/**
* Represents a stage instance.
* @extends {Base}
*/
class StageInstance extends Base {
constructor(client, data) {
super(client);
/**
* The stage instance's id
* @type {Snowflake}
*/
this.id = data.id;
this._patch(data);
}
_patch(data) {
if ('guild_id' in data) {
/**
* The id of the guild associated with the stage channel
* @type {Snowflake}
*/
this.guildId = data.guild_id;
}
if ('channel_id' in data) {
/**
* The id of the channel associated with the stage channel
* @type {Snowflake}
*/
this.channelId = data.channel_id;
}
if ('topic' in data) {
/**
* The topic of the stage instance
* @type {string}
*/
this.topic = data.topic;
}
if ('privacy_level' in data) {
/**
* The privacy level of the stage instance
* @type {PrivacyLevel}
*/
this.privacyLevel = PrivacyLevels[data.privacy_level];
}
if ('discoverable_disabled' in data) {
/**
* Whether or not stage discovery is disabled
* @type {?boolean}
*/
this.discoverableDisabled = data.discoverable_disabled;
} else {
this.discoverableDisabled ??= null;
}
if ('guild_scheduled_event_id' in data) {
/**
* The associated guild scheduled event id of this stage instance
* @type {?Snowflake}
*/
this.guildScheduledEventId = data.guild_scheduled_event_id;
} else {
this.guildScheduledEventId ??= null;
}
}
/**
* The stage channel associated with this stage instance
* @type {?StageChannel}
* @readonly
*/
get channel() {
return this.client.channels.resolve(this.channelId);
}
/**
* The associated guild scheduled event of this stage instance
* @type {?GuildScheduledEvent}
* @readonly
*/
get guildScheduledEvent() {
return this.guild?.scheduledEvents.resolve(this.guildScheduledEventId) ?? null;
}
/**
* Whether or not the stage instance has been deleted
* @type {boolean}
* @deprecated This will be removed in the next major version, see https://github.com/discordjs/discord.js/issues/7091
*/
get deleted() {
if (!deprecationEmittedForDeleted) {
deprecationEmittedForDeleted = true;
process.emitWarning(
'StageInstance#deleted is deprecated, see https://github.com/discordjs/discord.js/issues/7091.',
'DeprecationWarning',
);
}
return deletedStageInstances.has(this);
}
set deleted(value) {
if (!deprecationEmittedForDeleted) {
deprecationEmittedForDeleted = true;
process.emitWarning(
'StageInstance#deleted is deprecated, see https://github.com/discordjs/discord.js/issues/7091.',
'DeprecationWarning',
);
}
if (value) deletedStageInstances.add(this);
else deletedStageInstances.delete(this);
}
/**
* The guild this stage instance belongs to
* @type {?Guild}
* @readonly
*/
get guild() {
return this.client.guilds.resolve(this.guildId);
}
/**
* Edits this stage instance.
* @param {StageInstanceEditOptions} options The options to edit the stage instance
* @returns {Promise<StageInstance>}
* @example
* // Edit a stage instance
* stageInstance.edit({ topic: 'new topic' })
* .then(stageInstance => console.log(stageInstance))
* .catch(console.error)
*/
edit(options) {
return this.guild.stageInstances.edit(this.channelId, options);
}
/**
* Deletes this stage instance.
* @returns {Promise<StageInstance>}
* @example
* // Delete a stage instance
* stageInstance.delete()
* .then(stageInstance => console.log(stageInstance))
* .catch(console.error);
*/
async delete() {
await this.guild.stageInstances.delete(this.channelId);
const clone = this._clone();
deletedStageInstances.add(clone);
return clone;
}
/**
* Sets the topic of this stage instance.
* @param {string} topic The topic for the stage instance
* @returns {Promise<StageInstance>}
* @example
* // Set topic of a stage instance
* stageInstance.setTopic('new topic')
* .then(stageInstance => console.log(`Set the topic to: ${stageInstance.topic}`))
* .catch(console.error);
*/
setTopic(topic) {
return this.guild.stageInstances.edit(this.channelId, { topic });
}
/**
* The timestamp this stage instances was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.timestampFrom(this.id);
}
/**
* The time this stage instance was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
}
exports.StageInstance = StageInstance;
exports.deletedStageInstances = deletedStageInstances;