From b2c1996b64fa61dc59ab790e6a460fb7890f90b3 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Mon, 24 Mar 2025 17:47:35 +0100
Subject: [PATCH] Refactor connect_voice_chan method to streamline voice
 channel connection logic

---
 botbotbot/voice.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/botbotbot/voice.py b/botbotbot/voice.py
index e234108..6ad70b5 100644
--- a/botbotbot/voice.py
+++ b/botbotbot/voice.py
@@ -126,11 +126,12 @@ class VoiceBot:
     async def connect_voice_chan(
         self, channel: discord.VoiceChannel | discord.StageChannel
     ) -> discord.VoiceClient:
-        if channel.guild.id in self.vo:
+        vo = self.vo.get(channel.guild.id, None)
+        if vo is not None and vo.channel != channel:
             await self.disconnect_voice(channel.guild)
-
-        vo: discord.VoiceClient = await channel.connect()
-        self.vo[channel.guild.id] = vo
+        if vo is None or vo.channel != channel:
+            vo = await channel.connect()
+            self.vo[channel.guild.id] = vo
         return vo
 
     async def disconnect_voice(self, guild: discord.Guild) -> None: