aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cogs/tts.py21
1 files changed, 11 insertions, 10 deletions
diff --git a/cogs/tts.py b/cogs/tts.py
index 44a976f..a330b74 100644
--- a/cogs/tts.py
+++ b/cogs/tts.py
@@ -8,6 +8,7 @@ import json
8import discord 8import discord
9import aiofiles 9import aiofiles
10import logging 10import logging
11import tempfile
11from discord.ext import commands 12from discord.ext import commands
12from gtts import gTTS 13from gtts import gTTS
13import asyncio 14import asyncio
@@ -21,14 +22,13 @@ class TTS(commands.Cog):
21 async def _load_settings(self): 22 async def _load_settings(self):
22 """Load volume settings from settings.json""" 23 """Load volume settings from settings.json"""
23 volume = 1.0 24 volume = 1.0
24 if os.path.exists('settings.json'): 25 try:
25 try: 26 async with aiofiles.open('settings.json') as f:
26 async with aiofiles.open('settings.json') as f: 27 content = await f.read()
27 content = await f.read() 28 data = json.loads(content)
28 data = json.loads(content) 29 volume = data.get('volume', 1.0)
29 volume = data.get('volume', 1.0) 30 except (json.JSONDecodeError, KeyError, FileNotFoundError) as e:
30 except (json.JSONDecodeError, KeyError) as e: 31 logger.error(f"Error loading settings: {e}")
31 logger.error(f"Error loading settings: {e}")
32 return volume 32 return volume
33 33
34 async def _handle_file_output(self, ctx, temp_path, text): 34 async def _handle_file_output(self, ctx, temp_path, text):
@@ -96,8 +96,9 @@ class TTS(commands.Cog):
96 ): 96 ):
97 await ctx.defer() 97 await ctx.defer()
98 98
99 async with NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: 99 # Create temporary file path asynchronously
100 temp_path = temp_file.name 100 temp_fd, temp_path = tempfile.mkstemp(suffix=".mp3")
101 os.close(temp_fd) # Close the file descriptor, we'll write to it asynchronously
101 102
102 try: 103 try:
103 # Generate and save TTS to temp file 104 # Generate and save TTS to temp file