diff options
| author | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-13 14:59:22 +0200 |
|---|---|---|
| committer | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-08-13 14:59:22 +0200 |
| commit | 8d0a52ab34563beab33be2dfb207da0e03a8fe32 (patch) | |
| tree | 6d670fe2985259c9c2a69ce80f1b8a0bd1d16d6c /cogs | |
| parent | prevent discord from complaining (diff) | |
| download | senator-sperma-8d0a52ab34563beab33be2dfb207da0e03a8fe32.tar.gz senator-sperma-8d0a52ab34563beab33be2dfb207da0e03a8fe32.zip | |
prevent discord from complaining and bot does not leave vc after playing
Diffstat (limited to 'cogs')
| -rw-r--r-- | cogs/music/play.py | 14 | ||||
| -rw-r--r-- | cogs/music/play_compressed.py | 11 | ||||
| -rw-r--r-- | cogs/tts.py | 14 | ||||
| -rw-r--r-- | cogs/vclogs.py | 29 |
4 files changed, 31 insertions, 37 deletions
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 @@ | |||
| 1 | import asyncio | ||
| 2 | import logging | 1 | import logging |
| 3 | |||
| 4 | import discord | 2 | import discord |
| 5 | import yt_dlp | 3 | import yt_dlp |
| 6 | from discord.ext import commands | 4 | from discord.ext import commands |
| @@ -46,18 +44,8 @@ class Play(commands.Cog): | |||
| 46 | 'options': '-vn' | 44 | 'options': '-vn' |
| 47 | } | 45 | } |
| 48 | 46 | ||
| 49 | def after_playing(error): | ||
| 50 | if error: | ||
| 51 | logger.error(f'Player error: {error}') | ||
| 52 | coro = vc.disconnect() | ||
| 53 | fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop) | ||
| 54 | try: | ||
| 55 | fut.result() | ||
| 56 | except Exception as e: | ||
| 57 | logger.error(f'Error disconnecting: {e}', exc_info=True) | ||
| 58 | |||
| 59 | source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) | 47 | source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) |
| 60 | vc.play(source, after=after_playing) | 48 | vc.play(source) |
| 61 | await ctx.followup.send(f"gra: {title}") | 49 | await ctx.followup.send(f"gra: {title}") |
| 62 | logger.info(f"Playing: {title}") | 50 | logger.info(f"Playing: {title}") |
| 63 | except Exception as e: | 51 | 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): | |||
| 89 | found_file = compressed_file_path | 89 | found_file = compressed_file_path |
| 90 | found_title = title | 90 | found_title = title |
| 91 | title = found_title | 91 | title = found_title |
| 92 | def after_playing(error): | ||
| 93 | if error: | ||
| 94 | logger.error(f'Player error: {error}') | ||
| 95 | coro = vc.disconnect() | ||
| 96 | fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop) | ||
| 97 | try: | ||
| 98 | fut.result() | ||
| 99 | except Exception as e: | ||
| 100 | logger.error(f'Error disconnecting: {e}', exc_info=True) | ||
| 101 | 92 | ||
| 102 | if vc.is_playing() or vc.is_paused(): | 93 | if vc.is_playing() or vc.is_paused(): |
| 103 | vc.stop() | 94 | vc.stop() |
| 104 | vc.play(discord.FFmpegPCMAudio(found_file), after=after_playing) | 95 | vc.play(discord.FFmpegPCMAudio(found_file)) |
| 105 | await ctx.followup.send(f"gra (zkompresowane): {title}") | 96 | await ctx.followup.send(f"gra (zkompresowane): {title}") |
| 106 | logger.info(f"Playing audio: {title}") | 97 | logger.info(f"Playing audio: {title}") |
| 107 | 98 | ||
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 @@ | |||
| 1 | import datetime | 1 | import datetime |
| 2 | import os | 2 | import os |
| 3 | 3 | import json | |
| 4 | import discord | 4 | import discord |
| 5 | from discord.ext import commands | 5 | from discord.ext import commands |
| 6 | from gtts import gTTS | 6 | from gtts import gTTS |
| @@ -20,6 +20,13 @@ class TTS(commands.Cog): | |||
| 20 | tts = gTTS(text=text, lang='pl') | 20 | tts = gTTS(text=text, lang='pl') |
| 21 | tts.save('tts.mp3') | 21 | tts.save('tts.mp3') |
| 22 | 22 | ||
| 23 | if os.path.exists('settings.json'): | ||
| 24 | with open('settings.json') as f: | ||
| 25 | data = json.load(f) | ||
| 26 | volume = data['volume'] | ||
| 27 | else: | ||
| 28 | logger.error("settings.json not found") | ||
| 29 | |||
| 23 | if output == "file": | 30 | if output == "file": |
| 24 | await ctx.respond(file=discord.File('tts.mp3')) | 31 | await ctx.respond(file=discord.File('tts.mp3')) |
| 25 | logger.info("TTS file sent") | 32 | logger.info("TTS file sent") |
| @@ -42,7 +49,7 @@ class TTS(commands.Cog): | |||
| 42 | await voice_client.move_to(channel) | 49 | await voice_client.move_to(channel) |
| 43 | # Play the TTS audio | 50 | # Play the TTS audio |
| 44 | if not voice_client.is_playing(): | 51 | if not voice_client.is_playing(): |
| 45 | audio_source = discord.FFmpegPCMAudio('tts.mp3', options='-filter:a "volume=6"') | 52 | audio_source = discord.FFmpegPCMAudio('tts.mp3', options=f'-filter:a "volume={volume}"') |
| 46 | logger.info("TTS audio played") | 53 | logger.info("TTS audio played") |
| 47 | voice_client.play(audio_source) | 54 | voice_client.play(audio_source) |
| 48 | # Wait for playback to finish | 55 | # Wait for playback to finish |
| @@ -55,5 +62,4 @@ class TTS(commands.Cog): | |||
| 55 | os.remove('tts.mp3') | 62 | os.remove('tts.mp3') |
| 56 | 63 | ||
| 57 | def setup(bot): | 64 | def setup(bot): |
| 58 | bot.add_cog(TTS(bot)) | 65 | bot.add_cog(TTS(bot)) \ No newline at end of file |
| 59 | 1 \ 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 | |||
| 2 | import json | 2 | import json |
| 3 | import logging | 3 | import logging |
| 4 | import os | 4 | import os |
| 5 | |||
| 6 | import discord | 5 | import discord |
| 7 | from discord.ext import commands | 6 | from discord.ext import commands |
| 8 | from gtts import gTTS | 7 | from gtts import gTTS |
| @@ -10,9 +9,21 @@ from gtts import gTTS | |||
| 10 | logger = logging.getLogger(__name__) | 9 | logger = logging.getLogger(__name__) |
| 11 | 10 | ||
| 12 | 11 | ||
| 12 | |||
| 13 | class VcLogs(commands.Cog): | 13 | class VcLogs(commands.Cog): |
| 14 | def __init__(self, bot): | 14 | def __init__(self, bot): |
| 15 | self.bot = bot | 15 | self.bot = bot |
| 16 | self.settings_check() | ||
| 17 | |||
| 18 | @staticmethod | ||
| 19 | def settings_check(): | ||
| 20 | if not os.path.exists('settings.json'): | ||
| 21 | with open('settings.json', 'w') as f: | ||
| 22 | json.dump({'volume': '5'}, f, indent=4) | ||
| 23 | logger.info("settings.json created") | ||
| 24 | with open('settings.json', 'r') as f: | ||
| 25 | settings = json.load(f) | ||
| 26 | volume = settings.get('volume', '5') | ||
| 16 | 27 | ||
| 17 | @staticmethod | 28 | @staticmethod |
| 18 | async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): | 29 | async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): |
| @@ -50,6 +61,13 @@ class VcLogs(commands.Cog): | |||
| 50 | tts = gTTS(text=text, lang='pl') | 61 | tts = gTTS(text=text, lang='pl') |
| 51 | tts.save('temp_tts.mp3') | 62 | tts.save('temp_tts.mp3') |
| 52 | 63 | ||
| 64 | if os.path.exists('settings.json'): | ||
| 65 | with open('settings.json') as f: | ||
| 66 | data = json.load(f) | ||
| 67 | volume = data['volume'] | ||
| 68 | else: | ||
| 69 | logger.error("settings.json not found") | ||
| 70 | |||
| 53 | # Connect to the voice channel if not already connected | 71 | # Connect to the voice channel if not already connected |
| 54 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) | 72 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) |
| 55 | if not voice_client or not voice_client.is_connected(): | 73 | if not voice_client or not voice_client.is_connected(): |
| @@ -59,15 +77,6 @@ class VcLogs(commands.Cog): | |||
| 59 | 77 | ||
| 60 | # Play the TTS audio | 78 | # Play the TTS audio |
| 61 | if not voice_client.is_playing(): | 79 | if not voice_client.is_playing(): |
| 62 | if os.path.exists('settings.json'): | ||
| 63 | with open('settings.json') as f: | ||
| 64 | data = json.load(f) | ||
| 65 | volume = data['volume'] | ||
| 66 | else: | ||
| 67 | with open('settings.json', 'w') as f: | ||
| 68 | json.dump({'volume': '5'}, f, indent=4) | ||
| 69 | logger.info("settings.json created") | ||
| 70 | |||
| 71 | audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') | 80 | audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') |
| 72 | voice_client.play(audio_source) | 81 | voice_client.play(audio_source) |
| 73 | while voice_client.is_playing(): | 82 | while voice_client.is_playing(): |
