-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
GuildScheduledEvent.ts
167 lines (156 loc) · 7.24 KB
/
GuildScheduledEvent.ts
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
/** @module GuildScheduledEvent */
import Base from "./Base";
import type User from "./User";
import type Guild from "./Guild";
import type StageChannel from "./StageChannel";
import type Client from "../Client";
import type { ImageFormat, GuildScheduledEventEntityTypes, GuildScheduledEventPrivacyLevels, GuildScheduledEventStatuses } from "../Constants";
import * as Routes from "../util/Routes";
import type { RawScheduledEvent, ScheduledEventEntityMetadata } from "../types/scheduled-events";
import type { JSONScheduledEvent } from "../types/json";
import { UncachedError } from "../util/Errors";
/** Represents a guild scheduled event. */
export default class GuildScheduledEvent extends Base {
private _cachedChannel?: StageChannel | null;
private _cachedGuild?: Guild;
/** The id of the channel in which the event will be hosted. `null` if entityType is `EXTERNAL` */
channelID: string | null;
/** The creator of the event. Not present on events created before October 25th, 2021. */
creator?: User;
/** The description of the event. */
description?: string | null;
/** The id of the entity associated with the event. */
entityID: string | null;
/** The [metadata](https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-field-requirements-by-entity-type) associated with the event. */
entityMetadata: ScheduledEventEntityMetadata | null;
/** The [entity type](https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-entity-types) of the event */
entityType: GuildScheduledEventEntityTypes;
/** The id of the guild this scheduled event belongs to. */
guildID: string;
/** The cover image of this event. */
image: string | null;
/** The name of the event. */
name: string;
/** The [privacy level](https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-privacy-level) of the event. */
privacyLevel: GuildScheduledEventPrivacyLevels;
/** The time at which the event will end. Required if entityType is `EXTERNAL`. */
scheduledEndTime: Date | null;
/** The time at which the event will start. */
scheduledStartTime: Date;
/** The [status](https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-object-guild-scheduled-event-status) of the event. */
status: GuildScheduledEventStatuses;
/** The number of users subscribed to the event. */
userCount: number;
constructor(data: RawScheduledEvent, client: Client) {
super(data.id, client);
this.channelID = data.channel_id;
this.entityID = null;
this.entityMetadata = null;
this.entityType = data.entity_type;
this.guildID = data.guild_id;
this.image = null;
this.name = data.name;
this.privacyLevel = data.privacy_level;
this.scheduledEndTime = data.scheduled_end_time ? new Date(data.scheduled_end_time) : null;
this.scheduledStartTime = new Date(data.scheduled_start_time);
this.status = data.status;
this.userCount = 0;
if (data.creator) {
this.creator = client.users.update(data.creator);
}
this.update(data);
}
protected override update(data: Partial<RawScheduledEvent>): void {
if (data.channel_id !== undefined) {
this.channelID = data.channel_id;
}
if (data.description !== undefined) {
this.description = data.description;
}
if (data.entity_id !== undefined) {
this.entityID = data.entity_id;
}
if (data.entity_metadata !== undefined) {
this.entityMetadata = data.entity_metadata;
}
if (data.entity_type !== undefined) {
this.entityType = data.entity_type;
}
if (data.image !== undefined) {
this.image = data.image;
}
if (data.name !== undefined) {
this.name = data.name;
}
if (data.privacy_level !== undefined) {
this.privacyLevel = data.privacy_level;
}
if (data.scheduled_end_time !== undefined) {
this.scheduledEndTime = data.scheduled_end_time ? new Date(data.scheduled_end_time) : null;
}
if (data.scheduled_start_time !== undefined) {
this.scheduledStartTime = new Date(data.scheduled_start_time);
}
if (data.status !== undefined) {
this.status = data.status;
}
if (data.user_count !== undefined) {
this.userCount = data.user_count;
}
}
/** The channel in which the event will be hosted. `null` if entityType is `EXTERNAL` */
get channel(): StageChannel | null | undefined {
if (this.channelID !== null) {
return this._cachedChannel ??= this.client.getChannel<StageChannel>(this.channelID);
}
return this._cachedChannel === null ? this._cachedChannel : (this._cachedChannel = null);
}
/** The guild this scheduled event belongs to. This will throw an error if the guild is not cached. */
get guild(): Guild {
this._cachedGuild ??= this.client.guilds.get(this.guildID);
if (!this._cachedGuild) {
if (this.client.options.restMode) {
throw new UncachedError(`${this.constructor.name}#guild is not present when rest mode is enabled.`);
}
if (!this.client.shards.connected) {
throw new UncachedError(`${this.constructor.name}#guild is not present without a gateway connection.`);
}
throw new UncachedError(`${this.constructor.name}#guild is not present.`);
}
return this._cachedGuild;
}
/**
* Delete this scheduled event.
* @param reason The reason for deleting the scheduled event. Discord's docs do not explicitly state a reason can be provided, so it may not be used.
*/
async deleteScheduledEvent(reason?: string): Promise<void> {
return this.client.rest.guilds.deleteScheduledEvent(this.guildID, this.id, reason);
}
/**
* The url of this event's cover image.
* @param format The format of the image.
* @param size The size of the image.
*/
imageURL(format?: ImageFormat, size?: number): string | null {
return this.image ? this.client.util.formatImage(Routes.GUILD_SCHEDULED_EVENT_COVER(this.id, this.image), format, size) : null;
}
override toJSON(): JSONScheduledEvent {
return {
...super.toJSON(),
channelID: this.channelID,
creator: this.creator?.toJSON(),
description: this.description,
entityID: this.entityID,
entityMetadata: this.entityMetadata,
entityType: this.entityType,
guildID: this.guildID,
image: this.image,
name: this.name,
privacyLevel: this.privacyLevel,
scheduledEndTime: this.scheduledEndTime?.getTime() ?? null,
scheduledStartTime: this.scheduledStartTime.getTime(),
status: this.status,
userCount: this.userCount
};
}
}