diff options
Diffstat (limited to 'cogs/vclogs.py')
| -rw-r--r-- | cogs/vclogs.py | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/cogs/vclogs.py b/cogs/vclogs.py new file mode 100644 index 0000000..4bc7104 --- /dev/null +++ b/cogs/vclogs.py | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | import datetime | ||
| 2 | import json | ||
| 3 | import os | ||
| 4 | |||
| 5 | import discord | ||
| 6 | from discord.ext import commands | ||
| 7 | from gtts import gTTS | ||
| 8 | |||
| 9 | |||
| 10 | class VcLogs(commands.Cog): | ||
| 11 | def __init__(self, bot): | ||
| 12 | self.bot = bot | ||
| 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 | |||
| 45 | async def play_tts(self, channel, text): | ||
| 46 | # Generate TTS audio | ||
| 47 | tts = gTTS(text=text, lang='pl') | ||
| 48 | tts.save('temp_tts.mp3') | ||
| 49 | |||
| 50 | # Connect to the voice channel if not already connected | ||
| 51 | voice_client = discord.utils.get(self.bot.voice_clients, guild=channel.guild) | ||
| 52 | if not voice_client or not voice_client.is_connected(): | ||
| 53 | voice_client = await channel.connect() | ||
| 54 | elif voice_client.channel != channel: | ||
| 55 | await voice_client.move_to(channel) | ||
| 56 | |||
| 57 | # Play the TTS audio | ||
| 58 | if not voice_client.is_playing(): | ||
| 59 | if os.path.exists('settings.json'): | ||
| 60 | with open('settings.json') as f: | ||
| 61 | data = json.load(f) | ||
| 62 | volume = data['volume'] | ||
| 63 | else: | ||
| 64 | with open('settings.json', 'w') as f: | ||
| 65 | json.dump({'volume': '5'}, f, indent=4) | ||
| 66 | |||
| 67 | audio_source = discord.FFmpegPCMAudio('temp_tts.mp3', options=f'-filter:a "volume={volume}"') | ||
| 68 | voice_client.play(audio_source) | ||
| 69 | while voice_client.is_playing(): | ||
| 70 | await discord.utils.sleep_until(datetime.datetime.now() + datetime.timedelta(seconds=1)) | ||
| 71 | os.remove('temp_tts.mp3') | ||
| 72 | |||
| 73 | @commands.slash_command( | ||
| 74 | name="log_volume" | ||
| 75 | ) | ||
| 76 | async def volume(self, ctx: discord.ApplicationContext, volume: int): | ||
| 77 | try: | ||
| 78 | if not os.path.exists('settings.json'): | ||
| 79 | with open('settings.json', 'w') as f: | ||
| 80 | json.dump({'volume': '5'}, f, indent=4) | ||
| 81 | with open('settings.json', 'w') as f: | ||
| 82 | json.dump({"volume": volume}, f, indent=4) | ||
| 83 | await ctx.send(f"Volume has been set to: {volume}") | ||
| 84 | except Exception as e: | ||
| 85 | await ctx.respond(e, ephemeral=True) | ||
| 86 | print(e) | ||
| 87 | |||
| 88 | @commands.Cog.listener() | ||
| 89 | async def on_voice_state_update(self, member, before, after): | ||
| 90 | log_channel = self.bot.get_channel(1213843195457183885) | ||
| 91 | current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
| 92 | tts_text = None | ||
| 93 | tts_channel = None | ||
| 94 | |||
| 95 | # Ignore if the user is a bot | ||
| 96 | if member.bot: | ||
| 97 | return | ||
| 98 | |||
| 99 | if before.channel is None and after.channel is not None: | ||
| 100 | message = f"[{current_time}] {member.name} dołączył na {after.channel.name}" | ||
| 101 | tts_text = f"{member.display_name} sie zespawnił" | ||
| 102 | tts_channel = after.channel | ||
| 103 | await log_channel.send(message) | ||
| 104 | print(message) | ||
| 105 | elif before.channel is not None and after.channel is None: | ||
| 106 | message = f"[{current_time}] {member.name} rozłączył się z {before.channel.name}" | ||
| 107 | tts_text = f"{member.display_name} jest chujem" | ||
| 108 | tts_channel = before.channel | ||
| 109 | await log_channel.send(message) | ||
| 110 | print(message) | ||
| 111 | |||
| 112 | if before.self_deaf != after.self_deaf: | ||
| 113 | tts_text, tts_channel = await self.handle_state_change( | ||
| 114 | member, after.channel, 'deaf', after.self_deaf, current_time, log_channel | ||
| 115 | ) | ||
| 116 | if tts_text and tts_channel: | ||
| 117 | await self.play_tts(tts_channel, tts_text) | ||
| 118 | return | ||
| 119 | |||
| 120 | if before.self_mute != after.self_mute: | ||
| 121 | tts_text, tts_channel = await self.handle_state_change( | ||
| 122 | member, after.channel, 'mute', after.self_mute, current_time, log_channel | ||
| 123 | ) | ||
| 124 | |||
| 125 | if tts_text and tts_channel: | ||
| 126 | await self.play_tts(tts_channel, tts_text) | ||
| 127 | |||
| 128 | |||
| 129 | def setup(bot): | ||
| 130 | bot.add_cog(VcLogs(bot)) \ No newline at end of file | ||
