2021-01-21 12:39:51 +00:00
|
|
|
import { fetchAuto } from '../../deps.ts'
|
2020-11-08 07:58:24 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import { Guild } from '../structures/guild.ts'
|
2021-01-21 12:39:51 +00:00
|
|
|
import { Template } from '../structures/template.ts'
|
2021-01-15 16:15:52 +00:00
|
|
|
import { Role } from '../structures/role.ts'
|
2021-01-15 16:45:08 +00:00
|
|
|
import { GUILD, GUILDS, GUILD_PREVIEW } from '../types/endpoint.ts'
|
2021-01-15 16:15:52 +00:00
|
|
|
import {
|
|
|
|
GuildPayload,
|
|
|
|
MemberPayload,
|
|
|
|
GuildCreateRolePayload,
|
|
|
|
GuildCreatePayload,
|
2021-01-15 16:45:08 +00:00
|
|
|
GuildCreateChannelPayload,
|
|
|
|
GuildPreview,
|
2021-01-15 17:15:24 +00:00
|
|
|
GuildPreviewPayload,
|
|
|
|
GuildModifyOptions,
|
2021-01-20 16:28:18 +00:00
|
|
|
GuildModifyPayload,
|
|
|
|
GuildCreateOptions
|
2021-01-15 16:15:52 +00:00
|
|
|
} from '../types/guild.ts'
|
2020-11-08 07:58:24 +00:00
|
|
|
import { BaseManager } from './base.ts'
|
|
|
|
import { MembersManager } from './members.ts'
|
2021-01-15 16:45:08 +00:00
|
|
|
import { Emoji } from '../structures/emoji.ts'
|
2020-10-31 11:45:33 +00:00
|
|
|
|
|
|
|
export class GuildManager extends BaseManager<GuildPayload, Guild> {
|
2020-12-02 12:29:52 +00:00
|
|
|
constructor(client: Client) {
|
2020-11-02 06:58:23 +00:00
|
|
|
super(client, 'guilds', Guild)
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 12:29:52 +00:00
|
|
|
async fetch(id: string): Promise<Guild> {
|
2020-11-03 09:21:29 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
2020-11-08 07:58:24 +00:00
|
|
|
this.client.rest
|
|
|
|
.get(GUILD(id))
|
|
|
|
.then(async (data: any) => {
|
2021-02-06 14:01:28 +00:00
|
|
|
await this.set(id, data)
|
2020-11-08 07:58:24 +00:00
|
|
|
|
|
|
|
const guild = new Guild(this.client, data)
|
|
|
|
|
|
|
|
if ((data as GuildPayload).members !== undefined) {
|
|
|
|
const members = new MembersManager(this.client, guild)
|
|
|
|
await members.fromPayload(
|
|
|
|
(data as GuildPayload).members as MemberPayload[]
|
|
|
|
)
|
|
|
|
guild.members = members
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(guild)
|
|
|
|
})
|
2020-12-02 12:29:52 +00:00
|
|
|
.catch((e) => reject(e))
|
2020-10-31 11:45:33 +00:00
|
|
|
})
|
|
|
|
}
|
2021-01-21 12:39:51 +00:00
|
|
|
|
|
|
|
/** Create a new guild based on a template. */
|
|
|
|
async createFromTemplate(
|
|
|
|
template: Template | string,
|
|
|
|
name: string,
|
|
|
|
icon?: string
|
|
|
|
): Promise<Guild> {
|
|
|
|
if (icon?.startsWith('http') === true) icon = await fetchAuto(icon)
|
|
|
|
const guild = await this.client.rest.api.guilds.templates[
|
|
|
|
typeof template === 'object' ? template.code : template
|
|
|
|
].post({ name, icon })
|
|
|
|
return new Guild(this.client, guild)
|
2021-01-21 13:17:03 +00:00
|
|
|
}
|
2021-01-21 13:13:47 +00:00
|
|
|
|
2021-01-18 11:56:51 +00:00
|
|
|
/**
|
|
|
|
* Creates a guild. Returns Guild. Fires guildCreate event.
|
|
|
|
* @param options Options for creating a guild
|
|
|
|
*/
|
2021-01-15 16:15:52 +00:00
|
|
|
async create(options: GuildCreateOptions): Promise<Guild> {
|
2021-01-20 16:28:18 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
2021-01-15 16:15:52 +00:00
|
|
|
if (options.icon !== undefined && !options.icon.startsWith('data:')) {
|
|
|
|
options.icon = await fetchAuto(options.icon)
|
|
|
|
}
|
2021-01-24 18:49:08 +00:00
|
|
|
if (options.roles !== undefined && options.roles[0].name !== '@everyone') {
|
|
|
|
options.roles.unshift({
|
|
|
|
id: Math.floor(Math.random() * 18392375458).toString(),
|
|
|
|
name: '@everyone'
|
|
|
|
})
|
|
|
|
}
|
2021-01-15 16:15:52 +00:00
|
|
|
|
|
|
|
const body: GuildCreatePayload = {
|
|
|
|
name: options.name,
|
|
|
|
region: options.region,
|
|
|
|
icon: options.icon,
|
|
|
|
verification_level: options.verificationLevel,
|
2021-01-24 18:49:08 +00:00
|
|
|
roles: options.roles?.map((obj) => {
|
|
|
|
let result: GuildCreateRolePayload
|
|
|
|
if (obj instanceof Role) {
|
|
|
|
result = {
|
|
|
|
id: obj.id,
|
|
|
|
name: obj.name,
|
|
|
|
color: obj.color,
|
|
|
|
hoist: obj.hoist,
|
|
|
|
position: obj.position,
|
|
|
|
permissions: obj.permissions.bitfield.toString(),
|
|
|
|
managed: obj.managed,
|
|
|
|
mentionable: obj.mentionable
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = obj
|
|
|
|
}
|
2021-01-15 16:15:52 +00:00
|
|
|
|
2021-01-24 18:49:08 +00:00
|
|
|
return result
|
|
|
|
}),
|
|
|
|
channels: options.channels?.map(
|
|
|
|
(obj): GuildCreateChannelPayload => ({
|
|
|
|
id: obj.id,
|
|
|
|
name: obj.name,
|
|
|
|
type: obj.type,
|
|
|
|
parent_id: obj.parentID
|
|
|
|
})
|
|
|
|
),
|
2021-01-15 16:15:52 +00:00
|
|
|
afk_channel_id: options.afkChannelID,
|
|
|
|
afk_timeout: options.afkTimeout,
|
|
|
|
system_channel_id: options.systemChannelID
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: GuildPayload = await this.client.rest.post(GUILDS(), body)
|
|
|
|
const guild = new Guild(this.client, result)
|
|
|
|
|
|
|
|
return guild
|
|
|
|
}
|
2021-01-15 16:45:08 +00:00
|
|
|
|
2021-01-18 11:56:51 +00:00
|
|
|
/**
|
|
|
|
* Gets a preview of a guild. Returns GuildPreview.
|
|
|
|
* @param guildID Guild id
|
|
|
|
*/
|
2021-01-15 16:45:08 +00:00
|
|
|
async preview(guildID: string): Promise<GuildPreview> {
|
|
|
|
const resp: GuildPreviewPayload = await this.client.rest.get(
|
|
|
|
GUILD_PREVIEW(guildID)
|
|
|
|
)
|
|
|
|
|
|
|
|
const result: GuildPreview = {
|
|
|
|
id: resp.id,
|
|
|
|
name: resp.name,
|
|
|
|
icon: resp.icon,
|
|
|
|
splash: resp.splash,
|
|
|
|
discoverySplash: resp.discovery_splash,
|
|
|
|
emojis: resp.emojis.map((emoji) => new Emoji(this.client, emoji)),
|
|
|
|
features: resp.features,
|
|
|
|
approximateMemberCount: resp.approximate_member_count,
|
|
|
|
approximatePresenceCount: resp.approximate_presence_count,
|
|
|
|
description: resp.description
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2021-01-15 17:15:24 +00:00
|
|
|
|
2021-02-06 14:01:28 +00:00
|
|
|
/** Sets a value to Cache */
|
|
|
|
async set(key: string, value: GuildPayload): Promise<any> {
|
|
|
|
if ('roles' in value) value.roles = []
|
|
|
|
if ('emojis' in value) value.emojis = []
|
|
|
|
if ('members' in value) value.members = []
|
|
|
|
if ('presences' in value) value.presences = []
|
|
|
|
if ('voice_states' in value) value.voice_states = []
|
|
|
|
return this.client.cache.set(this.cacheName, key, value)
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:56:51 +00:00
|
|
|
/**
|
2021-01-20 16:28:18 +00:00
|
|
|
* Edits a guild. Returns edited guild.
|
2021-01-18 11:56:51 +00:00
|
|
|
* @param guild Guild or guild id
|
|
|
|
* @param options Guild edit options
|
|
|
|
* @param asRaw true for get raw data, false for get guild(defaults to false)
|
|
|
|
*/
|
2021-01-15 17:15:24 +00:00
|
|
|
async edit(
|
2021-01-16 14:22:13 +00:00
|
|
|
guild: Guild | string,
|
2021-01-15 17:15:24 +00:00
|
|
|
options: GuildModifyOptions,
|
|
|
|
asRaw: false
|
|
|
|
): Promise<Guild>
|
|
|
|
async edit(
|
2021-01-16 14:22:13 +00:00
|
|
|
guild: Guild | string,
|
2021-01-15 17:15:24 +00:00
|
|
|
options: GuildModifyOptions,
|
|
|
|
asRaw: true
|
|
|
|
): Promise<GuildPayload>
|
|
|
|
async edit(
|
2021-01-16 14:22:13 +00:00
|
|
|
guild: Guild | string,
|
2021-01-15 17:15:24 +00:00
|
|
|
options: GuildModifyOptions,
|
|
|
|
asRaw: boolean = false
|
|
|
|
): Promise<Guild | GuildPayload> {
|
|
|
|
if (
|
|
|
|
options.icon !== undefined &&
|
|
|
|
options.icon !== null &&
|
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
|
|
!options.icon.startsWith('data:')
|
|
|
|
) {
|
|
|
|
options.icon = await fetchAuto(options.icon)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
options.splash !== undefined &&
|
|
|
|
options.splash !== null &&
|
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
|
|
!options.splash.startsWith('data:')
|
|
|
|
) {
|
|
|
|
options.splash = await fetchAuto(options.splash)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
options.banner !== undefined &&
|
|
|
|
options.banner !== null &&
|
|
|
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
|
|
!options.banner.startsWith('data:')
|
|
|
|
) {
|
|
|
|
options.banner = await fetchAuto(options.banner)
|
|
|
|
}
|
2021-01-16 14:22:13 +00:00
|
|
|
if (guild instanceof Guild) {
|
|
|
|
guild = guild.id
|
|
|
|
}
|
2021-01-15 17:15:24 +00:00
|
|
|
|
|
|
|
const body: GuildModifyPayload = {
|
|
|
|
name: options.name,
|
|
|
|
region: options.region,
|
|
|
|
verification_level: options.verificationLevel,
|
|
|
|
default_message_notifications: options.defaultMessageNotifications,
|
|
|
|
explicit_content_filter: options.explicitContentFilter,
|
|
|
|
afk_channel_id: options.afkChannelID,
|
|
|
|
afk_timeout: options.afkTimeout,
|
2021-01-16 14:22:13 +00:00
|
|
|
owner_id: options.ownerID,
|
2021-01-15 17:15:24 +00:00
|
|
|
icon: options.icon,
|
|
|
|
splash: options.splash,
|
|
|
|
banner: options.banner,
|
|
|
|
system_channel_id: options.systemChannelID,
|
|
|
|
rules_channel_id: options.rulesChannelID,
|
|
|
|
public_updates_channel_id: options.publicUpdatesChannelID,
|
|
|
|
preferred_locale: options.preferredLocale
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: GuildPayload = await this.client.rest.patch(
|
2021-01-16 14:22:13 +00:00
|
|
|
GUILD(guild),
|
2021-01-15 17:15:24 +00:00
|
|
|
body
|
|
|
|
)
|
|
|
|
|
|
|
|
if (asRaw) {
|
|
|
|
const guild = new Guild(this.client, result)
|
|
|
|
return guild
|
|
|
|
} else {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 14:22:13 +00:00
|
|
|
|
2021-01-18 11:56:51 +00:00
|
|
|
/**
|
2021-01-20 16:28:18 +00:00
|
|
|
* Deletes a guild. Returns deleted guild.
|
2021-01-18 11:56:51 +00:00
|
|
|
* @param guild Guild or guild id
|
|
|
|
*/
|
2021-01-16 14:22:13 +00:00
|
|
|
async delete(guild: Guild | string): Promise<Guild | undefined> {
|
|
|
|
if (guild instanceof Guild) {
|
|
|
|
guild = guild.id
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldGuild = await this.get(guild)
|
|
|
|
|
|
|
|
await this.client.rest.delete(GUILD(guild))
|
|
|
|
return oldGuild
|
2021-01-21 12:39:51 +00:00
|
|
|
}
|
2020-11-02 06:58:23 +00:00
|
|
|
}
|