From c0053ca98912901b5d387d58fbc57f9b7da91f38 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 18 Mar 2026 15:29:51 +0100 Subject: voice doesn't work due to DAVE, but we got copyparty search --- cogs/vclogs.py | 55 ++++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'cogs/vclogs.py') diff --git a/cogs/vclogs.py b/cogs/vclogs.py index 901b557..9ed7134 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py @@ -7,28 +7,27 @@ import datetime import json import logging import os +import asyncio import discord from discord.ext import commands from gtts import gTTS import aiofiles +from cogs.voice import get_voice_manager logger = logging.getLogger(__name__) - class VcLogs(commands.Cog): def __init__(self, bot): self.bot = bot - self.settings_check() + self._voice_manager = None - settings_file = 'settings.json' + def get_voice_manager(self): + if self._voice_manager is None: + self._voice_manager = get_voice_manager(self.bot) + return self._voice_manager - @staticmethod - def settings_check(): - if not os.path.exists(VcLogs.settings_file): - with open(VcLogs.settings_file, 'w') as f: - json.dump({'volume': '5'}, f, indent=4) - logger.info("settings.json created") + settings_file = 'settings.json' @staticmethod async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): @@ -62,32 +61,34 @@ class VcLogs(commands.Cog): return tts_text, channel async def play_tts(self, channel, text): - tts_file = "temp_tts.mp3" - # Generate TTS audio + voice_mgr = self.get_voice_manager() + + temp_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'temp') + os.makedirs(temp_dir, exist_ok=True) + tts_file = os.path.join(temp_dir, f"temp_tts_{os.getpid()}.mp3") tts = gTTS(text=text, lang='pl') + logger.info("saving file") tts.save(tts_file) if os.path.exists(VcLogs.settings_file): async with aiofiles.open(VcLogs.settings_file) as f: content = await f.read() data = json.loads(content) - volume = data['volume'] - else: - logger.error("settings.json not found") - - # Connect to the voice channel if not already connected - voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) - if not isinstance(voice_client, discord.VoiceClient): - voice_client = await channel.connect() - elif voice_client.channel != channel: - await voice_client.move_to(channel) - - # Play the TTS audio - if not voice_client.is_playing(): + volume = data.get('volume', 1.0) + + voice_client = await voice_mgr.get_voice_client(channel.guild) + + if voice_client is None: + voice_client = await voice_mgr.join_channel(channel) + + logger.info(f"connected: {voice_client}") + + if voice_client and not voice_client.is_playing(): + logger.info("preparing to play audio") audio_source = discord.FFmpegPCMAudio(tts_file, options=f'-filter:a "volume={volume}"') voice_client.play(audio_source) while voice_client.is_playing(): - await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) + await asyncio.sleep(0.5) if os.path.exists(tts_file): os.remove(tts_file) @@ -96,10 +97,6 @@ class VcLogs(commands.Cog): ) async def volume(self, ctx: discord.ApplicationContext, volume: int): try: - if not os.path.exists(VcLogs.settings_file): - async with aiofiles.open(VcLogs.settings_file, 'w') as f: - json.dump({'volume': '5'}, f, indent=4) - logger.info("settings.json created") async with aiofiles.open(VcLogs.settings_file, 'w') as f: json.dump({"volume": volume}, f, indent=4) await ctx.followup.send(f"Volume has been set to: {volume}") -- cgit v1.2.3