From d3a6622b1454ced6f1d3f5a81a53f354bf1af859 Mon Sep 17 00:00:00 2001 From: 0t4u <61939142+0t4u@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:33:54 +0100 Subject: [PATCH] feat: move voice channel function --- src/Shoukaku.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Shoukaku.ts b/src/Shoukaku.ts index 8a61f1f3..373e332d 100644 --- a/src/Shoukaku.ts +++ b/src/Shoukaku.ts @@ -1,4 +1,4 @@ -import { ShoukakuDefaults, VoiceState } from './Constants'; +import { ShoukakuDefaults, State, VoiceState } from './Constants'; import { Node, NodeEvents } from './node/Node'; import { Connector } from './connectors/Connector'; import { Constructor, mergeDefault, TypedEventEmitter } from './Utils'; @@ -292,6 +292,37 @@ export class Shoukaku extends TypedEventEmitter { } } + /** + * Leaves current voice channel and joins a new one + * @param guildId GuildId in which the ChannelId of the voice channel is located + * @param channelId Id of channel to move to + * @throws {@link Error} When guild does not have an existing connection, or could not be moved + * @returns The moved player + */ + public async moveVoiceChannel(guildId: string, channelId: string) { + const connection = this.connections.get(guildId); + if (!connection) + throw new Error('This guild does not have an existing connection'); + + if (connection.channelId === channelId) return; + + try { + connection.setStateUpdate({ + session_id: connection.sessionId!, + channel_id: channelId, + self_deaf: connection.deafened, + self_mute: connection.muted + }); + connection.state = State.RECONNECTING; + await connection.connect(); + + // player should get updated automagically as connectionUpdate is fired + return this.players.get(guildId); + } catch (error) { + throw new Error(`Could not move to new voice channel with id ${channelId}`, { cause: error }); + } + } + /** * Cleans the disconnected lavalink node * @param node The node to clean