diff options
| author | salmonik <raisersoft@proton.me> | 2026-02-04 16:19:22 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-02-04 16:19:22 +0100 |
| commit | 72abc43db8f732743c65d6f6a820369ab8665766 (patch) | |
| tree | 094fafdb0baf2e06a02045b1b2c0be88931a379a /cogs/vclogs.py | |
| parent | refactor music (diff) | |
| download | senator-sperma-72abc43db8f732743c65d6f6a820369ab8665766.tar.gz senator-sperma-72abc43db8f732743c65d6f6a820369ab8665766.zip | |
more asynchronous work and other minor improvements
Diffstat (limited to 'cogs/vclogs.py')
| -rw-r--r-- | cogs/vclogs.py | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/cogs/vclogs.py b/cogs/vclogs.py index 40a618c..bc317a4 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py | |||
| @@ -10,6 +10,7 @@ import os | |||
| 10 | import discord | 10 | import discord |
| 11 | from discord.ext import commands | 11 | from discord.ext import commands |
| 12 | from gtts import gTTS | 12 | from gtts import gTTS |
| 13 | import aiofiles | ||
| 13 | 14 | ||
| 14 | logger = logging.getLogger(__name__) | 15 | logger = logging.getLogger(__name__) |
| 15 | 16 | ||
| @@ -20,15 +21,14 @@ class VcLogs(commands.Cog): | |||
| 20 | self.bot = bot | 21 | self.bot = bot |
| 21 | self.settings_check() | 22 | self.settings_check() |
| 22 | 23 | ||
| 24 | settings_file = 'settings.json' | ||
| 25 | |||
| 23 | @staticmethod | 26 | @staticmethod |
| 24 | def settings_check(): | 27 | def settings_check(): |
| 25 | if not os.path.exists('settings.json'): | 28 | if not os.path.exists(settings_file): |
| 26 | with open('settings.json', 'w') as f: | 29 | with open(settings_file, 'w') as f: |
| 27 | json.dump({'volume': '5'}, f, indent=4) | 30 | json.dump({'volume': '5'}, f, indent=4) |
| 28 | logger.info("settings.json created") | 31 | logger.info("settings.json created") |
| 29 | with open('settings.json', 'r') as f: | ||
| 30 | settings = json.load(f) | ||
| 31 | volume = settings.get('volume', '5') | ||
| 32 | 32 | ||
| 33 | @staticmethod | 33 | @staticmethod |
| 34 | async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): | 34 | async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): |
| @@ -62,12 +62,13 @@ class VcLogs(commands.Cog): | |||
| 62 | return tts_text, channel | 62 | return tts_text, channel |
| 63 | 63 | ||
| 64 | async def play_tts(self, channel, text): | 64 | async def play_tts(self, channel, text): |
| 65 | tts_file = "temp_tts.mp3" | ||
| 65 | # Generate TTS audio | 66 | # Generate TTS audio |
| 66 | tts = gTTS(text=text, lang='pl') | 67 | tts = gTTS(text=text, lang='pl') |
| 67 | tts.save('temp_tts.mp3') | 68 | tts.save(tts_file) |
| 68 | 69 | ||
| 69 | if os.path.exists('settings.json'): | 70 | if os.path.exists(settings_file): |
| 70 | with open('settings.json') as f: | 71 | async with aiofiles.open(settings_file) as f: |
| 71 | data = json.load(f) | 72 | data = json.load(f) |
| 72 | volume = data['volume'] | 73 | volume = data['volume'] |
| 73 | else: | 74 | else: |
| @@ -82,23 +83,23 @@ class VcLogs(commands.Cog): | |||
| 82 | 83 | ||
| 83 | # Play the TTS audio | 84 | # Play the TTS audio |
| 84 | if not voice_client.is_playing(): | 85 | if not voice_client.is_playing(): |
| 85 | audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') | 86 | audio_source = discord.FFmpegPCMAudio(tts_file, options=f'-filter:a "volume={volume}"') |
| 86 | voice_client.play(audio_source) | 87 | voice_client.play(audio_source) |
| 87 | while voice_client.is_playing(): | 88 | while voice_client.is_playing(): |
| 88 | await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) | 89 | await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) |
| 89 | if os.path.exists('temp_tts.mp3'): | 90 | if os.path.exists(tts_file): |
| 90 | os.remove('temp_tts.mp3') | 91 | os.remove(tts_file) |
| 91 | 92 | ||
| 92 | @commands.slash_command( | 93 | @commands.slash_command( |
| 93 | name="log_volume" | 94 | name="log_volume" |
| 94 | ) | 95 | ) |
| 95 | async def volume(self, ctx: discord.ApplicationContext, volume: int): | 96 | async def volume(self, ctx: discord.ApplicationContext, volume: int): |
| 96 | try: | 97 | try: |
| 97 | if not os.path.exists('settings.json'): | 98 | if not os.path.exists(settings_file): |
| 98 | with open('settings.json', 'w') as f: | 99 | async with aiofiles.open(settings_file, 'w') as f: |
| 99 | json.dump({'volume': '5'}, f, indent=4) | 100 | json.dump({'volume': '5'}, f, indent=4) |
| 100 | logger.info("settings.json created") | 101 | logger.info("settings.json created") |
| 101 | with open('settings.json', 'w') as f: | 102 | async with aiofiles.open(settings_file, 'w') as f: |
| 102 | json.dump({"volume": volume}, f, indent=4) | 103 | json.dump({"volume": volume}, f, indent=4) |
| 103 | await ctx.followup.send(f"Volume has been set to: {volume}") | 104 | await ctx.followup.send(f"Volume has been set to: {volume}") |
| 104 | logger.info(f"Volume changed to: {volume}") | 105 | logger.info(f"Volume changed to: {volume}") |
