Refactor VoiceBot connection logic to randomize connection intervals and improve handling of empty voice channels
This commit is contained in:
parent
e5d3c90e36
commit
390449ecdb
1 changed files with 3 additions and 6 deletions
|
@ -42,13 +42,13 @@ class VoiceBot:
|
||||||
while True:
|
while True:
|
||||||
for g_id in self.guild_ids:
|
for g_id in self.guild_ids:
|
||||||
guild = self.bot.get_guild(g_id)
|
guild = self.bot.get_guild(g_id)
|
||||||
if guild is None or random.random() > 10 / 100:
|
if guild is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
logger.info("Random connect.")
|
logger.info("Random connect.")
|
||||||
voice_chan = random.choice(guild.voice_channels)
|
voice_chan = random.choice(guild.voice_channels)
|
||||||
await self.connect_voice(voice_chan)
|
await self.connect_voice(voice_chan)
|
||||||
await asyncio.sleep(60)
|
await asyncio.sleep(3600 * random.random())
|
||||||
|
|
||||||
async def connect_voice(
|
async def connect_voice(
|
||||||
self, channel: discord.VoiceChannel | discord.StageChannel
|
self, channel: discord.VoiceChannel | discord.StageChannel
|
||||||
|
@ -59,8 +59,6 @@ class VoiceBot:
|
||||||
logger.info(f"Connecting to voice channel <{channel}>.")
|
logger.info(f"Connecting to voice channel <{channel}>.")
|
||||||
if len(channel.members) == 0:
|
if len(channel.members) == 0:
|
||||||
vo = await self.connect_voice_chan(channel)
|
vo = await self.connect_voice_chan(channel)
|
||||||
await asyncio.sleep(10)
|
|
||||||
await vo.disconnect()
|
|
||||||
return
|
return
|
||||||
elif len(channel.members) == 1:
|
elif len(channel.members) == 1:
|
||||||
member = self.rnd_name(channel.members[0])
|
member = self.rnd_name(channel.members[0])
|
||||||
|
@ -86,9 +84,8 @@ class VoiceBot:
|
||||||
)
|
)
|
||||||
source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
|
source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
|
||||||
vo = await self.connect_voice_chan(channel)
|
vo = await self.connect_voice_chan(channel)
|
||||||
|
await asyncio.sleep(10 * random.random())
|
||||||
await vo.play(source, wait_finish=True)
|
await vo.play(source, wait_finish=True)
|
||||||
await vo.disconnect()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def rnd_name(cls, user: discord.Member) -> str:
|
def rnd_name(cls, user: discord.Member) -> str:
|
||||||
|
|
Loading…
Add table
Reference in a new issue