aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/vclogs.py
diff options
context:
space:
mode:
Diffstat (limited to 'cogs/vclogs.py')
-rw-r--r--cogs/vclogs.py55
1 files changed, 26 insertions, 29 deletions
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
7import json 7import json
8import logging 8import logging
9import os 9import os
10import asyncio
10import discord 11import discord
11from discord.ext import commands 12from discord.ext import commands
12from gtts import gTTS 13from gtts import gTTS
13import aiofiles 14import aiofiles
15from cogs.voice import get_voice_manager
14 16
15logger = logging.getLogger(__name__) 17logger = logging.getLogger(__name__)
16 18
17 19
18
19class VcLogs(commands.Cog): 20class VcLogs(commands.Cog):
20 def __init__(self, bot): 21 def __init__(self, bot):
21 self.bot = bot 22 self.bot = bot
22 self.settings_check() 23 self._voice_manager = None
23 24
24 settings_file = 'settings.json' 25 def get_voice_manager(self):
26 if self._voice_manager is None:
27 self._voice_manager = get_voice_manager(self.bot)
28 return self._voice_manager
25 29
26 @staticmethod 30 settings_file = 'settings.json'
27 def settings_check():
28 if not os.path.exists(VcLogs.settings_file):
29 with open(VcLogs.settings_file, 'w') as f:
30 json.dump({'volume': '5'}, f, indent=4)
31 logger.info("settings.json created")
32 31
33 @staticmethod 32 @staticmethod
34 async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): 33 async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel):
@@ -62,32 +61,34 @@ class VcLogs(commands.Cog):
62 return tts_text, channel 61 return tts_text, channel
63 62
64 async def play_tts(self, channel, text): 63 async def play_tts(self, channel, text):
65 tts_file = "temp_tts.mp3" 64 voice_mgr = self.get_voice_manager()
66 # Generate TTS audio 65
66 temp_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'temp')
67 os.makedirs(temp_dir, exist_ok=True)
68 tts_file = os.path.join(temp_dir, f"temp_tts_{os.getpid()}.mp3")
67 tts = gTTS(text=text, lang='pl') 69 tts = gTTS(text=text, lang='pl')
70 logger.info("saving file")
68 tts.save(tts_file) 71 tts.save(tts_file)
69 72
70 if os.path.exists(VcLogs.settings_file): 73 if os.path.exists(VcLogs.settings_file):
71 async with aiofiles.open(VcLogs.settings_file) as f: 74 async with aiofiles.open(VcLogs.settings_file) as f:
72 content = await f.read() 75 content = await f.read()
73 data = json.loads(content) 76 data = json.loads(content)
74 volume = data['volume'] 77 volume = data.get('volume', 1.0)
75 else: 78
76 logger.error("settings.json not found") 79 voice_client = await voice_mgr.get_voice_client(channel.guild)
77 80
78 # Connect to the voice channel if not already connected 81 if voice_client is None:
79 voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) 82 voice_client = await voice_mgr.join_channel(channel)
80 if not isinstance(voice_client, discord.VoiceClient): 83
81 voice_client = await channel.connect() 84 logger.info(f"connected: {voice_client}")
82 elif voice_client.channel != channel: 85
83 await voice_client.move_to(channel) 86 if voice_client and not voice_client.is_playing():
84 87 logger.info("preparing to play audio")
85 # Play the TTS audio
86 if not voice_client.is_playing():
87 audio_source = discord.FFmpegPCMAudio(tts_file, options=f'-filter:a "volume={volume}"') 88 audio_source = discord.FFmpegPCMAudio(tts_file, options=f'-filter:a "volume={volume}"')
88 voice_client.play(audio_source) 89 voice_client.play(audio_source)
89 while voice_client.is_playing(): 90 while voice_client.is_playing():
90 await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) 91 await asyncio.sleep(0.5)
91 if os.path.exists(tts_file): 92 if os.path.exists(tts_file):
92 os.remove(tts_file) 93 os.remove(tts_file)
93 94
@@ -96,10 +97,6 @@ class VcLogs(commands.Cog):
96 ) 97 )
97 async def volume(self, ctx: discord.ApplicationContext, volume: int): 98 async def volume(self, ctx: discord.ApplicationContext, volume: int):
98 try: 99 try:
99 if not os.path.exists(VcLogs.settings_file):
100 async with aiofiles.open(VcLogs.settings_file, 'w') as f:
101 json.dump({'volume': '5'}, f, indent=4)
102 logger.info("settings.json created")
103 async with aiofiles.open(VcLogs.settings_file, 'w') as f: 100 async with aiofiles.open(VcLogs.settings_file, 'w') as f:
104 json.dump({"volume": volume}, f, indent=4) 101 json.dump({"volume": volume}, f, indent=4)
105 await ctx.followup.send(f"Volume has been set to: {volume}") 102 await ctx.followup.send(f"Volume has been set to: {volume}")