Refactor VoiceBot to manage voice connections with a dictionary and improve channel handling logic
This commit is contained in:
parent
1de2ffb2b6
commit
3f6e41b0c0
1 changed files with 33 additions and 14 deletions
|
@ -26,6 +26,7 @@ class VoiceBot:
|
||||||
self.shf = shuffler
|
self.shf = shuffler
|
||||||
self.aibot = aibot
|
self.aibot = aibot
|
||||||
self.guild_ids = guild_ids
|
self.guild_ids = guild_ids
|
||||||
|
self.vo: dict[int, discord.VoiceClient] = dict()
|
||||||
|
|
||||||
self.mots = pathlib.Path("assets/mots.txt").read_text().split("\n")
|
self.mots = pathlib.Path("assets/mots.txt").read_text().split("\n")
|
||||||
|
|
||||||
|
@ -59,7 +60,12 @@ class VoiceBot:
|
||||||
|
|
||||||
if random.random() < 50 / 100:
|
if random.random() < 50 / 100:
|
||||||
logger.info("Random connect.")
|
logger.info("Random connect.")
|
||||||
voice_chan = random.choice(guild.voice_channels)
|
voice_chan = random.choices(
|
||||||
|
guild.voice_channels,
|
||||||
|
weights=[
|
||||||
|
10 * len(vc.members) + 1 for vc in guild.voice_channels
|
||||||
|
],
|
||||||
|
)[0]
|
||||||
await self.connect_voice(voice_chan)
|
await self.connect_voice(voice_chan)
|
||||||
if not voice_chan.status and self.aibot is not None:
|
if not voice_chan.status and self.aibot is not None:
|
||||||
mot = random.choice(self.mots)
|
mot = random.choice(self.mots)
|
||||||
|
@ -82,7 +88,7 @@ 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)
|
await self.connect_voice_chan(channel)
|
||||||
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])
|
||||||
|
@ -121,13 +127,16 @@ 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:
|
||||||
await self.disconnect_voice(channel.guild)
|
if channel.guild.id in self.vo:
|
||||||
return await channel.connect()
|
await self.disconnect_voice(channel.guild)
|
||||||
|
|
||||||
|
vo: discord.VoiceClient = await channel.connect()
|
||||||
|
self.vo[channel.guild.id] = vo
|
||||||
|
return vo
|
||||||
|
|
||||||
async def disconnect_voice(self, guild: discord.Guild) -> None:
|
async def disconnect_voice(self, guild: discord.Guild) -> None:
|
||||||
for vc in self.bot.voice_clients:
|
if vo := self.vo.pop(guild.id, None):
|
||||||
if isinstance(vc, discord.VoiceClient) and vc.guild == guild:
|
await vo.disconnect()
|
||||||
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:
|
||||||
|
@ -157,14 +166,24 @@ class VoiceBot:
|
||||||
logger.debug(after.channel.members)
|
logger.debug(after.channel.members)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
self.cambai is not None
|
self.cambai is None
|
||||||
and before.channel is None
|
or after.channel is None
|
||||||
and after.channel is not None
|
or before.channel == after.channel
|
||||||
and self.bot not in after.channel.members
|
or (self.bot.user and member == self.bot.user)
|
||||||
and self.bot.user
|
|
||||||
and member.id != self.bot.user.id
|
|
||||||
and random.random() < 5 / 100
|
|
||||||
):
|
):
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.bot.user in after.channel.members and (
|
||||||
|
vo := self.vo.get(after.channel.guild.id, None)
|
||||||
|
):
|
||||||
|
m = self.rnd_name(member)
|
||||||
|
script = f"Salut {m}, ça va bien ?"
|
||||||
|
source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
|
||||||
|
await asyncio.sleep(10 * random.random())
|
||||||
|
await vo.play(source, wait_finish=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
if random.random() < 5 / 100:
|
||||||
await self.connect_voice(after.channel)
|
await self.connect_voice(after.channel)
|
||||||
|
|
||||||
async def shuffle(
|
async def shuffle(
|
||||||
|
|
Loading…
Add table
Reference in a new issue