-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
guild.go
463 lines (388 loc) · 15.7 KB
/
guild.go
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package discord
// https://discord.com/developers/docs/resources/guild#guild-object
type Guild struct {
// ID is the guild id.
ID GuildID `json:"id,string"`
// Name is the guild name (2-100 characters, excluding trailing and leading
// whitespace).
Name string `json:"name"`
// Icon is the icon hash.&nullableUint64{}
Icon Hash `json:"icon"`
// Splash is the splash hash.
Splash Hash `json:"splash,omitempty"`
// DiscoverySplash is the discovery splash hash.
//
// Only present for guilds with the "DISCOVERABLE" feature.
DiscoverySplash Hash `json:"discovery_splash,omitempty"`
// Owner is true if the user is the owner of the guild.
Owner bool `json:"owner,omitempty"`
// OwnerID is the id of owner.
OwnerID UserID `json:"owner_id,string"`
// Permissions are the total permissions for the user in the guild
// (excludes overrides).
Permissions Permissions `json:"permissions,omitempty"`
// VoiceRegion is the voice region id for the guild.
VoiceRegion string `json:"region"`
// AFKChannelID is the id of the afk channel.
AFKChannelID ChannelID `json:"afk_channel_id,string,omitempty"`
// AFKTimeout is the afk timeout in seconds.
AFKTimeout Seconds `json:"afk_timeout"`
// Embeddable is true if the server widget is enabled.
//
// Deprecated: replaced with WidgetEnabled
Embeddable bool `json:"embed_enabled,omitempty"`
// EmbedChannelID is the channel id that the widget will generate an invite
// to, or null if set to no invite .
//
// Deprecated: replaced with WidgetChannelID
EmbedChannelID ChannelID `json:"embed_channel_id,string,omitempty"`
// Verification is the verification level required for the guild.
Verification Verification `json:"verification_level"`
// Notification is the default message notifications level.
Notification Notification `json:"default_message_notifications"`
// ExplicitFilter is the explicit content filter level.
ExplicitFilter ExplicitFilter `json:"explicit_content_filter"`
// Roles are the roles in the guild.
Roles []Role `json:"roles"`
// Emojis are the custom guild emojis.
Emojis []Emoji `json:"emojis"`
// Features are the enabled guild features.
Features []GuildFeature `json:"guild_features"`
// MFA is the required MFA level for the guild.
MFA MFALevel `json:"mfa"`
// AppID is the application id of the guild creator if it is bot-created.
//
// This field is nullable.
AppID AppID `json:"application_id,string,omitempty"`
// Widget is true if the server widget is enabled.
Widget bool `json:"widget_enabled,omitempty"`
// WidgetChannelID is the channel id that the widget will generate an
// invite to, or null if set to no invite.
WidgetChannelID ChannelID `json:"widget_channel_id,string,omitempty"`
// SystemChannelID is the the id of the channel where guild notices such as
// welcome messages and boost events are posted.
SystemChannelID ChannelID `json:"system_channel_id,string,omitempty"`
// SystemChannelFlags are the system channel flags.
SystemChannelFlags SystemChannelFlags `json:"system_channel_flags"`
// RulesChannelID is the id of the channel where guilds with the "PUBLIC"
// feature can display rules and/or guidelines.
RulesChannelID ChannelID `json:"rules_channel_id"`
// MaxPresences is the maximum number of presences for the guild (the
// default value, currently 25000, is in effect when null is returned, so
// effectively when this field is 0).
MaxPresences uint64 `json:"max_presences,omitempty"`
// MaxMembers the maximum number of members for the guild.
MaxMembers uint64 `json:"max_members,omitempty"`
// VanityURL is the the vanity url code for the guild.
VanityURLCode string `json:"vanity_url_code,omitempty"`
// Description is the description for the guild, if the guild is
// discoverable.
Description string `json:"description,omitempty"`
// Banner is the banner hash.
Banner Hash `json:"banner,omitempty"`
// NitroBoost is the premium tier (Server Boost level).
NitroBoost NitroBoost `json:"premium_tier"`
// NitroBoosters is the number of boosts this guild currently has.
NitroBoosters uint64 `json:"premium_subscription_count,omitempty"`
// PreferredLocale is the the preferred locale of a guild with the "PUBLIC"
// feature; used in server discovery and notices from Discord. Defaults to
// "en-US".
PreferredLocale string `json:"preferred_locale"`
// PublicUpdatesChannelID is the id of the channel where admins and
// moderators of guilds with the "PUBLIC" feature receive notices from
// Discord.
PublicUpdatesChannelID ChannelID `json:"public_updates_channel_id"`
// MaxVideoChannelUsers is the maximum amount of users in a video channel.
MaxVideoChannelUsers uint64 `json:"max_video_channel_users,omitempty"`
// ApproximateMembers is the approximate number of members in this guild, returned from the GET /guild/<id> endpoint when with_counts is true
ApproximateMembers uint64 `json:"approximate_member_count,omitempty"`
// ApproximatePresences is the approximate number of non-offline members in
// this guild, returned by the GuildWithCount method.
ApproximatePresences uint64 `json:"approximate_presence_count,omitempty"`
}
// IconURL returns the URL to the guild icon and auto detects a suitable type.
// An empty string is returned if there's no icon.
func (g Guild) IconURL() string {
return g.IconURLWithType(AutoImage)
}
// IconURLWithType returns the URL to the guild icon using the passed
// ImageType. An empty string is returned if there's no icon.
//
// Supported ImageTypes: PNG, JPEG, WebP, GIF
func (g Guild) IconURLWithType(t ImageType) string {
if g.Icon == "" {
return ""
}
return "https://cdn.discordapp.com/icons/" + g.ID.String() + "/" + t.format(g.Icon)
}
// BannerURL returns the URL to the banner, which is the image on top of the
// channels list. This will always return a link to a PNG file.
func (g Guild) BannerURL() string {
return g.BannerURLWithType(PNGImage)
}
// BannerURLWithType returns the URL to the banner, which is the image on top
// of the channels list using the passed image type.
//
// Supported ImageTypes: PNG, JPEG, WebP
func (g Guild) BannerURLWithType(t ImageType) string {
if g.Banner == "" {
return ""
}
return "https://cdn.discordapp.com/banners/" +
g.ID.String() + "/" + t.format(g.Banner)
}
// SplashURL returns the URL to the guild splash, which is the invite page's
// background. This will always return a link to a PNG file.
func (g Guild) SplashURL() string {
return g.SplashURLWithType(PNGImage)
}
// SplashURLWithType returns the URL to the guild splash, which is the invite
// page's background, using the passed ImageType.
//
// Supported ImageTypes: PNG, JPEG, WebP
func (g Guild) SplashURLWithType(t ImageType) string {
if g.Splash == "" {
return ""
}
return "https://cdn.discordapp.com/splashes/" +
g.ID.String() + "/" + t.format(g.Splash)
}
// DiscoverySplashURL returns the URL to the guild discovery splash.
// This will always return a link to a PNG file.
func (g Guild) DiscoverySplashURL() string {
return g.DiscoverySplashURLWithType(PNGImage)
}
// DiscoverySplashURLWithType returns the URL to the guild discovery splash,
// using the passed ImageType.
//
// Supported ImageTypes: PNG, JPEG, WebP
func (g Guild) DiscoverySplashURLWithType(t ImageType) string {
if g.DiscoverySplash == "" {
return ""
}
return "https://cdn.discordapp.com/splashes/" +
g.ID.String() + "/" + t.format(g.DiscoverySplash)
}
// https://discord.com/developers/docs/resources/guild#guild-preview-object
type GuildPreview struct {
// ID is the guild id.
ID GuildID `json:"id"`
// Name is the guild name (2-100 characters).
Name string `json:"name"`
// Icon is the icon hash.
Icon Hash `json:"icon"`
// Splash is the splash hash.
Splash Hash `json:"splash"`
// DiscoverySplash is the discovery splash hash.
DiscoverySplash Hash `json:"discovery_splash"`
// Emojis are the custom guild emojis.
Emojis []Emoji `json:"emojis"`
// Features are the enabled guild features.
Features []GuildFeature `json:"guild_features"`
// ApproximateMembers is the approximate number of members in this guild.
ApproximateMembers uint64 `json:"approximate_member_count"`
// ApproximatePresences is the approximate number of online members in this
// guild.
ApproximatePresences uint64 `json:"approximate_presence_count"`
// Description is the description for the guild.
Description string `json:"description,omitempty"`
}
// IconURL returns the URL to the guild icon and auto detects a suitable type.
// An empty string is returned if there's no icon.
func (g GuildPreview) IconURL() string {
return g.IconURLWithType(AutoImage)
}
// IconURLWithType returns the URL to the guild icon using the passed
// ImageType. An empty string is returned if there's no icon.
//
// Supported ImageTypes: PNG, JPEG, WebP, GIF
func (g GuildPreview) IconURLWithType(t ImageType) string {
if g.Icon == "" {
return ""
}
return "https://cdn.discordapp.com/icons/" + g.ID.String() + "/" + t.format(g.Icon)
}
// SplashURL returns the URL to the guild splash, which is the invite page's
// background. This will always return a link to a PNG file.
func (g GuildPreview) SplashURL() string {
return g.SplashURLWithType(PNGImage)
}
// SplashURLWithType returns the URL to the guild splash, which is the invite
// page's background, using the passed ImageType.
//
// Supported ImageTypes: PNG, JPEG, WebP
func (g GuildPreview) SplashURLWithType(t ImageType) string {
if g.Splash == "" {
return ""
}
return "https://cdn.discordapp.com/splashes/" +
g.ID.String() + "/" + t.format(g.Splash)
}
// DiscoverySplashURL returns the URL to the guild discovery splash.
// This will always return a link to a PNG file.
func (g GuildPreview) DiscoverySplashURL() string {
return g.DiscoverySplashURLWithType(PNGImage)
}
// DiscoverySplashURLWithType returns the URL to the guild discovery splash,
// using the passed ImageType.
//
// Supported ImageTypes: PNG, JPEG, WebP
func (g GuildPreview) DiscoverySplashURLWithType(t ImageType) string {
if g.DiscoverySplash == "" {
return ""
}
return "https://cdn.discordapp.com/splashes/" +
g.ID.String() + "/" + t.format(g.DiscoverySplash)
}
// https://discord.com/developers/docs/topics/permissions#role-object
type Role struct {
// ID is the role id.
ID RoleID `json:"id,string"`
// Name is the role name.
Name string `json:"name"`
// Color is the integer representation of hexadecimal color code.
Color Color `json:"color"`
// Hoist specifies if this role is pinned in the user listing.
Hoist bool `json:"hoist"`
// Position is the position of this role.
Position int `json:"position"`
// Permissions is the permission bit set.
Permissions Permissions `json:"permissions"`
// Manages specifies whether this role is managed by an integration.
Managed bool `json:"managed"`
// Mentionable specifies whether this role is mentionable.
Mentionable bool `json:"mentionable"`
}
// Mention returns the mention of the Role.
func (r Role) Mention() string {
return "<&" + r.ID.String() + ">"
}
// https://discord.com/developers/docs/topics/gateway#presence-update
type Presence struct {
// User is the user presence is being updated for.
User User `json:"user"`
// RoleIDs are the roles this user is in.
RoleIDs []RoleID `json:"roles"`
// These fields are only filled in gateway events, according to the
// documentation.
// Game is null, or the user's current activity.
Game *Activity `json:"game"`
// GuildID is the id of the guild
GuildID GuildID `json:"guild_id"`
// Status is either "idle", "dnd", "online", or "offline".
Status Status `json:"status"`
// Activities are the user's current activities.
Activities []Activity `json:"activities"`
// ClientStaus is the user's platform-dependent status.
//
// https://discord.com/developers/docs/topics/gateway#client-status-object
ClientStatus struct {
// Desktop is the user's status set for an active desktop (Windows,
// Linux, Mac) application session.
Desktop Status `json:"desktop,omitempty"`
// Mobile is the user's status set for an active mobile (iOS, Android)
// application session.
Mobile Status `json:"mobile,omitempty"`
// Web is the user's status set for an active web (browser, bot
// account) application session.
Web Status `json:"web,omitempty"`
} `json:"client_status"`
// Premium since specifies when the user started boosting the guild.
PremiumSince Timestamp `json:"premium_since,omitempty"`
// Nick is this users guild nickname (if one is set).
Nick string `json:"nick,omitempty"`
}
// https://discord.com/developers/docs/resources/guild#guild-member-object
//
// The field user won't be included in the member object attached to
// MESSAGE_CREATE and MESSAGE_UPDATE gateway events.
type Member struct {
// User is the user this guild member represents.
User User `json:"user"`
// Nick is this users guild nickname.
Nick string `json:"nick,omitempty"`
// RoleIDs is an array of role object ids.
RoleIDs []RoleID `json:"roles"`
// Joined specifies when the user joined the guild.
Joined Timestamp `json:"joined_at"`
// BoostedSince specifies when the user started boosting the guild.
BoostedSince Timestamp `json:"premium_since,omitempty"`
// Deaf specifies whether the user is deafened in voice channels.
Deaf bool `json:"deaf"`
// Mute specifies whether the user is muted in voice channels.
Mute bool `json:"mute"`
}
// Mention returns the mention of the role.
func (m Member) Mention() string {
return "<@!" + m.User.ID.String() + ">"
}
// https://discord.com/developers/docs/resources/guild#ban-object
type Ban struct {
// Reason is the reason for the ban.
Reason string `json:"reason,omitempty"`
// User is the banned user.
User User `json:"user"`
}
// https://discord.com/developers/docs/resources/guild#integration-object
type Integration struct {
// ID is the integration id.
ID IntegrationID `json:"id"`
// Name is the integration name.
Name string `json:"name"`
// Type is the integration type (twitch, youtube, etc).
Type Service `json:"type"`
// Enables specifies if the integration is enabled.
Enabled bool `json:"enabled"`
// Syncing specifies if the integration is syncing.
Syncing bool `json:"syncing"`
// RoleID is the id that this integration uses for "subscribers".
RoleID RoleID `json:"role_id"`
// EnableEmoticons specifies whether emoticons should be synced for this
// integration (twitch only currently).
EnableEmoticons bool `json:"enable_emoticons,omitempty"`
// ExpireBehavior is the behavior of expiring subscribers
ExpireBehavior ExpireBehavior `json:"expire_behavior"`
// ExpireGracePeriod is the grace period (in days) before expiring
// subscribers.
ExpireGracePeriod int `json:"expire_grace_period"`
// User is the user for this integration.
User User `json:"user"`
// Account is the integration account information.
//
// https://discord.com/developers/docs/resources/guild#integration-account-object
Account struct {
// ID is the id of the account.
ID string `json:"id"`
// Name is the name of the account.
Name string `json:"name"`
} `json:"account"`
// SyncedAt specifies when this integration was last synced.
SyncedAt Timestamp `json:"synced_at"`
}
// https://discord.com/developers/docs/resources/guild#guild-widget-object
type GuildWidget struct {
// Enabled specifies whether the widget is enabled.
Enabled bool `json:"enabled"`
// ChannelID is the widget channel id.
ChannelID ChannelID `json:"channel_id,omitempty"`
}
// DefaultMemberColor is the color used for members without colored roles.
var DefaultMemberColor Color = 0x0
// MemberColor computes the effective color of the Member, taking into account
// the role colors.
func MemberColor(guild Guild, member Member) Color {
var c = DefaultMemberColor
var pos int
for _, r := range guild.Roles {
for _, mr := range member.RoleIDs {
if mr != r.ID {
continue
}
if r.Color > 0 && r.Position > pos {
c = r.Color
pos = r.Position
}
}
}
return c
}