From 98727bf749c471a4caf0ca1ad96d067411fdde4f Mon Sep 17 00:00:00 2001 From: salmonik Date: Fri, 27 Feb 2026 20:53:49 +0100 Subject: fix tts --- cogs/tts.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'cogs/tts.py') 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 import discord import aiofiles import logging +import tempfile from discord.ext import commands from gtts import gTTS import asyncio @@ -21,14 +22,13 @@ class TTS(commands.Cog): async def _load_settings(self): """Load volume settings from settings.json""" volume = 1.0 - if os.path.exists('settings.json'): - try: - async with aiofiles.open('settings.json') as f: - content = await f.read() - data = json.loads(content) - volume = data.get('volume', 1.0) - except (json.JSONDecodeError, KeyError) as e: - logger.error(f"Error loading settings: {e}") + try: + async with aiofiles.open('settings.json') as f: + content = await f.read() + data = json.loads(content) + volume = data.get('volume', 1.0) + except (json.JSONDecodeError, KeyError, FileNotFoundError) as e: + logger.error(f"Error loading settings: {e}") return volume async def _handle_file_output(self, ctx, temp_path, text): @@ -96,8 +96,9 @@ class TTS(commands.Cog): ): await ctx.defer() - async with NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: - temp_path = temp_file.name + # Create temporary file path asynchronously + temp_fd, temp_path = tempfile.mkstemp(suffix=".mp3") + os.close(temp_fd) # Close the file descriptor, we'll write to it asynchronously try: # Generate and save TTS to temp file -- cgit v1.2.3