From cc75a34d56d4ba1de3b18a48f232fb888b7e43c2 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 21 Jan 2021 18:09:51 +0530 Subject: [PATCH] complete rest added --- src/managers/guildChannels.ts | 16 +++++ src/managers/guilds.ts | 15 +++++ src/managers/roles.ts | 16 +++++ src/models/client.ts | 56 ++++++++++++++++ src/structures/guild.ts | 122 ++++++++++++++++++++++++++++++++++ src/types/guild.ts | 9 +++ src/types/voice.ts | 16 +++++ 7 files changed, 250 insertions(+) diff --git a/src/managers/guildChannels.ts b/src/managers/guildChannels.ts index bbc279e..258e6d9 100644 --- a/src/managers/guildChannels.ts +++ b/src/managers/guildChannels.ts @@ -99,4 +99,20 @@ export class GuildChannelsManager extends BaseChildManager< const channel = await this.get(res.id) return (channel as unknown) as GuildChannel } + + /** Modify the positions of a set of channel positions for the guild. */ + async editPositions( + ...positions: Array<{ id: string | GuildChannel; position: number | null }> + ): Promise { + if (positions.length === 0) + throw new Error('No channel positions to change specified') + + await this.client.rest.api.guilds[this.guild.id].channels.patch( + positions.map((e) => ({ + id: typeof e.id === 'string' ? e.id : e.id.id, + position: e.position ?? null + })) + ) + return this + } } diff --git a/src/managers/guilds.ts b/src/managers/guilds.ts index b90f247..c2c7970 100644 --- a/src/managers/guilds.ts +++ b/src/managers/guilds.ts @@ -1,5 +1,7 @@ +import { fetchAuto } from '../../deps.ts' import { Client } from '../models/client.ts' import { Guild } from '../structures/guild.ts' +import { Template } from '../structures/template.ts' import { GUILD } from '../types/endpoint.ts' import { GuildPayload, MemberPayload } from '../types/guild.ts' import { BaseManager } from './base.ts' @@ -32,4 +34,17 @@ export class GuildManager extends BaseManager { .catch((e) => reject(e)) }) } + + /** Create a new guild based on a template. */ + async createFromTemplate( + template: Template | string, + name: string, + icon?: string + ): Promise { + 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) + } } diff --git a/src/managers/roles.ts b/src/managers/roles.ts index 0ad93ef..aaa5995 100644 --- a/src/managers/roles.ts +++ b/src/managers/roles.ts @@ -80,4 +80,20 @@ export class RolesManager extends BaseManager { ) return true } + + /** Modify the positions of a set of role positions for the guild. */ + async editPositions( + ...positions: Array<{ id: string | Role; position: number | null }> + ): Promise { + if (positions.length === 0) + throw new Error('No role positions to change specified') + + await this.client.rest.api.guilds[this.guild.id].roles.patch( + positions.map((e) => ({ + id: typeof e.id === 'string' ? e.id : e.id.id, + position: e.position ?? null + })) + ) + return this + } } diff --git a/src/models/client.ts b/src/models/client.ts index efbc478..4f1e185 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -21,6 +21,10 @@ import { INVITE } from '../types/endpoint.ts' import { ClientEvents } from '../gateway/handlers/index.ts' import type { Collector } from './collectors.ts' import { HarmonyEventEmitter } from '../utils/events.ts' +import { VoiceRegion } from '../types/voice.ts' +import { fetchAuto } from '../../deps.ts' +import { DMChannel } from '../structures/dmChannel.ts' +import { Template } from '../structures/template.ts' /** OS related properties sent with Gateway Identify */ export interface ClientProperties { @@ -354,6 +358,58 @@ export class Client extends HarmonyEventEmitter { // @ts-ignore return super.emit(event, ...args) } + + /** Returns an array of voice region objects that can be used when creating servers. */ + async fetchVoiceRegions(): Promise { + return this.rest.api.voice.regions.get() + } + + /** Modify current (Client) User. */ + async editUser(data: { + username?: string + avatar?: string + }): Promise { + if (data.username === undefined && data.avatar === undefined) + throw new Error( + 'Either username or avatar or both must be specified to edit' + ) + + if (data.avatar?.startsWith('http') === true) { + data.avatar = await fetchAuto(data.avatar) + } + + await this.rest.api.users['@me'].patch({ + username: data.username, + avatar: data.avatar + }) + return this + } + + /** Change Username of the Client User */ + async setUsername(username: string): Promise { + return await this.editUser({ username }) + } + + /** Change Avatar of the Client User */ + async setAvatar(avatar: string): Promise { + return await this.editUser({ avatar }) + } + + /** Create a DM Channel with a User */ + async createDM(user: User | string): Promise { + const id = typeof user === 'object' ? user.id : user + const dmPayload = await this.rest.api.users['@me'].channels.post({ + recipient_id: id + }) + await this.channels.set(dmPayload.id, dmPayload) + return (this.channels.get(dmPayload.id) as unknown) as DMChannel + } + + /** Returns a template object for the given code. */ + async fetchTemplate(code: string): Promise