aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/vclogs.py
diff options
context:
space:
mode:
Diffstat (limited to 'cogs/vclogs.py')
-rw-r--r--cogs/vclogs.py29
1 files changed, 15 insertions, 14 deletions
diff --git a/cogs/vclogs.py b/cogs/vclogs.py
index 40a618c..bc317a4 100644
--- a/cogs/vclogs.py
+++ b/cogs/vclogs.py
@@ -10,6 +10,7 @@ import os
10import discord 10import discord
11from discord.ext import commands 11from discord.ext import commands
12from gtts import gTTS 12from gtts import gTTS
13import aiofiles
13 14
14logger = logging.getLogger(__name__) 15logger = logging.getLogger(__name__)
15 16
@@ -20,15 +21,14 @@ class VcLogs(commands.Cog):
20 self.bot = bot 21 self.bot = bot
21 self.settings_check() 22 self.settings_check()
22 23
24 settings_file = 'settings.json'
25
23 @staticmethod 26 @staticmethod
24 def settings_check(): 27 def settings_check():
25 if not os.path.exists('settings.json'): 28 if not os.path.exists(settings_file):
26 with open('settings.json', 'w') as f: 29 with open(settings_file, 'w') as f:
27 json.dump({'volume': '5'}, f, indent=4) 30 json.dump({'volume': '5'}, f, indent=4)
28 logger.info("settings.json created") 31 logger.info("settings.json created")
29 with open('settings.json', 'r') as f:
30 settings = json.load(f)
31 volume = settings.get('volume', '5')
32 32
33 @staticmethod 33 @staticmethod
34 async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): 34 async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel):
@@ -62,12 +62,13 @@ class VcLogs(commands.Cog):
62 return tts_text, channel 62 return tts_text, channel
63 63
64 async def play_tts(self, channel, text): 64 async def play_tts(self, channel, text):
65 tts_file = "temp_tts.mp3"
65 # Generate TTS audio 66 # Generate TTS audio
66 tts = gTTS(text=text, lang='pl') 67 tts = gTTS(text=text, lang='pl')
67 tts.save('temp_tts.mp3') 68 tts.save(tts_file)
68 69
69 if os.path.exists('settings.json'): 70 if os.path.exists(settings_file):
70 with open('settings.json') as f: 71 async with aiofiles.open(settings_file) as f:
71 data = json.load(f) 72 data = json.load(f)
72 volume = data['volume'] 73 volume = data['volume']
73 else: 74 else:
@@ -82,23 +83,23 @@ class VcLogs(commands.Cog):
82 83
83 # Play the TTS audio 84 # Play the TTS audio
84 if not voice_client.is_playing(): 85 if not voice_client.is_playing():
85 audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') 86 audio_source = discord.FFmpegPCMAudio(tts_file, options=f'-filter:a "volume={volume}"')
86 voice_client.play(audio_source) 87 voice_client.play(audio_source)
87 while voice_client.is_playing(): 88 while voice_client.is_playing():
88 await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) 89 await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1))
89 if os.path.exists('temp_tts.mp3'): 90 if os.path.exists(tts_file):
90 os.remove('temp_tts.mp3') 91 os.remove(tts_file)
91 92
92 @commands.slash_command( 93 @commands.slash_command(
93 name="log_volume" 94 name="log_volume"
94 ) 95 )
95 async def volume(self, ctx: discord.ApplicationContext, volume: int): 96 async def volume(self, ctx: discord.ApplicationContext, volume: int):
96 try: 97 try:
97 if not os.path.exists('settings.json'): 98 if not os.path.exists(settings_file):
98 with open('settings.json', 'w') as f: 99 async with aiofiles.open(settings_file, 'w') as f:
99 json.dump({'volume': '5'}, f, indent=4) 100 json.dump({'volume': '5'}, f, indent=4)
100 logger.info("settings.json created") 101 logger.info("settings.json created")
101 with open('settings.json', 'w') as f: 102 async with aiofiles.open(settings_file, 'w') as f:
102 json.dump({"volume": volume}, f, indent=4) 103 json.dump({"volume": volume}, f, indent=4)
103 await ctx.followup.send(f"Volume has been set to: {volume}") 104 await ctx.followup.send(f"Volume has been set to: {volume}")
104 logger.info(f"Volume changed to: {volume}") 105 logger.info(f"Volume changed to: {volume}")