From 50213749703d33ee39bdc1044e03fa18cd3a7052 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 19 Jul 2022 18:47:42 -0300 Subject: [PATCH] Fix for ChannelFactory and add Guild.fetchVoiceRegions (#74) * Add CategoryChannel and update ChannelFactory * Add Guild.fetchVoiceRegions method * Remove no used type * Update CategoryChannel * Update guilds cache * Fix --- packages/biscuit/Routes.ts | 5 +++ packages/biscuit/structures/channels.ts | 43 ++++++++++++++++++++++++- packages/biscuit/structures/guilds.ts | 14 ++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/biscuit/Routes.ts b/packages/biscuit/Routes.ts index 5a0e632..c87e8a4 100644 --- a/packages/biscuit/Routes.ts +++ b/packages/biscuit/Routes.ts @@ -448,3 +448,8 @@ export function GUILD_WIDGET(guildId: Snowflake, options: GetWidget = { get: 'se return url; } + +/** @link https://discord.com/developers/docs/resources/guild#get-guild-voice-regions */ +export function GUILD_VOICE_REGIONS(guildId: string): string { + return `/guilds/${guildId}/regions`; +} \ No newline at end of file diff --git a/packages/biscuit/structures/channels.ts b/packages/biscuit/structures/channels.ts index 366c7a3..a6185de 100644 --- a/packages/biscuit/structures/channels.ts +++ b/packages/biscuit/structures/channels.ts @@ -30,6 +30,7 @@ import Invite from './Invite.ts'; import Webhook from './Webhook.ts'; import User from './User.ts'; import ThreadMember from './ThreadMember.ts'; +import Permissions, { PermissionResolvable } from "./Permissions.ts"; /** * Abstract class that represents the base for creating a new channel. @@ -89,6 +90,43 @@ export abstract class BaseChannel implements Model { } } +/** + * Represents a category channel. + */ +export class CategoryChannel extends BaseChannel { + constructor(session: Session, data: DiscordChannel) { + super(session, data); + this.id = data.id; + this.name = data.name ? data.name : ''; + this.nsfw = data.nsfw ? data.nsfw : false; + this.guildId = data.guild_id ? data.guild_id : undefined; + this.type = ChannelTypes.GuildCategory; + this.position = data.position ? data.position : undefined; + this.parentId = data.parent_id ? data.parent_id : undefined; + + this.permissionOverwrites = [] as PermissionsOverwrites[]; + // TODO: improve this and test + if (data.permission_overwrites && data.permission_overwrites.length > 0) { + data.permission_overwrites.forEach(v => { + this.permissionOverwrites.push({ + id: v.id, + type: v.type, + allow: new Permissions(parseInt(v.allow as string) as PermissionResolvable), + deny: new Permissions(parseInt(v.deny as string) as PermissionResolvable), + } as PermissionsOverwrites); + }) + } + } + + id: Snowflake; + parentId?: string; + name: string; + permissionOverwrites: PermissionsOverwrites[]; + nsfw: boolean; + guildId?: Snowflake; + position?: number; +} + /** TextChannel */ /** * @link https://discord.com/developers/docs/resources/channel#create-channel-invite-json-params @@ -816,7 +854,8 @@ export type Channel = | DMChannel | NewsChannel | ThreadChannel - | StageChannel; + | StageChannel + | CategoryChannel; export type ChannelInGuild = | GuildTextChannel @@ -868,6 +907,8 @@ export class ChannelFactory { return new VoiceChannel(session, channel, channel.guild_id!); case ChannelTypes.GuildStageVoice: return new StageChannel(session, channel, channel.guild_id!); + case ChannelTypes.GuildCategory: + return new CategoryChannel(session, channel); default: if (textBasedChannels.includes(channel.type)) { return new TextChannel(session, channel); diff --git a/packages/biscuit/structures/guilds.ts b/packages/biscuit/structures/guilds.ts index 6683a62..32c02cf 100644 --- a/packages/biscuit/structures/guilds.ts +++ b/packages/biscuit/structures/guilds.ts @@ -10,6 +10,7 @@ import type { DiscordMemberWithUser, DiscordOverwrite, DiscordRole, + DiscordVoiceRegion, ExplicitContentFilterLevels, GuildNsfwLevel, MakeRequired, @@ -994,6 +995,19 @@ export class Guild extends BaseGuild implements Model { return new Guild(this.session, guild); } + + /** + * fetchVoiceRegions gets the voice regions available for the guild. + * @see {@link DiscordVoiceRegion} + * @returns Promise that resolves to an array of voice regions. + */ + async fetchVoiceRegions(): Promise { + return await this.session.rest.runMethod( + this.session.rest, + 'GET', + Routes.GUILD_VOICE_REGIONS(this.id), + ) + } } export default Guild;