diff --git a/structures/VoiceChannel.ts b/structures/VoiceChannel.ts index 30a4d50..2c65c9f 100644 --- a/structures/VoiceChannel.ts +++ b/structures/VoiceChannel.ts @@ -1,16 +1,31 @@ import type { Snowflake } from "../util/Snowflake.ts"; import type { Session } from "../session/Session.ts"; import type { DiscordChannel, VideoQualityModes } from "../vendor/external.ts"; +import { GatewayOpcodes } from "../vendor/external.ts"; +import { calculateShardId } from "../vendor/gateway/calculateShardId.ts"; import GuildChannel from "./GuildChannel.ts"; +/** + * @link https://discord.com/developers/docs/topics/gateway#update-voice-state + * */ +export interface UpdateVoiceState { + guildId: string; + channelId?: string; + selfMute: boolean; + selfDeaf: boolean; +} + export class VoiceChannel extends GuildChannel { - constructor(session: Session, guildId: Snowflake, data: DiscordChannel) { + constructor(session: Session, data: DiscordChannel, guildId: Snowflake) { super(session, data, guildId); this.bitRate = data.bitrate; this.userLimit = data.user_limit ?? 0; - data.rtc_region ? this.rtcRegion = data.rtc_region : undefined; this.videoQuality = data.video_quality_mode; this.nsfw = !!data.nsfw; + + if (data.rtc_region) { + this.rtcRegion = data.rtc_region; + } } bitRate?: number; userLimit: number; @@ -18,6 +33,28 @@ export class VoiceChannel extends GuildChannel { videoQuality?: VideoQualityModes; nsfw: boolean; + + /** + * This function was gathered from Discordeno it may not work + * */ + async connect(options?: UpdateVoiceState) { + const shardId = calculateShardId(this.session.gateway, BigInt(super.guildId)); + const shard = this.session.gateway.manager.shards.get(shardId); + + if (!shard) { + throw new Error(`Shard (id: ${shardId} not found`); + } + + await shard.send({ + op: GatewayOpcodes.VoiceStateUpdate, + d: { + guild_id: super.guildId, + channel_id: super.id, + self_mute: Boolean(options?.selfMute), + self_deaf: options?.selfDeaf ?? true, + }, + }); + } } export default VoiceChannel;