forked from diamondburned/arikawa
/
guild.go
536 lines (464 loc) · 18.8 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package api
import (
"io"
"net/url"
"github.com/germanoeich/arikawa/v3/discord" // for clarity
"github.com/germanoeich/arikawa/v3/internal/intmath"
"github.com/germanoeich/arikawa/v3/utils/httputil"
"github.com/germanoeich/arikawa/v3/utils/json/option"
)
// MaxGuildFetchLimit is the limit of max guilds per request, as imposed by
// Discord.
const MaxGuildFetchLimit = 100
var EndpointGuilds = Endpoint + "guilds/"
// https://discord.com/developers/docs/resources/guild#create-guild-json-params
type CreateGuildData struct {
// Name is the name of the guild (2-100 characters)
Name string `json:"name"`
// VoiceRegion is the voice region id.
VoiceRegion string `json:"region,omitempty"`
// Icon is the base64 128x128 image for the guild icon.
Icon *Image `json:"image,omitempty"`
// Verification is the verification level.
Verification *discord.Verification `json:"verification_level,omitempty"`
// Notification is the default message notification level.
Notification *discord.Notification `json:"default_message_notifications,omitempty"`
// ExplicitFilter is the explicit content filter level.
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter,omitempty"`
// Roles are the new guild roles.
//
// When using the roles parameter, the first member of the array is used to
// change properties of the guild's @everyone role. If you are trying to
// bootstrap a guild with additional roles, keep this in mind.
//
// When using the roles parameter, the required id field within each role
// object is an integer placeholder, and will be replaced by the API upon
// consumption. Its purpose is to allow you to overwrite a role's
// permissions in a channel when also passing in channels with the channels
// array.
Roles []discord.Role `json:"roles,omitempty"`
// Channels are the new guild's channels.
// Assigning a channel to a channel category is not supported by this
// endpoint, i.e. a channel can't have the parent_id field.
//
// When using the channels parameter, the position field is ignored,
// and none of the default channels are created.
//
// When using the channels parameter, the id field within each channel
// object may be set to an integer placeholder, and will be replaced by the
// API upon consumption. Its purpose is to allow you to create
// GUILD_CATEGORY channels by setting the parent_id field on any children
// to the category's id field. Category channels must be listed before any
// children.
Channels []discord.Channel `json:"channels,omitempty"`
// AFKChannelID is the id for the afk channel.
AFKChannelID discord.ChannelID `json:"afk_channel_id,omitempty"`
// AFKTimeout is the afk timeout in seconds.
AFKTimeout option.Seconds `json:"afk_timeout,omitempty"`
// SystemChannelID is the id of the channel where guild notices such as
// welcome messages and boost events are posted.
SystemChannelID discord.ChannelID `json:"system_channel_id,omitempty"`
}
// CreateGuild creates a new guild. Returns a guild object on success.
// Fires a Guild Create Gateway event.
//
// This endpoint can be used only by bots in less than 10 guilds.
func (c *Client) CreateGuild(data CreateGuildData) (*discord.Guild, error) {
var g *discord.Guild
return g, c.RequestJSON(&g, "POST", Endpoint+"guilds", httputil.WithJSONBody(data))
}
// Guild returns the guild object for the given id.
// ApproximateMembers and ApproximatePresences will not be set.
func (c *Client) Guild(id discord.GuildID) (*discord.Guild, error) {
var g *discord.Guild
return g, c.RequestJSON(&g, "GET", EndpointGuilds+id.String())
}
// GuildPreview returns the guild preview object for the given id, even if the
// user is not in the guild.
//
// This endpoint is only for public guilds.
func (c *Client) GuildPreview(id discord.GuildID) (*discord.GuildPreview, error) {
var g *discord.GuildPreview
return g, c.RequestJSON(&g, "GET", EndpointGuilds+id.String()+"/preview")
}
// GuildWithCount returns the guild object for the given id.
// This will also set the ApproximateMembers and ApproximatePresences fields
// of the guild struct.
func (c *Client) GuildWithCount(id discord.GuildID) (*discord.Guild, error) {
var g *discord.Guild
return g, c.RequestJSON(
&g, "GET",
EndpointGuilds+id.String(),
httputil.WithSchema(c, url.Values{
"with_counts": {"true"},
}),
)
}
// Guilds returns a list of partial guild objects the current user is a member
// of. This method automatically paginates until it reaches the passed limit,
// or, if the limit is set to 0, has fetched all guilds the user has joined.
//
// As the underlying endpoint has a maximum of 100 guilds per request, at
// maximum a total of limit/100 rounded up requests will be made, although they
// may be less, if no more guilds are available.
//
// When fetching the guilds, those with the smallest ID will be fetched first.
//
// Also note that 100 is the maximum number of guilds a non-bot user can join.
// Therefore, pagination is not needed for integrations that need to get a list
// of the users' guilds.
//
// Requires the guilds OAuth2 scope.
func (c *Client) Guilds(limit uint) ([]discord.Guild, error) {
return c.GuildsAfter(0, limit)
}
// GuildsBefore returns a list of partial guild objects the current user is a
// member of. This method automatically paginates until it reaches the
// passed limit, or, if the limit is set to 0, has fetched all guilds with an
// id smaller than before.
//
// As the underlying endpoint has a maximum of 100 guilds per request, at
// maximum a total of limit/100 rounded up requests will be made, although they
// may be less, if no more guilds are available.
//
// Requires the guilds OAuth2 scope.
func (c *Client) GuildsBefore(before discord.GuildID, limit uint) ([]discord.Guild, error) {
guilds := make([]discord.Guild, 0, limit)
fetch := uint(MaxGuildFetchLimit)
unlimited := limit == 0
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit)))
limit -= fetch
}
g, err := c.guildsRange(before, 0, fetch)
if err != nil {
return guilds, err
}
guilds = append(g, guilds...)
if len(g) < MaxGuildFetchLimit {
break
}
before = g[0].ID
}
if len(guilds) == 0 {
return nil, nil
}
return guilds, nil
}
// GuildsAfter returns a list of partial guild objects the current user is a
// member of. This method automatically paginates until it reaches the
// passed limit, or, if the limit is set to 0, has fetched all guilds with an
// id higher than after.
//
// As the underlying endpoint has a maximum of 100 guilds per request, at
// maximum a total of limit/100 rounded up requests will be made, although they
// may be less, if no more guilds are available.
//
// Requires the guilds OAuth2 scope.
func (c *Client) GuildsAfter(after discord.GuildID, limit uint) ([]discord.Guild, error) {
guilds := make([]discord.Guild, 0, limit)
fetch := uint(MaxGuildFetchLimit)
unlimited := limit == 0
for limit > 0 || unlimited {
if limit > 0 {
// Only fetch as much as we need. Since limit gradually decreases,
// we only need to fetch intmath.Min(fetch, limit).
fetch = uint(intmath.Min(MaxGuildFetchLimit, int(limit)))
limit -= fetch
}
g, err := c.guildsRange(0, after, fetch)
if err != nil {
return guilds, err
}
guilds = append(guilds, g...)
if len(g) < MaxGuildFetchLimit {
break
}
after = g[len(g)-1].ID
}
if len(guilds) == 0 {
return nil, nil
}
return guilds, nil
}
func (c *Client) guildsRange(
before, after discord.GuildID, limit uint) ([]discord.Guild, error) {
var param struct {
Before discord.GuildID `schema:"before,omitempty"`
After discord.GuildID `schema:"after,omitempty"`
Limit uint `schema:"limit"`
}
param.Before = before
param.After = after
param.Limit = limit
var gs []discord.Guild
return gs, c.RequestJSON(
&gs, "GET",
EndpointMe+"/guilds",
httputil.WithSchema(c, param),
)
}
// LeaveGuild leaves a guild.
func (c *Client) LeaveGuild(id discord.GuildID) error {
return c.FastRequest("DELETE", EndpointMe+"/guilds/"+id.String())
}
// https://discord.com/developers/docs/resources/guild#modify-guild-json-params
type ModifyGuildData struct {
// Name is the guild's name.
Name string `json:"name,omitempty"`
// Region is the guild's voice region id.
Region option.NullableString `json:"region,omitempty"`
// Verification is the verification level.
//
// This field is nullable.
Verification *discord.Verification `json:"verification_level,omitempty"`
// Notification is the default message notification level.
//
// This field is nullable.
Notification *discord.Notification `json:"default_message_notifications,omitempty"`
// ExplicitFilter is the explicit content filter level.
//
// This field is nullable.
ExplicitFilter *discord.ExplicitFilter `json:"explicit_content_filter,omitempty"`
// AFKChannelID is the id for the afk channel.
//
// This field is nullable.
AFKChannelID discord.ChannelID `json:"afk_channel_id,string,omitempty"`
// AFKTimeout is the afk timeout in seconds.
AFKTimeout option.Seconds `json:"afk_timeout,omitempty"`
// Icon is the base64 1024x1024 png/jpeg/gif image for the guild icon
// (can be animated gif when the server has the ANIMATED_ICON feature).
Icon *Image `json:"icon,omitempty"`
// Splash is the base64 16:9 png/jpeg image for the guild splash
// (when the server has the INVITE_SPLASH feature).
Splash *Image `json:"splash,omitempty"`
// Banner is the base64 16:9 png/jpeg image for the guild banner (when the
// server has BANNER feature).
Banner *Image `json:"banner,omitempty"`
// OwnerID is the user id to transfer guild ownership to (must be owner).
OwnerID discord.UserID `json:"owner_id,omitempty"`
// SystemChannelID is the id of the channel where guild notices such as
// welcome messages and boost events are posted.
//
// This field is nullable.
SystemChannelID discord.ChannelID `json:"system_channel_id,omitempty"`
// RulesChannelID is the id of the channel where "PUBLIC" guilds display
// rules and/or guidelines.
//
// This field is nullable.
RulesChannelID discord.ChannelID `json:"rules_channel_id,omitempty"`
// PublicUpdatesChannelID is the id of the channel where admins and
// moderators of "PUBLIC" guilds receive notices from Discord.
//
// This field is nullable.
PublicUpdatesChannelID discord.ChannelID `json:"public_updates_channel_id,omitempty"`
// PreferredLocale is the preferred locale of a "PUBLIC" guild used in
// server discovery and notices from Discord.
//
// This defaults to "en-US".
PreferredLocale option.NullableString `json:"preferred_locale,omitempty"`
}
// ModifyGuild modifies a guild's settings. Requires the MANAGE_GUILD permission.
// Fires a Guild Update Gateway event.
func (c *Client) ModifyGuild(id discord.GuildID, data ModifyGuildData) (*discord.Guild, error) {
var g *discord.Guild
return g, c.RequestJSON(
&g, "PATCH",
EndpointGuilds+id.String(),
httputil.WithJSONBody(data),
)
}
// DeleteGuild deletes a guild permanently. The User must be owner.
//
// Fires a Guild Delete Gateway event.
func (c *Client) DeleteGuild(id discord.GuildID) error {
return c.FastRequest("DELETE", EndpointGuilds+id.String())
}
// GuildVoiceRegions is the same as /voice, but returns VIP ones as well if
// available.
func (c *Client) VoiceRegionsGuild(guildID discord.GuildID) ([]discord.VoiceRegion, error) {
var vrs []discord.VoiceRegion
return vrs, c.RequestJSON(&vrs, "GET", EndpointGuilds+guildID.String()+"/regions")
}
// https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log-query-string-parameters
type AuditLogData struct {
// UserID filters the log for actions made by a user.
UserID discord.UserID `schema:"user_id,omitempty"`
// ActionType is the type of audit log event.
ActionType discord.AuditLogEvent `schema:"action_type,omitempty"`
// Before filters the log before a certain entry ID.
Before discord.AuditLogEntryID `schema:"before,omitempty"`
// Limit limits how many entries are returned (default 50, minimum 1,
// maximum 100).
Limit uint `schema:"limit"`
}
// AuditLog returns an audit log object for the guild.
//
// Requires the VIEW_AUDIT_LOG permission.
func (c *Client) AuditLog(guildID discord.GuildID, data AuditLogData) (*discord.AuditLog, error) {
switch {
case data.Limit == 0:
data.Limit = 50
case data.Limit > 100:
data.Limit = 100
}
var audit *discord.AuditLog
return audit, c.RequestJSON(
&audit, "GET",
EndpointGuilds+guildID.String()+"/audit-logs",
httputil.WithSchema(c, data),
)
}
// Integrations returns a list of integration objects for the guild.
//
// Requires the MANAGE_GUILD permission.
func (c *Client) Integrations(guildID discord.GuildID) ([]discord.Integration, error) {
var ints []discord.Integration
return ints, c.RequestJSON(&ints, "GET", EndpointGuilds+guildID.String()+"/integrations")
}
// AttachIntegration attaches an integration object from the current user to
// the guild.
//
// Requires the MANAGE_GUILD permission.
// Fires a Guild Integrations Update Gateway event.
func (c *Client) AttachIntegration(
guildID discord.GuildID, integrationID discord.IntegrationID,
integrationType discord.Service) error {
var param struct {
Type discord.Service `json:"type"`
ID discord.IntegrationID `json:"id"`
}
param.Type = integrationType
param.ID = integrationID
return c.FastRequest(
"POST",
EndpointGuilds+guildID.String()+"/integrations",
httputil.WithJSONBody(param),
)
}
// https://discord.com/developers/docs/resources/guild#modify-guild-integration-json-params
type ModifyIntegrationData struct {
// ExpireBehavior is the behavior when an integration subscription lapses
// (see the integration expire behaviors documentation).
ExpireBehavior *discord.ExpireBehavior `json:"expire_behavior,omitempty"`
// ExpireGracePeriod is the period (in days) where the integration will
// ignore lapsed subscriptions.
ExpireGracePeriod option.NullableInt `json:"expire_grace_period,omitempty"`
// EnableEmoticons specifies whether emoticons should be synced for this
// integration (twitch only currently).
EnableEmoticons option.NullableBool `json:"enable_emoticons,omitempty"`
}
// ModifyIntegration modifies the behavior and settings of an integration
// object for the guild.
//
// Requires the MANAGE_GUILD permission.
// Fires a Guild Integrations Update Gateway event.
func (c *Client) ModifyIntegration(
guildID discord.GuildID, integrationID discord.IntegrationID, data ModifyIntegrationData) error {
return c.FastRequest(
"PATCH",
EndpointGuilds+guildID.String()+"/integrations/"+integrationID.String(),
httputil.WithJSONBody(data),
)
}
// Sync an integration. Requires the MANAGE_GUILD permission.
func (c *Client) SyncIntegration(guildID discord.GuildID, integrationID discord.IntegrationID) error {
return c.FastRequest(
"POST",
EndpointGuilds+guildID.String()+"/integrations/"+integrationID.String()+"/sync",
)
}
// GuildWidgetSettings returns the guild widget object.
//
// Requires the MANAGE_GUILD permission.
func (c *Client) GuildWidgetSettings(
guildID discord.GuildID) (*discord.GuildWidgetSettings, error) {
var ge *discord.GuildWidgetSettings
return ge, c.RequestJSON(&ge, "GET", EndpointGuilds+guildID.String()+"/widget")
}
// ModifyGuildWidgetData is the structure to modify a guild widget object for
// the guild. All attributes may be passed in with JSON and modified.
//
// https://discord.com/developers/docs/resources/guild#guild-widget-object
type ModifyGuildWidgetData struct {
// Enabled specifies whether the widget is enabled.
Enabled option.Bool `json:"enabled,omitempty"`
// ChannelID is the widget channel ID.
ChannelID discord.ChannelID `json:"channel_id,omitempty"`
}
// ModifyGuildWidget modifies a guild widget object for the guild.
//
// Requires the MANAGE_GUILD permission.
func (c *Client) ModifyGuildWidget(
guildID discord.GuildID, data ModifyGuildWidgetData) (*discord.GuildWidgetSettings, error) {
var w *discord.GuildWidgetSettings
return w, c.RequestJSON(
&w, "PATCH",
EndpointGuilds+guildID.String()+"/widget",
httputil.WithJSONBody(data),
)
}
// GuildWidget returns the widget for the guild.
func (c *Client) GuildWidget(guildID discord.GuildID) (*discord.GuildWidget, error) {
var w *discord.GuildWidget
return w, c.RequestJSON(
&w, "GET",
EndpointGuilds+guildID.String()+"/widget.json")
}
// GuildVanityInvite returns the vanity invite for guilds that have that
// feature enabled. Only Code and Uses are filled. Code will be "" if a vanity
// url for the guild is not set.
//
// Requires MANAGE_GUILD.
func (c *Client) GuildVanityInvite(guildID discord.GuildID) (*discord.Invite, error) {
var inv *discord.Invite
return inv, c.RequestJSON(&inv, "GET", EndpointGuilds+guildID.String()+"/vanity-url")
}
// https://discord.com/developers/docs/resources/guild#get-guild-widget-image-widget-style-options
type GuildWidgetImageStyle string
const (
// GuildShield is a shield style widget with Discord icon and guild members
// online count.
//
// Example: https://discordapp.com/api/guilds/81384788765712384/widget.png?style=shield
GuildShield GuildWidgetImageStyle = "shield"
// GuildBanner1 is a large image with guild icon, name and online count.
// "POWERED BY DISCORD" as the footer of the widget.
//
// Example: https://discordapp.com/api/guilds/81384788765712384/widget.png?style=banner1
GuildBanner1 GuildWidgetImageStyle = "banner1"
// GuildBanner2 is a smaller widget style with guild icon, name and online
// count. Split on the right with Discord logo.
//
// Example: https://discordapp.com/api/guilds/81384788765712384/widget.png?style=banner2
GuildBanner2 GuildWidgetImageStyle = "banner2"
// GuildBanner3 is a large image with guild icon, name and online count. In
// the footer, Discord logo on the left and "Chat Now" on the right.
//
// Example: https://discordapp.com/api/guilds/81384788765712384/widget.png?style=banner3
GuildBanner3 GuildWidgetImageStyle = "banner3"
// GuildBanner4 is a large Discord logo at the top of the widget.
// Guild icon, name and online count in the middle portion of the widget
// and a "JOIN MY SERVER" button at the bottom.
//
// Example: https://discordapp.com/api/guilds/81384788765712384/widget.png?style=banner4
GuildBanner4 GuildWidgetImageStyle = "banner4"
)
// GuildWidgetImageURL returns a link to the PNG image widget for the guild.
//
// Requires no permissions or authentication.
func (c *Client) GuildWidgetImageURL(guildID discord.GuildID, img GuildWidgetImageStyle) string {
return EndpointGuilds + guildID.String() + "/widget.png?style=" + string(img)
}
// GuildWidgetImage returns a PNG image widget for the guild. Requires no permissions
// or authentication.
func (c *Client) GuildWidgetImage(guildID discord.GuildID, img GuildWidgetImageStyle) (io.ReadCloser, error) {
r, err := c.Request("GET", c.GuildWidgetImageURL(guildID, img))
if err != nil {
return nil, err
}
return r.GetBody(), nil
}