Enhance VoiceBot to accept guild IDs and implement join command for voice channel connection
This commit is contained in:
parent
31f10b8438
commit
3166661adc
2 changed files with 60 additions and 18 deletions
botbotbot
|
@ -59,7 +59,7 @@ def main() -> None:
|
||||||
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, aibot=aibot, shuffler=shf)
|
voice_bot = VoiceBot(bot, cambai, aibot=aibot, shuffler=shf, guild_ids=guild_ids)
|
||||||
voice_bot.init_events()
|
voice_bot.init_events()
|
||||||
|
|
||||||
@bot.listen("on_ready")
|
@bot.listen("on_ready")
|
||||||
|
|
|
@ -17,14 +17,72 @@ class VoiceBot:
|
||||||
cambai: CambAI | None = None,
|
cambai: CambAI | None = None,
|
||||||
aibot: AIBot | None = None,
|
aibot: AIBot | None = None,
|
||||||
shuffler: Shuffler | None = None,
|
shuffler: Shuffler | None = None,
|
||||||
|
guild_ids: list[int] = [],
|
||||||
) -> None:
|
) -> None:
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.cambai = cambai
|
self.cambai = cambai
|
||||||
self.shf = shuffler
|
self.shf = shuffler
|
||||||
self.aibot = aibot
|
self.aibot = aibot
|
||||||
|
self.guild_ids = guild_ids
|
||||||
|
|
||||||
def init_events(self) -> None:
|
def init_events(self) -> None:
|
||||||
self.bot.add_listener(self.on_voice_state_update, "on_voice_state_update")
|
self.bot.add_listener(self.on_voice_state_update, "on_voice_state_update")
|
||||||
|
self.bot.add_application_command(
|
||||||
|
discord.SlashCommand(
|
||||||
|
self.join_voice,
|
||||||
|
name="join",
|
||||||
|
description="Rejoindre un channel vocal",
|
||||||
|
guild_ids=self.guild_ids,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
async def connect_voice(
|
||||||
|
self, channel: discord.VoiceChannel | discord.StageChannel
|
||||||
|
) -> None:
|
||||||
|
if self.cambai is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.info("Generating tts")
|
||||||
|
if len(channel.members) == 1:
|
||||||
|
member = channel.members[0].display_name
|
||||||
|
else:
|
||||||
|
member = (
|
||||||
|
", ".join(member.display_name for member in channel.members[:-1])
|
||||||
|
+ " et "
|
||||||
|
+ channel.members[-1].display_name
|
||||||
|
)
|
||||||
|
|
||||||
|
script = None
|
||||||
|
if self.aibot is not None and random.random() < 20 / 100:
|
||||||
|
script = self.aibot.answer("Dis un truc aléatoire et original.")
|
||||||
|
if script is None:
|
||||||
|
script = random.choice(
|
||||||
|
[
|
||||||
|
"Salut la jeunesse !",
|
||||||
|
f"Salut {member}, ça va bien ?",
|
||||||
|
"Allo ? À l'huile !",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
source = await discord.FFmpegOpusAudio.from_probe(self.cambai.tts(script))
|
||||||
|
vo: discord.VoiceClient = await channel.connect()
|
||||||
|
|
||||||
|
await vo.play(source, wait_finish=True)
|
||||||
|
await vo.disconnect()
|
||||||
|
|
||||||
|
async def join_voice(self, ctx: discord.ApplicationContext) -> None:
|
||||||
|
if ctx.user.voice:
|
||||||
|
await ctx.respond(
|
||||||
|
f"Connecting to voice channel {ctx.user.voice.channel}.",
|
||||||
|
ephemeral=True,
|
||||||
|
delete_after=30,
|
||||||
|
)
|
||||||
|
await self.connect_voice(ctx.user.voice.channel)
|
||||||
|
else:
|
||||||
|
await ctx.respond(
|
||||||
|
"You are not connected to a voice channel.",
|
||||||
|
ephemeral=True,
|
||||||
|
delete_after=30,
|
||||||
|
)
|
||||||
|
|
||||||
async def on_voice_state_update(
|
async def on_voice_state_update(
|
||||||
self,
|
self,
|
||||||
|
@ -47,23 +105,7 @@ class VoiceBot:
|
||||||
and member.id != self.bot.user.id
|
and member.id != self.bot.user.id
|
||||||
and random.random() < 5 / 100
|
and random.random() < 5 / 100
|
||||||
):
|
):
|
||||||
logger.info("Generating tts")
|
await self.connect_voice(after.channel)
|
||||||
script = None
|
|
||||||
if self.aibot is not None and random.random() < 20 / 100:
|
|
||||||
script = self.aibot.answer("Dis un truc aléatoire et original.")
|
|
||||||
if script is None:
|
|
||||||
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:
|
if self.shf and before.channel is None and random.random() < 5 / 100:
|
||||||
logger.info(f"Voice shuffle from {member}")
|
logger.info(f"Voice shuffle from {member}")
|
||||||
|
|
Loading…
Add table
Reference in a new issue