From c0d5dd52499db8f35943b270c2e8a5d21afe099f Mon Sep 17 00:00:00 2001 From: Xargana Date: Fri, 9 May 2025 21:53:48 +0300 Subject: [PATCH] added channel/user management commands --- bot/commands/user_management_commands.py | 142 +++++++++++++++++++++++ bot/handlers/message_handler.py | 42 ++++++- bot/selfbot.py | 7 ++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 bot/commands/user_management_commands.py diff --git a/bot/commands/user_management_commands.py b/bot/commands/user_management_commands.py new file mode 100644 index 0000000..3410eee --- /dev/null +++ b/bot/commands/user_management_commands.py @@ -0,0 +1,142 @@ +import discord +import asyncio + +class UserManagementCommands: + def __init__(self, bot): + self.bot = bot + # Store lists of ignored/muted users and channels + self.ignored_channels = set() + self.muted_channels = set() + self.muted_users = set() + + async def cmd_ignore(self, message): + """ + Ignore all users in the current channel except for self + Usage: .ignore + """ + try: + channel_id = message.channel.id + self.ignored_channels.add(channel_id) + await message.edit(content=f"✅ Ignoring users in this channel. Use `.unignore` to revert.") + except Exception as e: + await message.edit(content=f"❌ Error ignoring channel: {str(e)}") + + async def cmd_unignore(self, message): + """ + Stop ignoring users in the current channel + Usage: .unignore + """ + try: + channel_id = message.channel.id + if channel_id in self.ignored_channels: + self.ignored_channels.remove(channel_id) + await message.edit(content=f"✅ No longer ignoring users in this channel.") + else: + await message.edit(content=f"❌ This channel is not being ignored.") + except Exception as e: + await message.edit(content=f"❌ Error: {str(e)}") + + async def cmd_close(self, message): + """ + Close the DM channel + Usage: .close + """ + try: + # Only works in DM channels + if isinstance(message.channel, discord.DMChannel): + await message.edit(content="✅ Closing DM...") + await asyncio.sleep(1) # Give a second for the message to be seen + await message.channel.close() + else: + await message.edit(content="❌ This command only works in DM channels.") + except Exception as e: + await message.edit(content=f"❌ Error closing DM: {str(e)}") + + async def cmd_mute(self, message): + """ + Mute the current channel (no notifications) + Usage: .mute + """ + try: + channel_id = message.channel.id + self.muted_channels.add(channel_id) + await message.edit(content=f"✅ Channel muted. Use `.unmute` to revert.") + except Exception as e: + await message.edit(content=f"❌ Error muting channel: {str(e)}") + + async def cmd_unmute(self, message): + """ + Unmute the current channel + Usage: .unmute + """ + try: + channel_id = message.channel.id + if channel_id in self.muted_channels: + self.muted_channels.remove(channel_id) + await message.edit(content=f"✅ Channel unmuted.") + else: + await message.edit(content=f"❌ This channel is not muted.") + except Exception as e: + await message.edit(content=f"❌ Error: {str(e)}") + + async def cmd_block(self, message): + """ + Block a user + Usage: .block @user or .block [in reply to a message] + """ + try: + # Check if we have a mention or if it's a reply + if message.mentions: + user = message.mentions[0] + elif message.reference and message.reference.resolved: + user = message.reference.resolved.author + else: + await message.edit(content="❌ Usage: `.block @user` or reply to a message with `.block`") + return + + if user == self.bot.user: + await message.edit(content="❌ Cannot block yourself.") + return + + await user.block() + await message.edit(content=f"✅ Blocked user {user.name}#{user.discriminator}.") + except Exception as e: + await message.edit(content=f"❌ Error blocking user: {str(e)}") + + async def cmd_unblock(self, message): + """ + Unblock a user + Usage: .unblock @user or user_id + """ + try: + content = message.content.strip() + + # Check if we have a mention + if message.mentions: + user = message.mentions[0] + await user.unblock() + await message.edit(content=f"✅ Unblocked user {user.name}#{user.discriminator}.") + return + + # Try to get user by ID + parts = content.split() + if len(parts) >= 2: + try: + user_id = int(parts[1]) + user = await self.bot.fetch_user(user_id) + await user.unblock() + await message.edit(content=f"✅ Unblocked user {user.name}#{user.discriminator}.") + except: + await message.edit(content="❌ Could not find user with that ID.") + else: + await message.edit(content="❌ Usage: `.unblock @user` or `.unblock user_id`") + except Exception as e: + await message.edit(content=f"❌ Error unblocking user: {str(e)}") + + # Method to check if a channel is ignored + def is_channel_ignored(self, channel_id): + return channel_id in self.ignored_channels + + # Method to check if a channel is muted + def is_channel_muted(self, channel_id): + return channel_id in self.muted_channels \ No newline at end of file diff --git a/bot/handlers/message_handler.py b/bot/handlers/message_handler.py index 1beee69..d0f0203 100644 --- a/bot/handlers/message_handler.py +++ b/bot/handlers/message_handler.py @@ -8,6 +8,7 @@ from bot.commands.utility_commands import UtilityCommands from bot.commands.fun_commands import FunCommands from bot.commands.admin_commands import AdminCommands from bot.commands.test_commands import TestCommands +from bot.commands.user_management_commands import UserManagementCommands from utils.time_parser import parse_time class MessageHandler: @@ -19,6 +20,7 @@ class MessageHandler: self.fun_commands = FunCommands(bot) self.admin_commands = AdminCommands(bot) self.test_commands = TestCommands(bot) + self.user_management_commands = UserManagementCommands(bot) # Attach command handlers to the bot for easier access from tests bot.afk_commands = self.afk_commands @@ -26,6 +28,7 @@ class MessageHandler: bot.fun_commands = self.fun_commands bot.admin_commands = self.admin_commands bot.test_commands = self.test_commands + bot.user_management_commands = self.user_management_commands # Regex for detecting "in X time" patterns self.time_pattern = re.compile( @@ -77,6 +80,12 @@ class MessageHandler: if message.author.bot: return + # Check if the channel is in ignored channels list and message is not from bot user + if (message.author != self.bot.user and + hasattr(self, 'user_management_commands') and + self.user_management_commands.is_channel_ignored(message.channel.id)): + return + # Look for and replace time patterns (only for non-command messages) if not message.content.startswith('.'): original_content = message.content @@ -131,8 +140,39 @@ class MessageHandler: """Handle commands issued by the bot user""" content = message.content + # Check for custom commands first + if content.startswith('.'): + cmd = content.split()[0][1:] # Remove the '.' and get the command name + if hasattr(self.bot, 'loaded_commands') and cmd in self.bot.loaded_commands: + # Execute the custom command + try: + # Extract arguments from the command + args = content.split(' ')[1:] if ' ' in content else [] + await self.bot.loaded_commands[cmd](self.bot, message, args) + return + except Exception as e: + print(f"Error executing custom command {cmd}: {e}") + await message.channel.send(f"Error executing command: {e}") + return + + # User Management Commands + if content.startswith(".ignore"): + await self.user_management_commands.cmd_ignore(message) + elif content.startswith(".unignore"): + await self.user_management_commands.cmd_unignore(message) + elif content.startswith(".close"): + await self.user_management_commands.cmd_close(message) + elif content.startswith(".mute"): + await self.user_management_commands.cmd_mute(message) + elif content.startswith(".unmute"): + await self.user_management_commands.cmd_unmute(message) + elif content.startswith(".block"): + await self.user_management_commands.cmd_block(message) + elif content.startswith(".unblock"): + await self.user_management_commands.cmd_unblock(message) + # AFK Commands - if content.startswith(".afk"): + elif content.startswith(".afk"): await self.afk_commands.cmd_afk(message) elif content.startswith(".unafk"): await self.afk_commands.cmd_unafk(message) diff --git a/bot/selfbot.py b/bot/selfbot.py index 40b4b65..a044c6b 100644 --- a/bot/selfbot.py +++ b/bot/selfbot.py @@ -25,6 +25,13 @@ class Selfbot(discord.Client): print(f"Logged in as {self.user}") async def on_message(self, message): + # Skip notifications for muted channels + if (message.channel.id in self.user_management_commands.muted_channels and + message.author != self.user): + # Still process the message, but don't trigger notifications + pass + + # Regular message handling await self.message_handler.handle_message(message) async def on_message_delete(self, message):