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'
|
|
|
|
import { GUILD, GUILDS } from '../types/endpoint.ts'
|
|
|
|
import {
|
|
|
|
GuildChannels,
|
|
|
|
GuildPayload,
|
|
|
|
MemberPayload,
|
|
|
|
GuildCreateRolePayload,
|
|
|
|
GuildCreatePayload,
|
|
|
|
Verification,
|
|
|
|
GuildCreateChannelOptions,
|
|
|
|
GuildCreateChannelPayload
|
|
|
|
} 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'
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-11-02 06:58:23 +00:00
|
|
|
}
|