-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
guild.ts
739 lines (674 loc) · 22 KB
/
guild.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import type { Client } from '../client/mod.ts'
import {
GuildBanPayload,
GuildFeatures,
GuildIntegrationPayload,
GuildPayload,
GuildWidgetPayload,
IntegrationAccountPayload,
IntegrationExpireBehavior,
Verification,
GuildChannels,
GuildPreview,
MessageNotification,
ContentFilter,
GuildModifyOptions,
GuildGetPruneCountPayload,
GuildPruneCountPayload,
GuildBeginPrunePayload,
AuditLog,
AuditLogEvents,
AuditLogEntryPayload,
AuditLogEntry
} from '../types/guild.ts'
import { Base, SnowflakeBase } from './base.ts'
import { CreateGuildRoleOptions, RolesManager } from '../managers/roles.ts'
import { InviteManager } from '../managers/invites.ts'
import {
CreateChannelOptions,
GuildChannelsManager
} from '../managers/guildChannels.ts'
import { MembersManager } from '../managers/members.ts'
import { Role } from './role.ts'
import { GuildEmojisManager } from '../managers/guildEmojis.ts'
import { Member } from './member.ts'
import { User } from './user.ts'
import { Application } from './application.ts'
import {
GUILD_BAN,
GUILD_BANNER,
GUILD_BANS,
GUILD_DISCOVERY_SPLASH,
GUILD_ICON,
GUILD_INTEGRATIONS,
GUILD_PRUNE,
GUILD_SPLASH
} from '../types/endpoint.ts'
import { GuildVoiceStatesManager } from '../managers/guildVoiceStates.ts'
import type { RequestMembersOptions } from '../gateway/mod.ts'
import { GuildPresencesManager } from '../managers/presences.ts'
import type { TemplatePayload } from '../types/template.ts'
import { Template } from './template.ts'
import { DiscordAPIError } from '../rest/mod.ts'
import type { ImageFormats, ImageSize } from '../types/cdn.ts'
import { ImageURL } from './cdn.ts'
import type { GuildApplicationCommandsManager } from '../interactions/applicationCommand.ts'
import { toCamelCase } from '../utils/snakeCase.ts'
import { ThreadsManager } from '../managers/threads.ts'
import { GuildStickersManager } from '../managers/guildStickers.ts'
import type { InteractionsClient } from '../interactions/client.ts'
export class GuildBan extends Base {
guild: Guild
reason?: string
user: User
constructor(client: Client, data: GuildBanPayload, guild: Guild) {
super(client, data)
this.guild = guild
this.reason = data.reason === null ? undefined : data.reason
this.user = new User(client, data.user)
}
}
export class GuildBans extends Base {
guild: Guild
constructor(client: Client, guild: Guild) {
super(client)
this.guild = guild
}
/**
* Gets all bans in the Guild.
*/
async all(): Promise<GuildBan[]> {
const res = await this.client.rest.get(GUILD_BANS(this.guild.id))
if (typeof res !== 'object' || !Array.isArray(res))
throw new Error('Failed to fetch Guild Bans')
const bans = (res as GuildBanPayload[]).map(
(ban) => new GuildBan(this.client, ban, this.guild)
)
return bans
}
/**
* Gets ban details of a User if any.
* @param user User to get ban of, ID or User object.
*/
async get(user: string | User): Promise<GuildBan> {
const res = await this.client.rest.get(
GUILD_BAN(this.guild.id, typeof user === 'string' ? user : user.id)
)
if (typeof res !== 'object') throw new Error('Failed to fetch Guild Ban')
return new GuildBan(this.client, res, this.guild)
}
/**
* Bans a User.
* @param user User to ban, ID or User object.
* @param reason Reason for the Ban.
* @param deleteMessagesDays Delete Old Messages? If yes, how much days.
*/
async add(
user: string | User,
reason?: string,
deleteMessagesDays?: number
): Promise<void> {
const res = await this.client.rest.put(
GUILD_BAN(this.guild.id, typeof user === 'string' ? user : user.id),
{
delete_message_days: deleteMessagesDays
},
undefined,
null,
true,
{ reason }
)
if (res.response.status !== 204) throw new Error('Failed to Add Guild Ban')
}
/**
* Unbans (removes ban from) a User.
* @param user User to unban, ID or User object.
* @param reason Reason for the Unban.
*/
async remove(user: string | User, reason?: string): Promise<boolean> {
await this.client.rest.delete(
GUILD_BAN(this.guild.id, typeof user === 'string' ? user : user.id),
undefined,
undefined,
undefined,
undefined,
{ reason }
)
return true
}
}
export class Guild extends SnowflakeBase {
name?: string
icon?: string
splash?: string
discoverySplash?: string
owner?: boolean
ownerID?: string
permissions?: string
region?: string
afkChannelID?: string
afkTimeout?: number
widgetEnabled?: boolean
widgetChannelID?: string
verificationLevel?: Verification
defaultMessageNotifications?: MessageNotification
explicitContentFilter?: ContentFilter
roles: RolesManager
emojis: GuildEmojisManager
invites: InviteManager
features?: GuildFeatures[]
mfaLevel?: string
applicationID?: string
systemChannelID?: string
systemChannelFlags?: string
rulesChannelID?: string
joinedAt?: string
large?: boolean
unavailable = false
memberCount?: number
voiceStates: GuildVoiceStatesManager
members: MembersManager
channels: GuildChannelsManager
presences: GuildPresencesManager
maxPresences?: number
maxMembers?: number
vanityURLCode?: string
description?: string
banner?: string
premiumTier?: number
premiumSubscriptionCount?: number
preferredLocale?: string
publicUpdatesChannelID?: string
maxVideoChannelUsers?: number
approximateNumberCount?: number
approximatePresenceCount?: number
bans: GuildBans
nsfw?: boolean
commands: GuildApplicationCommandsManager
threads: ThreadsManager
stickers: GuildStickersManager
/** Get Shard ID of Guild on which it is */
get shardID(): number {
return Number((BigInt(this.id) << 22n) % BigInt(this.client.shardCount))
}
constructor(client: Client, data: GuildPayload) {
super(client, data)
this.readFromData(data)
this.bans = new GuildBans(client, this)
this.members = new MembersManager(this.client, this)
this.voiceStates = new GuildVoiceStatesManager(client, this)
this.presences = new GuildPresencesManager(client, this)
this.channels = new GuildChannelsManager(
this.client,
this.client.channels,
this
)
this.threads = new ThreadsManager(client, this.channels)
this.roles = new RolesManager(this.client, this)
this.emojis = new GuildEmojisManager(this.client, this.client.emojis, this)
this.invites = new InviteManager(this.client, this)
this.stickers = new GuildStickersManager(this.client, this)
this.commands =
typeof this.client.interactions === 'object'
? this.client.interactions.commands.for(this)
: (this.client as unknown as InteractionsClient).commands.for(this)
}
readFromData(data: GuildPayload): void {
this.id = data.id ?? this.id
this.unavailable = data.unavailable ?? this.unavailable
if (!this.unavailable) {
this.name = data.name ?? this.name
this.icon = data.icon ?? this.icon
this.splash = data.splash ?? this.splash
this.discoverySplash = data.discovery_splash ?? this.discoverySplash
this.owner = data.owner ?? this.owner
this.ownerID = data.owner_id ?? this.ownerID
this.permissions = data.permissions ?? this.permissions
this.region = data.region ?? this.region
this.afkTimeout = data.afk_timeout ?? this.afkTimeout
this.afkChannelID = data.afk_channel_id ?? this.afkChannelID
this.widgetEnabled = data.widget_enabled ?? this.widgetEnabled
this.widgetChannelID = data.widget_channel_id ?? this.widgetChannelID
this.verificationLevel = data.verification_level ?? this.verificationLevel
this.defaultMessageNotifications =
data.default_message_notifications ?? this.defaultMessageNotifications
this.explicitContentFilter =
data.explicit_content_filter ?? this.explicitContentFilter
this.features = data.features ?? this.features
this.mfaLevel = data.mfa_level ?? this.mfaLevel
this.systemChannelID = data.system_channel_id ?? this.systemChannelID
this.systemChannelFlags =
data.system_channel_flags ?? this.systemChannelFlags
this.rulesChannelID = data.rules_channel_id ?? this.rulesChannelID
this.joinedAt = data.joined_at ?? this.joinedAt
this.large = data.large ?? this.large
this.memberCount = data.member_count ?? this.memberCount
this.maxPresences = data.max_presences ?? this.maxPresences
this.maxMembers = data.max_members ?? this.maxMembers
this.vanityURLCode = data.vanity_url_code ?? this.vanityURLCode
this.description = data.description ?? this.description
this.banner = data.banner ?? this.banner
this.premiumTier = data.premium_tier ?? this.premiumTier
this.premiumSubscriptionCount =
data.premium_subscription_count ?? this.premiumSubscriptionCount
this.preferredLocale = data.preferred_locale ?? this.preferredLocale
this.publicUpdatesChannelID =
data.public_updates_channel_id ?? this.publicUpdatesChannelID
this.maxVideoChannelUsers =
data.max_video_channel_users ?? this.maxVideoChannelUsers
this.approximateNumberCount =
data.approximate_number_count ?? this.approximateNumberCount
this.approximatePresenceCount =
data.approximate_presence_count ?? this.approximatePresenceCount
this.nsfw = data.nsfw ?? this.nsfw ?? false
}
}
/**
* Gets guild icon URL
*/
iconURL(
format: ImageFormats = 'png',
size: ImageSize = 512
): string | undefined {
return this.icon != null
? `${ImageURL(GUILD_ICON(this.id, this.icon), format, size)}`
: undefined
}
/**
* Gets guild splash URL
*/
splashURL(
format: ImageFormats = 'png',
size: ImageSize = 512
): string | undefined {
return this.splash != null
? `${ImageURL(GUILD_SPLASH(this.id, this.splash), format, size)}`
: undefined
}
/**
* Gets guild discover splash URL
*/
discoverSplashURL(
format: ImageFormats = 'png',
size: ImageSize = 512
): string | undefined {
return this.discoverySplash != null
? `${ImageURL(
GUILD_DISCOVERY_SPLASH(this.id, this.discoverySplash),
format,
size
)}`
: undefined
}
/**
* Gets guild banner URL
*/
bannerURL(
format: ImageFormats = 'png',
size: ImageSize = 512
): string | undefined {
return this.banner != null
? `${ImageURL(GUILD_BANNER(this.id, this.banner), format, size)}`
: undefined
}
/**
* Gets Everyone role of the Guild
*/
async getEveryoneRole(): Promise<Role> {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return (await this.roles.get(this.id)) as Role
}
/**
* Gets current client's member in the Guild
*/
async me(): Promise<Member> {
const get = await this.members.get(this.client.user?.id as string)
if (get === undefined) throw new Error('Guild#me is not cached')
return get
}
/**
* Fetches Guild's Integrations (Webhooks, Bots, etc.)
*/
async fetchIntegrations(): Promise<GuildIntegration[]> {
const raw = (await this.client.rest.get(
GUILD_INTEGRATIONS(this.id)
)) as GuildIntegrationPayload[]
return raw.map((e) => new GuildIntegration(this.client, e))
}
/** Create a new Guild Channel */
async createChannel(options: CreateChannelOptions): Promise<GuildChannels> {
return this.channels.create(options)
}
/** Create a new Guild Role */
async createRole(options?: CreateGuildRoleOptions): Promise<Role> {
return this.roles.create(options)
}
/**
* Chunks the Guild Members, i.e. cache them.
* @param options Options regarding the Members Request
* @param wait Whether to wait for all Members to come before resolving Promise or not.
* @param timeout Configurable timeout to cancel the wait to safely remove listener.
*/
async chunk(
options: RequestMembersOptions,
wait: boolean = false,
timeout: number = 60000
): Promise<Guild> {
return await new Promise((resolve, reject) => {
this.client.shards.get(this.shardID)?.requestMembers(this.id, options)
if (!wait) return resolve(this)
else {
let chunked = false
const listener = (guild: Guild): void => {
if (guild.id === this.id) {
chunked = true
this.client.off('guildMembersChunked', listener)
resolve(this)
}
}
this.client.on('guildMembersChunked', listener)
setTimeout(() => {
if (!chunked) {
this.client.off('guildMembersChunked', listener)
}
}, timeout)
}
})
}
/**
* Fulfills promise when guild becomes available
* @param timeout Configurable timeout to cancel the wait to safely remove listener.
*/
async awaitAvailability(timeout: number = 1000): Promise<Guild> {
return await new Promise((resolve, reject) => {
if (!this.unavailable) resolve(this)
const listener = (guild: Guild): void => {
if (guild.id === this.id) {
this.client.off('guildLoaded', listener)
resolve(this)
}
}
this.client.on('guildLoaded', listener)
setTimeout(() => {
this.client.off('guildLoaded', listener)
reject(Error("Timeout. Guild didn't arrive in time."))
}, timeout)
})
}
/** Attach an integration object from the current user to the guild. */
async createIntegration(id: string, type: string): Promise<Guild> {
await this.client.rest.api.guilds[this.id].integrations.post({ id, type })
return this
}
/** Modify the behavior and settings of an integration object for the guild. */
async editIntegration(
id: string,
data: {
expireBehavior?: number | null
expireGracePeriod?: number | null
enableEmoticons?: boolean | null
}
): Promise<Guild> {
await this.client.rest.api.guilds[this.id].integrations[id].patch({
expire_behaviour: data.expireBehavior,
expire_grace_period: data.expireGracePeriod,
enable_emoticons: data.enableEmoticons
})
return this
}
/** Delete the attached integration object for the guild. Deletes any associated webhooks and kicks the associated bot if there is one. */
async deleteIntegration(id: string): Promise<Guild> {
await this.client.rest.api.guilds[this.id].integrations[id].delete()
return this
}
/** Sync an integration. */
async syncIntegration(id: string): Promise<Guild> {
await this.client.rest.api.guilds[this.id].integrations[id].sync.post()
return this
}
/** Returns the widget for the guild. */
async getWidget(): Promise<GuildWidgetPayload> {
return this.client.rest.api.guilds[this.id]['widget.json'].get()
}
/** Modify a guild widget object for the guild. */
async editWidget(data: {
enabled?: boolean
channel?: string | GuildChannels
}): Promise<Guild> {
await this.client.rest.api.guilds[this.id].widget.patch({
enabled: data.enabled,
channel_id:
typeof data.channel === 'object' ? data.channel.id : data.channel
})
return this
}
/** Returns a partial invite object for guilds with that feature enabled. */
async getVanity(): Promise<{ code: string | null; uses: number }> {
try {
const value = await this.client.rest.api.guilds[this.id][
'vanity-url'
].get()
return value
} catch (error) {
if (error instanceof DiscordAPIError) {
if (error.error?.code === 50020) {
return {
code: null,
uses: 0
}
}
}
throw error
}
}
/** Returns a PNG (URL) image widget for the guild. */
getWidgetImageURL(
style?: 'shield' | 'banner1' | 'banner2' | 'banner3' | 'banner4'
): string {
return `https://discord.com/api/v${this.client.rest.version ?? 8}/guilds/${
this.id
}/widget.png${style !== undefined ? `?style=${style}` : ''}`
}
/** Leave a Guild. */
async leave(): Promise<Client> {
await this.client.rest.api.users['@me'].guilds[this.id].delete()
return this.client
}
/** Returns an array of template objects. */
async getTemplates(): Promise<Template[]> {
return this.client.rest.api.guilds[this.id].templates
.get()
.then((temps: TemplatePayload[]) =>
temps.map((temp) => new Template(this.client, temp))
)
}
/** Creates a template for the guild. */
async createTemplate(
name: string,
description?: string | null
): Promise<Template> {
const payload = await this.client.rest.api.guilds[this.id].templates.post({
name,
description
})
return new Template(this.client, payload)
}
/** Syncs the template to the guild's current state. */
async syncTemplate(code: string): Promise<Template> {
const payload = await this.client.rest.api.guilds[this.id].templates[
code
].put()
return new Template(this.client, payload)
}
/** Modifies the template's metadata. */
async editTemplate(
code: string,
data: { name?: string; description?: string }
): Promise<Template> {
const payload = await this.client.rest.api.guilds[this.id].templates[
code
].patch({ name: data.name, description: data.description })
return new Template(this.client, payload)
}
/** Deletes the template. Requires the MANAGE_GUILD permission. */
async deleteTemplate(code: string): Promise<Guild> {
await this.client.rest.api.guilds[this.id].templates[code].delete()
return this
}
/** Gets a preview of the guild. Returns GuildPreview. */
async preview(): Promise<GuildPreview> {
return this.client.guilds.preview(this.id)
}
/**
* Edits the guild.
* @param options Guild edit options
*/
async edit(options: GuildModifyOptions): Promise<Guild> {
const result = await this.client.guilds.edit(this.id, options, true)
this.readFromData(result)
return new Guild(this.client, result)
}
/** Deletes the guild. */
async delete(): Promise<Guild> {
const result = await this.client.guilds.delete(this.id)
return result === undefined ? this : result
}
async getPruneCount(options?: {
days?: number
includeRoles?: Array<Role | string>
}): Promise<number> {
const query: GuildGetPruneCountPayload = {
days: options?.days,
include_roles: options?.includeRoles
?.map((role) => (role instanceof Role ? role.id : role))
.join(',')
}
const result: GuildPruneCountPayload = await this.client.rest.get(
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
GUILD_PRUNE(this.id) +
'?' +
Object.entries(query)
.map(([key, value]) => `${key}=${value}`)
.join('&')
)
return result.pruned as number
}
async prune(options?: {
days?: number
computePruneCount?: true
includeRoles?: Array<Role | string>
}): Promise<number>
async prune(options?: {
days?: number
computePruneCount: false
includeRoles?: Array<Role | string>
}): Promise<null>
async prune(options?: {
days?: number
computePruneCount?: boolean
includeRoles?: Array<Role | string>
}): Promise<number | null> {
const body: GuildBeginPrunePayload = {
days: options?.days,
compute_prune_count: options?.computePruneCount,
include_roles: options?.includeRoles?.map((role) =>
role instanceof Role ? role.id : role
)
}
const result: GuildPruneCountPayload = await this.client.rest.post(
GUILD_PRUNE(this.id),
body
)
return result.pruned
}
async fetchAuditLog(
options: {
user?: string | User
actionType?: AuditLogEvents
before?: string
limit?: number
} = {}
): Promise<AuditLog> {
if (
typeof options.limit === 'number' &&
(options.limit < 1 || options.limit > 100)
)
throw new Error('Invalid limit, must be between 1-100')
const data = await this.client.rest.endpoints.getGuildAuditLog(this.id, {
userId: typeof options.user === 'object' ? options.user.id : options.user,
actionType: options.actionType,
before: options.before,
limit: options.limit ?? 50
})
const ret: AuditLog = {
webhooks: [],
users: [],
entries: [],
integrations: []
}
if ('audit_log_entries' in data) {
ret.entries = data.audit_log_entries.map(transformAuditLogEntryPayload)
}
if ('users' in data) {
const users: User[] = []
for (const d of data.users) {
await this.client.users.set(d.id, d)
users.push((await this.client.users.get(d.id))!)
}
ret.users = users
}
if ('integrations' in data) {
ret.integrations = data.integrations.map(
(e) => new GuildIntegration(this.client, e)
)
}
if ('webhooks' in data) {
ret.webhooks = data.webhooks
}
return ret
}
}
export class GuildIntegration extends Base {
id: string
name: string
type: string
enabled: boolean
syncing?: boolean
roleID?: string
enableEmoticons?: boolean
expireBehaviour?: IntegrationExpireBehavior
expireGracePeriod?: number
user?: User
account: IntegrationAccountPayload
syncedAt?: string // Actually a ISO Timestamp, but we parse in constructor
subscriberCount?: number
revoked?: boolean
application?: Application
constructor(client: Client, data: GuildIntegrationPayload) {
super(client, data)
this.id = data.id
this.name = data.name
this.type = data.type
this.enabled = data.enabled
this.syncing = data.syncing
this.roleID = data.role_id
this.enableEmoticons = data.enable_emoticons
this.expireBehaviour = data.expire_behaviour
this.expireGracePeriod = data.expire_grace_period
this.user =
data.user !== undefined ? new User(client, data.user) : undefined
this.account = data.account
this.syncedAt = data.synced_at
this.subscriberCount = data.subscriber_count
this.revoked = data.revoked
this.application =
data.application !== undefined
? new Application(client, data.application)
: undefined
}
}
export function transformAuditLogEntryPayload(
d: AuditLogEntryPayload
): AuditLogEntry {
return toCamelCase(d)
}