From 8d0a52ab34563beab33be2dfb207da0e03a8fe32 Mon Sep 17 00:00:00 2001 From: Samlon Date: Wed, 13 Aug 2025 14:59:22 +0200 Subject: prevent discord from complaining and bot does not leave vc after playing --- cogs/music/play.py | 14 +------------- cogs/music/play_compressed.py | 11 +---------- cogs/tts.py | 14 ++++++++++---- cogs/vclogs.py | 29 +++++++++++++++++++---------- 4 files changed, 31 insertions(+), 37 deletions(-) (limited to 'cogs') diff --git a/cogs/music/play.py b/cogs/music/play.py index cf04ded..2314545 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py @@ -1,6 +1,4 @@ -import asyncio import logging - import discord import yt_dlp from discord.ext import commands @@ -46,18 +44,8 @@ class Play(commands.Cog): 'options': '-vn' } - def after_playing(error): - if error: - logger.error(f'Player error: {error}') - coro = vc.disconnect() - fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop) - try: - fut.result() - except Exception as e: - logger.error(f'Error disconnecting: {e}', exc_info=True) - source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) - vc.play(source, after=after_playing) + vc.play(source) await ctx.followup.send(f"gra: {title}") logger.info(f"Playing: {title}") except Exception as e: diff --git a/cogs/music/play_compressed.py b/cogs/music/play_compressed.py index dd8dfd8..2bae269 100644 --- a/cogs/music/play_compressed.py +++ b/cogs/music/play_compressed.py @@ -89,19 +89,10 @@ class GrajcujZkompresowane(commands.Cog): found_file = compressed_file_path found_title = title title = found_title - def after_playing(error): - if error: - logger.error(f'Player error: {error}') - coro = vc.disconnect() - fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop) - try: - fut.result() - except Exception as e: - logger.error(f'Error disconnecting: {e}', exc_info=True) if vc.is_playing() or vc.is_paused(): vc.stop() - vc.play(discord.FFmpegPCMAudio(found_file), after=after_playing) + vc.play(discord.FFmpegPCMAudio(found_file)) await ctx.followup.send(f"gra (zkompresowane): {title}") logger.info(f"Playing audio: {title}") diff --git a/cogs/tts.py b/cogs/tts.py index c00259f..7461a1f 100644 --- a/cogs/tts.py +++ b/cogs/tts.py @@ -1,6 +1,6 @@ import datetime import os - +import json import discord from discord.ext import commands from gtts import gTTS @@ -20,6 +20,13 @@ class TTS(commands.Cog): tts = gTTS(text=text, lang='pl') tts.save('tts.mp3') + if os.path.exists('settings.json'): + with open('settings.json') as f: + data = json.load(f) + volume = data['volume'] + else: + logger.error("settings.json not found") + if output == "file": await ctx.respond(file=discord.File('tts.mp3')) logger.info("TTS file sent") @@ -42,7 +49,7 @@ class TTS(commands.Cog): await voice_client.move_to(channel) # Play the TTS audio if not voice_client.is_playing(): - audio_source = discord.FFmpegPCMAudio('tts.mp3', options='-filter:a "volume=6"') + audio_source = discord.FFmpegPCMAudio('tts.mp3', options=f'-filter:a "volume={volume}"') logger.info("TTS audio played") voice_client.play(audio_source) # Wait for playback to finish @@ -55,5 +62,4 @@ class TTS(commands.Cog): os.remove('tts.mp3') def setup(bot): - bot.add_cog(TTS(bot)) -1 \ No newline at end of file + bot.add_cog(TTS(bot)) \ No newline at end of file diff --git a/cogs/vclogs.py b/cogs/vclogs.py index 50c3fa7..9e74877 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py @@ -2,7 +2,6 @@ import datetime import json import logging import os - import discord from discord.ext import commands from gtts import gTTS @@ -10,9 +9,21 @@ from gtts import gTTS logger = logging.getLogger(__name__) + class VcLogs(commands.Cog): def __init__(self, bot): self.bot = bot + self.settings_check() + + @staticmethod + def settings_check(): + if not os.path.exists('settings.json'): + with open('settings.json', 'w') as f: + json.dump({'volume': '5'}, f, indent=4) + logger.info("settings.json created") + with open('settings.json', 'r') as f: + settings = json.load(f) + volume = settings.get('volume', '5') @staticmethod async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): @@ -50,6 +61,13 @@ class VcLogs(commands.Cog): tts = gTTS(text=text, lang='pl') tts.save('temp_tts.mp3') + if os.path.exists('settings.json'): + with open('settings.json') as f: + data = json.load(f) + volume = data['volume'] + else: + logger.error("settings.json not found") + # Connect to the voice channel if not already connected voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) if not voice_client or not voice_client.is_connected(): @@ -59,15 +77,6 @@ class VcLogs(commands.Cog): # Play the TTS audio if not voice_client.is_playing(): - if os.path.exists('settings.json'): - with open('settings.json') as f: - data = json.load(f) - volume = data['volume'] - else: - with open('settings.json', 'w') as f: - json.dump({'volume': '5'}, f, indent=4) - logger.info("settings.json created") - audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') voice_client.play(audio_source) while voice_client.is_playing(): -- cgit v1.2.3