Add VoiceBot class for voice state handling; enhance Wordlist initialization with guild IDs

This commit is contained in:
Edgar P. Burkhart 2025-03-22 23:07:54 +01:00
parent b9c84f16be
commit e6fc040d78
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 89 additions and 42 deletions

View file

@ -8,6 +8,7 @@ from botbotbot.ai import AIBot
from botbotbot.shuffle import Shuffler from botbotbot.shuffle import Shuffler
from botbotbot.text import TextBot from botbotbot.text import TextBot
from botbotbot.tts import CambAI from botbotbot.tts import CambAI
from botbotbot.voice import VoiceBot
from botbotbot.wordlist import Wordlist from botbotbot.wordlist import Wordlist
@ -21,6 +22,9 @@ def main() -> None:
config = tomllib.load(config_file) config = tomllib.load(config_file)
guild_ids = config.get("guild_ids") guild_ids = config.get("guild_ids")
if not (isinstance(guild_ids, list) and all(isinstance(i, int) for i in guild_ids)):
logger.error("Guild IDs must be a list of integers.")
guild_ids = []
system_prompt = """Tu es une intelligence artificelle qui répond en français. system_prompt = """Tu es une intelligence artificelle qui répond en français.
Tu dois faire un commentaire pertinent en lien avec ce qui te sera dit. Tu dois faire un commentaire pertinent en lien avec ce qui te sera dit.
@ -49,11 +53,15 @@ def main() -> None:
bot = discord.Bot(description=description, intents=intents) bot = discord.Bot(description=description, intents=intents)
shf = Shuffler() shf = Shuffler()
wl = Wordlist(bot) wl = Wordlist(bot, guild_ids)
wl.init_events()
text_bot = TextBot(bot, wl, aibot=aibot, shuffler=shf) text_bot = TextBot(bot, wl, aibot=aibot, shuffler=shf)
text_bot.init_events() text_bot.init_events()
voice_bot = VoiceBot(bot, cambai, shuffler=shf)
voice_bot.init_events()
@bot.listen("on_ready") @bot.listen("on_ready")
async def on_ready() -> None: async def on_ready() -> None:
logger.info(f"We have logged in as {bot.user}") logger.info(f"We have logged in as {bot.user}")
@ -73,43 +81,6 @@ def main() -> None:
await ctx.respond(embed=embed) await ctx.respond(embed=embed)
logger.info("ERRE ALEA") logger.info("ERRE ALEA")
@bot.listen("on_voice_state_update")
async def on_voice_state_update(
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 (
cambai is not None
and before.channel is None
and after.channel is not None
and bot not in after.channel.members
and bot.user
and member.id != 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(cambai.tts(script))
vo: discord.VoiceClient = await after.channel.connect()
await vo.play(source, wait_finish=True)
await vo.disconnect()
if before.channel is None and random.random() < 5 / 100:
logger.info(f"Voice shuffle from {member}")
await shf.try_shuffle(member.guild)
@bot.slash_command( @bot.slash_command(
name="indu", guild_ids=guild_ids, description="Poser une question à MistralAI" name="indu", guild_ids=guild_ids, description="Poser une question à MistralAI"
) )

View file

@ -0,0 +1,63 @@
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)

View file

@ -10,28 +10,41 @@ logger = logging.getLogger(__name__)
class Wordlist: class Wordlist:
def __init__( def __init__(
self, bot: discord.Bot, path: pathlib.Path = pathlib.Path("wordlist.pickle") self,
bot: discord.Bot,
guild_ids: list[int] = [],
path: pathlib.Path = pathlib.Path("wordlist.pickle"),
): ):
self.path = path self.path = path
if self.path.exists(): if self.path.exists():
with self.path.open("rb") as w_file: with self.path.open("rb") as w_file:
self.word_list: list[str] = pickle.load(w_file) self.word_list: list[str] = pickle.load(w_file)
self.bot = bot self.bot = bot
self.guild_ids = guild_ids
def init_events(self) -> None: def init_events(self) -> None:
self.bot.add_application_command( self.bot.add_application_command(
discord.SlashCommand( discord.SlashCommand(
self.bibl, name="bibl", description="Ajouter une phrase" self.bibl,
name="bibl",
description="Ajouter une phrase",
guild_ids=self.guild_ids,
) )
) )
self.bot.add_application_command( self.bot.add_application_command(
discord.SlashCommand( discord.SlashCommand(
self.tabl, name="tabl", description="Lister les phrases" self.tabl,
name="tabl",
description="Lister les phrases",
guild_ids=self.guild_ids,
) )
) )
self.bot.add_application_command( self.bot.add_application_command(
discord.SlashCommand( discord.SlashCommand(
self.enle, name="enle", description="Enlever une phrase" self.enle,
name="enle",
description="Enlever une phrase",
guild_ids=self.guild_ids,
) )
) )