diff options
| author | SamlonDev <stankiewiczgracjan2019@gmail.com> | 2025-06-14 18:54:02 +0200 |
|---|---|---|
| committer | SamlonDev <stankiewiczgracjan2019@gmail.com> | 2025-06-14 18:54:02 +0200 |
| commit | 791e75cb2beeca45a6edf52b65f7e299e03b5eaf (patch) | |
| tree | cfee93211b7f8ac1c985325aaf8d9399b7a357ca /cogs/vclogs.py | |
| parent | temp downloads the files instead (diff) | |
| download | senator-sperma-791e75cb2beeca45a6edf52b65f7e299e03b5eaf.tar.gz senator-sperma-791e75cb2beeca45a6edf52b65f7e299e03b5eaf.zip | |
code-style issues
Diffstat (limited to '')
| -rw-r--r-- | cogs/vclogs.py (renamed from cogs/vc_logs.py) | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/cogs/vc_logs.py b/cogs/vclogs.py index e4a8454..4bc7104 100644 --- a/cogs/vc_logs.py +++ b/cogs/vclogs.py | |||
| @@ -1,27 +1,59 @@ | |||
| 1 | import datetime | ||
| 2 | import json | ||
| 3 | import os | ||
| 1 | 4 | ||
| 2 | import discord | 5 | import discord |
| 3 | from discord.ext import commands | 6 | from discord.ext import commands |
| 4 | import datetime | ||
| 5 | from gtts import gTTS | 7 | from gtts import gTTS |
| 6 | import os | ||
| 7 | import json | ||
| 8 | 8 | ||
| 9 | class vc_logs(commands.Cog): | 9 | |
| 10 | class VcLogs(commands.Cog): | ||
| 10 | def __init__(self, bot): | 11 | def __init__(self, bot): |
| 11 | self.bot = bot | 12 | self.bot = bot |
| 12 | 13 | ||
| 14 | @staticmethod | ||
| 15 | async def handle_state_change(member, channel, state_type, is_enabled, current_time, log_channel): | ||
| 16 | """Helper method to handle voice state changes""" | ||
| 17 | state_messages = { | ||
| 18 | 'deaf': { | ||
| 19 | True: ( | ||
| 20 | f"[{current_time}] {member.name} ogłuchnął się na {channel.name}", | ||
| 21 | f"{member.display_name} jest bity przez ojca" | ||
| 22 | ), | ||
| 23 | False: ( | ||
| 24 | f"[{current_time}] {member.name} odgłuchał się na {channel.name}", | ||
| 25 | f"{member.display_name} uciek od ojca" | ||
| 26 | ) | ||
| 27 | }, | ||
| 28 | 'mute': { | ||
| 29 | True: ( | ||
| 30 | f"[{current_time}] {member.name} wyciszył się na {channel.name}", | ||
| 31 | f"{member.display_name} idzie robić jajecznice" | ||
| 32 | ), | ||
| 33 | False: ( | ||
| 34 | f"[{current_time}] {member.name} odciszył się na {channel.name}", | ||
| 35 | f"{member.display_name} zrobił jajecznice" | ||
| 36 | ) | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | message, tts_text = state_messages[state_type][is_enabled] | ||
| 41 | await log_channel.send(message) | ||
| 42 | print(message) | ||
| 43 | return tts_text, channel | ||
| 44 | |||
| 13 | async def play_tts(self, channel, text): | 45 | async def play_tts(self, channel, text): |
| 14 | # Generate TTS audio | 46 | # Generate TTS audio |
| 15 | tts = gTTS(text=text, lang='pl') | 47 | tts = gTTS(text=text, lang='pl') |
| 16 | tts.save('temp_tts.mp3') | 48 | tts.save('temp_tts.mp3') |
| 17 | 49 | ||
| 18 | # Connect to voice channel if not already connected | 50 | # Connect to the voice channel if not already connected |
| 19 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) | 51 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) |
| 20 | if not voice_client or not voice_client.is_connected(): | 52 | if not voice_client or not voice_client.is_connected(): |
| 21 | voice_client = await channel.connect() | 53 | voice_client = await channel.connect() |
| 22 | elif voice_client.channel != channel: | 54 | elif voice_client.channel != channel: |
| 23 | await voice_client.move_to(channel) | 55 | await voice_client.move_to(channel) |
| 24 | 56 | ||
| 25 | # Play the TTS audio | 57 | # Play the TTS audio |
| 26 | if not voice_client.is_playing(): | 58 | if not voice_client.is_playing(): |
| 27 | if os.path.exists('settings.json'): | 59 | if os.path.exists('settings.json'): |
| @@ -31,7 +63,7 @@ class vc_logs(commands.Cog): | |||
| 31 | else: | 63 | else: |
| 32 | with open('settings.json', 'w') as f: | 64 | with open('settings.json', 'w') as f: |
| 33 | json.dump({'volume': '5'}, f, indent=4) | 65 | json.dump({'volume': '5'}, f, indent=4) |
| 34 | 66 | ||
| 35 | audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') | 67 | audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') |
| 36 | voice_client.play(audio_source) | 68 | voice_client.play(audio_source) |
| 37 | while voice_client.is_playing(): | 69 | while voice_client.is_playing(): |
| @@ -52,8 +84,6 @@ class vc_logs(commands.Cog): | |||
| 52 | except Exception as e: | 84 | except Exception as e: |
| 53 | await ctx.respond(e, ephemeral=True) | 85 | await ctx.respond(e, ephemeral=True) |
| 54 | print(e) | 86 | print(e) |
| 55 | |||
| 56 | |||
| 57 | 87 | ||
| 58 | @commands.Cog.listener() | 88 | @commands.Cog.listener() |
| 59 | async def on_voice_state_update(self, member, before, after): | 89 | async def on_voice_state_update(self, member, before, after): |
| @@ -61,9 +91,11 @@ class vc_logs(commands.Cog): | |||
| 61 | current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | 91 | current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 62 | tts_text = None | 92 | tts_text = None |
| 63 | tts_channel = None | 93 | tts_channel = None |
| 64 | # Ignore if user is a bot | 94 | |
| 95 | # Ignore if the user is a bot | ||
| 65 | if member.bot: | 96 | if member.bot: |
| 66 | return | 97 | return |
| 98 | |||
| 67 | if before.channel is None and after.channel is not None: | 99 | if before.channel is None and after.channel is not None: |
| 68 | message = f"[{current_time}] {member.name} dołączył na {after.channel.name}" | 100 | message = f"[{current_time}] {member.name} dołączył na {after.channel.name}" |
| 69 | tts_text = f"{member.display_name} sie zespawnił" | 101 | tts_text = f"{member.display_name} sie zespawnił" |
| @@ -76,35 +108,23 @@ class vc_logs(commands.Cog): | |||
| 76 | tts_channel = before.channel | 108 | tts_channel = before.channel |
| 77 | await log_channel.send(message) | 109 | await log_channel.send(message) |
| 78 | print(message) | 110 | print(message) |
| 79 | 111 | ||
| 80 | if before.self_deaf != after.self_deaf: | 112 | if before.self_deaf != after.self_deaf: |
| 81 | if after.self_deaf: | 113 | tts_text, tts_channel = await self.handle_state_change( |
| 82 | message = f"[{current_time}] {member.name} ogłuchnął się na {after.channel.name}" | 114 | member, after.channel, 'deaf', after.self_deaf, current_time, log_channel |
| 83 | tts_text = f"{member.display_name} jest bity przez ojca" | 115 | ) |
| 84 | else: | ||
| 85 | message = f"[{current_time}] {member.name} odgłuchął się na {after.channel.name}" | ||
| 86 | tts_text = f"{member.display_name} uciek od ojca" | ||
| 87 | tts_channel = after.channel | ||
| 88 | await log_channel.send(message) | ||
| 89 | print(message) | ||
| 90 | if tts_text and tts_channel: | 116 | if tts_text and tts_channel: |
| 91 | await self.play_tts(tts_channel, tts_text) | 117 | await self.play_tts(tts_channel, tts_text) |
| 92 | return | 118 | return |
| 93 | 119 | ||
| 94 | if before.self_mute != after.self_mute: | 120 | if before.self_mute != after.self_mute: |
| 95 | if after.self_mute: | 121 | tts_text, tts_channel = await self.handle_state_change( |
| 96 | message = f"[{current_time}] {member.name} wyciszył się na {after.channel.name}" | 122 | member, after.channel, 'mute', after.self_mute, current_time, log_channel |
| 97 | tts_text = f"{member.display_name} idzie robić jajecznice" | 123 | ) |
| 98 | else: | 124 | |
| 99 | message = f"[{current_time}] {member.name} odciszył się na {after.channel.name}" | ||
| 100 | tts_text = f"{member.display_name} zrobił jajecznice" | ||
| 101 | tts_channel = after.channel | ||
| 102 | await log_channel.send(message) | ||
| 103 | print(message) | ||
| 104 | |||
| 105 | if tts_text and tts_channel: | 125 | if tts_text and tts_channel: |
| 106 | await self.play_tts(tts_channel, tts_text) | 126 | await self.play_tts(tts_channel, tts_text) |
| 107 | 127 | ||
| 108 | 128 | ||
| 109 | def setup(bot): | 129 | def setup(bot): |
| 110 | bot.add_cog(vc_logs(bot)) \ No newline at end of file | 130 | bot.add_cog(VcLogs(bot)) \ No newline at end of file |
