diff options
Diffstat (limited to 'cogs/tts.py')
| -rw-r--r-- | cogs/tts.py | 21 |
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 | |||
| 8 | import discord | 8 | import discord |
| 9 | import aiofiles | 9 | import aiofiles |
| 10 | import logging | 10 | import logging |
| 11 | import tempfile | ||
| 11 | from discord.ext import commands | 12 | from discord.ext import commands |
| 12 | from gtts import gTTS | 13 | from gtts import gTTS |
| 13 | import asyncio | 14 | import 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 |
