Implement random disconnect logic in VoiceBot to enhance connection management and improve voice channel handling
This commit is contained in:
parent
390449ecdb
commit
a811d870f3
1 changed files with 14 additions and 6 deletions
|
@ -45,9 +45,14 @@ class VoiceBot:
|
||||||
if guild is None:
|
if guild is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
logger.info("Random connect.")
|
if random.random() < 50 / 100:
|
||||||
voice_chan = random.choice(guild.voice_channels)
|
logger.info("Random connect.")
|
||||||
await self.connect_voice(voice_chan)
|
voice_chan = random.choice(guild.voice_channels)
|
||||||
|
await self.connect_voice(voice_chan)
|
||||||
|
else:
|
||||||
|
logger.info("Random disconnect.")
|
||||||
|
await self.disconnect_voice(guild)
|
||||||
|
|
||||||
await asyncio.sleep(3600 * random.random())
|
await asyncio.sleep(3600 * random.random())
|
||||||
|
|
||||||
async def connect_voice(
|
async def connect_voice(
|
||||||
|
@ -94,11 +99,14 @@ class VoiceBot:
|
||||||
async def connect_voice_chan(
|
async def connect_voice_chan(
|
||||||
self, channel: discord.VoiceChannel | discord.StageChannel
|
self, channel: discord.VoiceChannel | discord.StageChannel
|
||||||
) -> discord.VoiceClient:
|
) -> discord.VoiceClient:
|
||||||
for vc in self.bot.voice_clients:
|
await self.disconnect_voice(channel.guild)
|
||||||
if isinstance(vc, discord.VoiceClient) and vc.guild == channel.guild:
|
|
||||||
await vc.disconnect()
|
|
||||||
return await channel.connect()
|
return await channel.connect()
|
||||||
|
|
||||||
|
async def disconnect_voice(self, guild: discord.Guild) -> None:
|
||||||
|
for vc in self.bot.voice_clients:
|
||||||
|
if isinstance(vc, discord.VoiceClient) and vc.guild == guild:
|
||||||
|
await vc.disconnect()
|
||||||
|
|
||||||
async def join_voice(self, ctx: discord.ApplicationContext) -> None:
|
async def join_voice(self, ctx: discord.ApplicationContext) -> None:
|
||||||
if ctx.user.voice:
|
if ctx.user.voice:
|
||||||
await ctx.respond(
|
await ctx.respond(
|
||||||
|
|
Loading…
Add table
Reference in a new issue