GuildPreview.js
4.19 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
'use strict';
const { Collection } = require('@discordjs/collection');
const Base = require('./Base');
const GuildPreviewEmoji = require('./GuildPreviewEmoji');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* Represents the data about the guild any bot can preview, connected to the specified guild.
* @extends {Base}
*/
class GuildPreview extends Base {
constructor(client, data) {
super(client);
if (!data) return;
this._patch(data);
}
_patch(data) {
/**
* The id of this guild
* @type {string}
*/
this.id = data.id;
if ('name' in data) {
/**
* The name of this guild
* @type {string}
*/
this.name = data.name;
}
if ('icon' in data) {
/**
* The icon of this guild
* @type {?string}
*/
this.icon = data.icon;
}
if ('splash' in data) {
/**
* The splash icon of this guild
* @type {?string}
*/
this.splash = data.splash;
}
if ('discovery_splash' in data) {
/**
* The discovery splash icon of this guild
* @type {?string}
*/
this.discoverySplash = data.discovery_splash;
}
if ('features' in data) {
/**
* An array of enabled guild features
* @type {Features[]}
*/
this.features = data.features;
}
if ('approximate_member_count' in data) {
/**
* The approximate count of members in this guild
* @type {number}
*/
this.approximateMemberCount = data.approximate_member_count;
}
if ('approximate_presence_count' in data) {
/**
* The approximate count of online members in this guild
* @type {number}
*/
this.approximatePresenceCount = data.approximate_presence_count;
}
if ('description' in data) {
/**
* The description for this guild
* @type {?string}
*/
this.description = data.description;
} else {
this.description ??= null;
}
if (!this.emojis) {
/**
* Collection of emojis belonging to this guild
* @type {Collection<Snowflake, GuildPreviewEmoji>}
*/
this.emojis = new Collection();
} else {
this.emojis.clear();
}
for (const emoji of data.emojis) {
this.emojis.set(emoji.id, new GuildPreviewEmoji(this.client, emoji, this));
}
}
/**
* The timestamp this guild was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return SnowflakeUtil.timestampFrom(this.id);
}
/**
* The time this guild was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}
/**
* The URL to this guild's splash.
* @param {StaticImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
splashURL({ format, size } = {}) {
return this.splash && this.client.rest.cdn.Splash(this.id, this.splash, format, size);
}
/**
* The URL to this guild's discovery splash.
* @param {StaticImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
discoverySplashURL({ format, size } = {}) {
return this.discoverySplash && this.client.rest.cdn.DiscoverySplash(this.id, this.discoverySplash, format, size);
}
/**
* The URL to this guild's icon.
* @param {ImageURLOptions} [options={}] Options for the Image URL
* @returns {?string}
*/
iconURL({ format, size, dynamic } = {}) {
return this.icon && this.client.rest.cdn.Icon(this.id, this.icon, format, size, dynamic);
}
/**
* Fetches this guild.
* @returns {Promise<GuildPreview>}
*/
async fetch() {
const data = await this.client.api.guilds(this.id).preview.get();
this._patch(data);
return this;
}
/**
* When concatenated with a string, this automatically returns the guild's name instead of the Guild object.
* @returns {string}
* @example
* // Logs: Hello from My Guild!
* console.log(`Hello from ${previewGuild}!`);
*/
toString() {
return this.name;
}
toJSON() {
const json = super.toJSON();
json.iconURL = this.iconURL();
json.splashURL = this.splashURL();
return json;
}
}
module.exports = GuildPreview;