2020-11-08 07:58:24 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import { Guild } from '../structures/guild.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 {
|
|
|
|
GuildChannels,
|
|
|
|
GuildPayload,
|
|
|
|
MemberPayload,
|
|
|
|
GuildCreateRolePayload,
|
|
|
|
GuildCreatePayload,
|
|
|
|
Verification,
|
|
|
|
GuildCreateChannelOptions,
|
2021-01-15 16:45:08 +00:00
|
|
|
GuildCreateChannelPayload,
|
|
|
|
GuildPreview,
|
2021-01-15 17:15:24 +00:00
|
|
|
GuildPreviewPayload,
|
|
|
|
GuildModifyOptions,
|
|
|
|
GuildModifyPayload
|
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:15:52 +00:00
|
|
|
import { fetchAuto } from '../../deps.ts'
|
2021-01-15 16:45:08 +00:00
|
|
|
import { Emoji } from '../structures/emoji.ts'
|
2021-01-15 16:15:52 +00:00
|
|
|
|
|
|
|
export interface GuildCreateOptions {
|
|
|
|
name: string
|
|
|
|
region?: string
|
|
|
|
icon?: string
|
|
|
|
verificationLevel?: Verification
|
|
|
|
roles?: Array<Role | GuildCreateRolePayload>
|
|
|
|
channels?: Array<GuildChannels | GuildCreateChannelOptions>
|
|
|
|
afkChannelID?: string
|
|
|
|
afkTimeout?: number
|
|
|
|
systemChannelID?: string
|
|
|
|
}
|
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) => {
|
|
|
|
this.set(id, data)
|
|
|
|
|
|
|
|
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-15 16:15:52 +00:00
|
|
|
|
|
|
|
async create(options: GuildCreateOptions): Promise<Guild> {
|
|
|
|
if (options.icon !== undefined && !options.icon.startsWith('data:')) {
|
|
|
|
options.icon = await fetchAuto(options.icon)
|
|
|
|
}
|
|
|
|
|
|
|
|
const body: GuildCreatePayload = {
|
|
|
|
name: options.name,
|
|
|
|
region: options.region,
|
|
|
|
icon: options.icon,
|
|
|
|
verification_level: options.verificationLevel,
|
|
|
|
roles:
|
|
|
|
options.roles !== undefined
|
|
|
|
? 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
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
})
|
|
|
|
: undefined,
|
|
|
|
channels:
|
|
|
|
options.channels !== undefined
|
|
|
|
? options.channels.map(
|
|
|
|
(obj): GuildCreateChannelPayload => ({
|
|
|
|
id: obj.id,
|
|
|
|
name: obj.name,
|
|
|
|
type: obj.type,
|
|
|
|
parent_id: obj.parentID
|
|
|
|
})
|
|
|
|
)
|
|
|
|
: undefined,
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-11-02 06:58:23 +00:00
|
|
|
}
|