2020-12-02 12:29:52 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import {
|
|
|
|
GuildBanPayload,
|
|
|
|
GuildFeatures,
|
|
|
|
GuildIntegrationPayload,
|
|
|
|
GuildPayload,
|
2021-01-21 12:39:51 +00:00
|
|
|
GuildWidgetPayload,
|
2020-12-02 12:29:52 +00:00
|
|
|
IntegrationAccountPayload,
|
2021-01-15 16:15:52 +00:00
|
|
|
IntegrationExpireBehavior,
|
|
|
|
Verification,
|
2021-01-15 16:48:08 +00:00
|
|
|
GuildChannels,
|
2021-01-15 17:15:24 +00:00
|
|
|
GuildPreview,
|
|
|
|
MessageNotification,
|
|
|
|
ContentFilter,
|
2021-01-21 05:24:41 +00:00
|
|
|
GuildModifyOptions,
|
|
|
|
GuildGetPruneCountPayload,
|
2021-01-21 05:37:10 +00:00
|
|
|
GuildPruneCountPayload,
|
|
|
|
GuildBeginPrunePayload
|
2020-12-02 12:29:52 +00:00
|
|
|
} from '../types/guild.ts'
|
|
|
|
import { Base } from './base.ts'
|
2021-01-01 05:55:23 +00:00
|
|
|
import { CreateGuildRoleOptions, RolesManager } from '../managers/roles.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
import { InviteManager } from '../managers/invites.ts'
|
2021-01-01 05:55:23 +00:00
|
|
|
import {
|
|
|
|
CreateChannelOptions,
|
|
|
|
GuildChannelsManager
|
|
|
|
} from '../managers/guildChannels.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
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'
|
2021-01-21 05:24:41 +00:00
|
|
|
import {
|
|
|
|
GUILD_BAN,
|
|
|
|
GUILD_BANS,
|
|
|
|
GUILD_INTEGRATIONS,
|
|
|
|
GUILD_PRUNE
|
|
|
|
} from '../types/endpoint.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
import { GuildVoiceStatesManager } from '../managers/guildVoiceStates.ts'
|
|
|
|
import { RequestMembersOptions } from '../gateway/index.ts'
|
|
|
|
import { GuildPresencesManager } from '../managers/presences.ts'
|
2021-01-21 12:39:51 +00:00
|
|
|
import { TemplatePayload } from '../types/template.ts'
|
|
|
|
import { Template } from './template.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
client: Client
|
|
|
|
guild: Guild
|
|
|
|
|
|
|
|
constructor(client: Client, guild: Guild) {
|
|
|
|
this.client = client
|
|
|
|
this.guild = guild
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Gets all bans in the Guild.
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Gets ban details of a User if any.
|
2020-12-02 12:29:52 +00:00
|
|
|
* @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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Bans a User.
|
2020-12-02 12:29:52 +00:00
|
|
|
* @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),
|
|
|
|
{
|
|
|
|
reason,
|
|
|
|
delete_message_days: deleteMessagesDays
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
)
|
2020-12-04 02:33:15 +00:00
|
|
|
if (res.response.status !== 204) throw new Error('Failed to Add Guild Ban')
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Unbans (removes ban from) a User.
|
2020-12-02 12:29:52 +00:00
|
|
|
* @param user User to unban, ID or User object.
|
|
|
|
*/
|
|
|
|
async remove(user: string | User): Promise<boolean> {
|
|
|
|
const res = await this.client.rest.delete(
|
|
|
|
GUILD_BAN(this.guild.id, typeof user === 'string' ? user : user.id),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
|
2020-12-04 02:33:15 +00:00
|
|
|
if (res.response.status !== 204) return false
|
2020-12-02 12:29:52 +00:00
|
|
|
else return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Guild extends Base {
|
|
|
|
id: string
|
|
|
|
name?: string
|
|
|
|
icon?: string
|
|
|
|
iconHash?: string
|
|
|
|
splash?: string
|
|
|
|
discoverySplash?: string
|
|
|
|
owner?: boolean
|
|
|
|
ownerID?: string
|
|
|
|
permissions?: string
|
|
|
|
region?: string
|
|
|
|
afkChannelID?: string
|
|
|
|
afkTimeout?: number
|
|
|
|
widgetEnabled?: boolean
|
|
|
|
widgetChannelID?: string
|
2021-01-15 16:15:52 +00:00
|
|
|
verificationLevel?: Verification
|
2021-01-15 17:15:24 +00:00
|
|
|
defaultMessageNotifications?: MessageNotification
|
|
|
|
explicitContentFilter?: ContentFilter
|
2020-12-02 12:29:52 +00:00
|
|
|
roles: RolesManager
|
|
|
|
emojis: GuildEmojisManager
|
|
|
|
invites: InviteManager
|
|
|
|
features?: GuildFeatures[]
|
|
|
|
mfaLevel?: string
|
|
|
|
applicationID?: string
|
|
|
|
systemChannelID?: string
|
|
|
|
systemChannelFlags?: string
|
|
|
|
rulesChannelID?: string
|
|
|
|
joinedAt?: string
|
|
|
|
large?: boolean
|
|
|
|
unavailable: boolean
|
|
|
|
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
|
|
|
|
|
|
|
|
constructor(client: Client, data: GuildPayload) {
|
|
|
|
super(client, data)
|
2021-01-01 16:09:41 +00:00
|
|
|
this.id = data.id
|
|
|
|
this.unavailable = data.unavailable
|
2021-01-01 15:27:30 +00:00
|
|
|
this.readFromData(data)
|
2020-12-02 12:29:52 +00:00
|
|
|
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.roles = new RolesManager(this.client, this)
|
|
|
|
this.emojis = new GuildEmojisManager(this.client, this.client.emojis, this)
|
|
|
|
this.invites = new InviteManager(this.client, this)
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:28:20 +00:00
|
|
|
readFromData(data: GuildPayload): void {
|
2020-12-02 12:29:52 +00:00
|
|
|
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.iconHash = data.icon_hash ?? this.iconHash
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Gets Everyone role of the Guild
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
async getEveryoneRole(): Promise<Role> {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
|
|
return (await this.roles.get(this.id)) as Role
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Gets current client's member in the Guild
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-05 02:39:37 +00:00
|
|
|
* Fetches Guild's Integrations (Webhooks, Bots, etc.)
|
2020-12-02 12:29:52 +00:00
|
|
|
*/
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2021-01-01 05:55:23 +00:00
|
|
|
/** Create a new Guild Channel */
|
2021-01-15 16:15:52 +00:00
|
|
|
async createChannel(options: CreateChannelOptions): Promise<GuildChannels> {
|
2021-01-01 05:55:23 +00:00
|
|
|
return this.channels.create(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a new Guild Role */
|
|
|
|
async createRole(options?: CreateGuildRoleOptions): Promise<Role> {
|
|
|
|
return this.roles.create(options)
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
/**
|
2020-12-05 02:24:08 +00:00
|
|
|
* Chunks the Guild Members, i.e. cache them.
|
2020-12-02 12:29:52 +00:00
|
|
|
* @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.gateway?.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
|
2021-01-20 10:05:15 +00:00
|
|
|
this.client.off('guildMembersChunked', listener)
|
2020-12-02 12:29:52 +00:00
|
|
|
resolve(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.client.on('guildMembersChunked', listener)
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!chunked) {
|
2021-01-20 10:05:15 +00:00
|
|
|
this.client.off('guildMembersChunked', listener)
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
|
|
|
}, timeout)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-01-01 15:54:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fulfills promise when guild becomes available
|
2021-01-07 10:08:37 +00:00
|
|
|
* @param timeout Configurable timeout to cancel the wait to safely remove listener.
|
2021-01-01 15:54:33 +00:00
|
|
|
*/
|
2021-01-07 10:08:37 +00:00
|
|
|
async awaitAvailability(timeout: number = 1000): Promise<Guild> {
|
2021-01-05 13:42:27 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
2021-01-20 10:05:15 +00:00
|
|
|
if (!this.unavailable) resolve(this)
|
2021-01-05 13:53:00 +00:00
|
|
|
const listener = (guild: Guild): void => {
|
2021-01-05 13:42:27 +00:00
|
|
|
if (guild.id === this.id) {
|
2021-01-20 10:05:15 +00:00
|
|
|
this.client.off('guildLoaded', listener)
|
|
|
|
resolve(this)
|
2021-01-05 13:42:27 +00:00
|
|
|
}
|
2021-01-20 10:05:15 +00:00
|
|
|
}
|
|
|
|
this.client.on('guildLoaded', listener)
|
2021-01-07 10:08:37 +00:00
|
|
|
setTimeout(() => {
|
2021-01-20 10:05:15 +00:00
|
|
|
this.client.off('guildLoaded', listener)
|
|
|
|
reject(Error("Timeout. Guild didn't arrive in time."))
|
|
|
|
}, timeout)
|
|
|
|
})
|
2021-01-01 15:54:33 +00:00
|
|
|
}
|
2021-01-21 12:39:51 +00:00
|
|
|
|
|
|
|
/** 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 | GuildChannel
|
|
|
|
}): 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 }> {
|
|
|
|
return this.client.rest.api.guilds[this.id]['vanity-url'].get()
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
].sync.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
|
2021-01-21 13:13:47 +00:00
|
|
|
|
2021-01-18 11:56:51 +00:00
|
|
|
/** Gets a preview of the guild. Returns GuildPreview. */
|
2021-01-15 16:48:08 +00:00
|
|
|
async preview(): Promise<GuildPreview> {
|
|
|
|
return this.client.guilds.preview(this.id)
|
|
|
|
}
|
2021-01-15 17:15:24 +00:00
|
|
|
|
2021-01-18 11:56:51 +00:00
|
|
|
/**
|
|
|
|
* Edits the guild.
|
|
|
|
* @param options Guild edit options
|
|
|
|
*/
|
2021-01-15 17:15:24 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-01-16 14:22:13 +00:00
|
|
|
|
2021-01-18 11:56:51 +00:00
|
|
|
/** Deletes the guild. */
|
2021-01-16 14:22:13 +00:00
|
|
|
async delete(): Promise<Guild> {
|
|
|
|
const result = await this.client.guilds.delete(this.id)
|
|
|
|
|
|
|
|
return result === undefined ? this : result
|
|
|
|
}
|
2021-01-21 05:24:41 +00:00
|
|
|
|
2021-01-21 06:16:45 +00:00
|
|
|
async getPruneCount(options?: {
|
|
|
|
days?: number
|
2021-01-21 05:37:10 +00:00
|
|
|
includeRoles?: Array<Role | string>
|
2021-01-21 06:16:45 +00:00
|
|
|
}): Promise<number> {
|
2021-01-21 05:24:41 +00:00
|
|
|
const query: GuildGetPruneCountPayload = {
|
2021-01-21 06:16:45 +00:00
|
|
|
days: options?.days,
|
|
|
|
include_roles: options?.includeRoles
|
2021-01-21 05:37:10 +00:00
|
|
|
?.map((role) => (role instanceof Role ? role.id : role))
|
|
|
|
.join(',')
|
2021-01-21 05:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-01-21 05:37:10 +00:00
|
|
|
|
2021-01-21 06:16:45 +00:00
|
|
|
async prune(options?: {
|
|
|
|
days?: number
|
|
|
|
computePruneCount: true | undefined
|
2021-01-21 05:37:10 +00:00
|
|
|
includeRoles?: Array<Role | string>
|
2021-01-21 06:16:45 +00:00
|
|
|
}): Promise<number>
|
|
|
|
async prune(options?: {
|
|
|
|
days?: number
|
|
|
|
computePruneCount: false
|
2021-01-21 05:37:10 +00:00
|
|
|
includeRoles?: Array<Role | string>
|
2021-01-21 06:16:45 +00:00
|
|
|
}): Promise<null>
|
|
|
|
async prune(options?: {
|
|
|
|
days?: number
|
|
|
|
computePruneCount?: boolean | undefined
|
2021-01-21 05:37:10 +00:00
|
|
|
includeRoles?: Array<Role | string>
|
2021-01-21 06:16:45 +00:00
|
|
|
}): Promise<number | null> {
|
2021-01-21 05:37:10 +00:00
|
|
|
const body: GuildBeginPrunePayload = {
|
2021-01-21 06:16:45 +00:00
|
|
|
days: options?.days,
|
|
|
|
compute_prune_count: options?.computePruneCount,
|
|
|
|
include_roles: options?.includeRoles?.map((role) =>
|
2021-01-21 05:37:10 +00:00
|
|
|
role instanceof Role ? role.id : role
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: GuildPruneCountPayload = await this.client.rest.post(
|
|
|
|
GUILD_PRUNE(this.id),
|
|
|
|
body
|
|
|
|
)
|
|
|
|
|
|
|
|
return result.pruned
|
2021-01-21 12:39:51 +00:00
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-12-05 02:24:08 +00:00
|
|
|
syncedAt?: string // Actually a ISO Timestamp, but we parse in constructor
|
2020-12-02 12:29:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|