From 390449ecdb7aeb601926a5a141431d7216d499fb Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Sun, 23 Mar 2025 12:11:43 +0100
Subject: [PATCH] Refactor VoiceBot connection logic to randomize connection
 intervals and improve handling of empty voice channels

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

diff --git a/botbotbot/voice.py b/botbotbot/voice.py
index 0eebbb6..20ca4b8 100644
--- a/botbotbot/voice.py
+++ b/botbotbot/voice.py
@@ -42,13 +42,13 @@ class VoiceBot:
         while True:
             for g_id in self.guild_ids:
                 guild = self.bot.get_guild(g_id)
-                if guild is None or random.random() > 10 / 100:
+                if guild is None:
                     continue
 
                 logger.info("Random connect.")
                 voice_chan = random.choice(guild.voice_channels)
                 await self.connect_voice(voice_chan)
-            await asyncio.sleep(60)
+            await asyncio.sleep(3600 * random.random())
 
     async def connect_voice(
         self, channel: discord.VoiceChannel | discord.StageChannel
@@ -59,8 +59,6 @@ class VoiceBot:
         logger.info(f"Connecting to voice channel <{channel}>.")
         if len(channel.members) == 0:
             vo = await self.connect_voice_chan(channel)
-            await asyncio.sleep(10)
-            await vo.disconnect()
             return
         elif len(channel.members) == 1:
             member = self.rnd_name(channel.members[0])
@@ -86,9 +84,8 @@ class VoiceBot:
             )
         source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
         vo = await self.connect_voice_chan(channel)
-
+        await asyncio.sleep(10 * random.random())
         await vo.play(source, wait_finish=True)
-        await vo.disconnect()
 
     @classmethod
     def rnd_name(cls, user: discord.Member) -> str: