botbotbot/botbotbot/voice.py

63 lines
1.9 KiB
Python

import logging
import random
import discord
from botbotbot.shuffle import Shuffler
from botbotbot.tts import CambAI
logger = logging.getLogger(__name__)
class VoiceBot:
def __init__(
self,
bot: discord.Bot,
cambai: CambAI | None = None,
shuffler: Shuffler | None = None,
) -> None:
self.bot = bot
self.cambai = cambai
self.shf = shuffler
def init_events(self) -> None:
self.bot.add_listener(self.on_voice_state_update, "on_voice_state_update")
async def on_voice_state_update(
self,
member: discord.Member,
before: discord.VoiceState,
after: discord.VoiceState,
) -> None:
logger.debug("Voice state update")
logger.debug(before.channel)
logger.debug(after.channel)
if after.channel:
logger.debug(after.channel.members)
if (
self.cambai is not None
and before.channel is None
and after.channel is not None
and self.bot not in after.channel.members
and self.bot.user
and member.id != self.bot.user.id
and random.random() < 5 / 100
):
logger.info("Generating tts")
script = random.choice(
[
"Salut la jeunesse !",
f"Salut {member.display_name}, ça va bien ?",
"Allo ? À l'huile !",
]
)
source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
vo: discord.VoiceClient = await after.channel.connect()
await vo.play(source, wait_finish=True)
await vo.disconnect()
if self.shf and before.channel is None and random.random() < 5 / 100:
logger.info(f"Voice shuffle from {member}")
await self.shf.try_shuffle(member.guild)