complete rest added

This commit is contained in:
DjDeveloperr 2021-01-21 18:09:51 +05:30
parent e7b0804616
commit cc75a34d56
7 changed files with 250 additions and 0 deletions

View file

@ -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<GuildChannelsManager> {
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
}
}

View file

@ -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<GuildPayload, Guild> {
.catch((e) => reject(e))
})
}
/** 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)
}
}

View file

@ -80,4 +80,20 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
)
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<RolesManager> {
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
}
}

View file

@ -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<ClientEvents> {
// @ts-ignore
return super.emit(event, ...args)
}
/** Returns an array of voice region objects that can be used when creating servers. */
async fetchVoiceRegions(): Promise<VoiceRegion[]> {
return this.rest.api.voice.regions.get()
}
/** Modify current (Client) User. */
async editUser(data: {
username?: string
avatar?: string
}): Promise<Client> {
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<Client> {
return await this.editUser({ username })
}
/** Change Avatar of the Client User */
async setAvatar(avatar: string): Promise<Client> {
return await this.editUser({ avatar })
}
/** Create a DM Channel with a User */
async createDM(user: User | string): Promise<DMChannel> {
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<DMChannel>(dmPayload.id) as unknown) as DMChannel
}
/** Returns a template object for the given code. */
async fetchTemplate(code: string): Promise<Template> {
const payload = await this.rest.api.guilds.templates[code].get()
return new Template(this, payload)
}
}
/** Event decorator to create an Event handler from function */

View file

@ -4,6 +4,7 @@ import {
GuildFeatures,
GuildIntegrationPayload,
GuildPayload,
GuildWidgetPayload,
IntegrationAccountPayload,
IntegrationExpireBehavior
} from '../types/guild.ts'
@ -25,6 +26,8 @@ import { GUILD_BAN, GUILD_BANS, GUILD_INTEGRATIONS } from '../types/endpoint.ts'
import { GuildVoiceStatesManager } from '../managers/guildVoiceStates.ts'
import { RequestMembersOptions } from '../gateway/index.ts'
import { GuildPresencesManager } from '../managers/presences.ts'
import { TemplatePayload } from '../types/template.ts'
import { Template } from './template.ts'
export class GuildBan extends Base {
guild: Guild
@ -326,6 +329,125 @@ export class Guild extends Base {
}, timeout)
})
}
/** 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
}
}
export class GuildIntegration extends Base {

View file

@ -150,3 +150,12 @@ export interface GuildBanPayload {
reason: string | null
user: UserPayload
}
export interface GuildWidgetPayload {
id: string
name: string
instant_invite: string
channels: Array<{ id: string; name: string; position: number }>
members: MemberPayload[]
presence_count: number
}

View file

@ -42,3 +42,19 @@ export interface VoiceStatePayload {
self_video: boolean
suppress: boolean
}
/** Voice Region Structure */
export interface VoiceRegion {
/** Unique ID for the region */
id: string
/** Name of the region */
name: string
/** True if this is a vip-only server */
vip: boolean
/** True for a single server that is closest to the current user's client */
optimal: boolean
/** Whether this is a deprecated voice region (avoid switching to these) */
deprecated: boolean
/** Whether this is a custom voice region (used for events/etc) */
custom: boolean
}