i left skid marks in the code

This commit is contained in:
Xargana 2025-05-09 22:43:55 +03:00
parent fbfa27c979
commit 282ca4374a
13 changed files with 942 additions and 523 deletions

View file

@ -1 +1 @@
# Empty init to make directory a package
# This makes the handlers directory a proper Python package# Empty init to make directory a package

View file

@ -3,39 +3,17 @@ import asyncio
import re
import time
from config import BLACKLISTED_USERS, BUCKET_REACT_USERS, AUTO_DELETE_USERS, SPECIAL_RESPONSES
from bot.commands.afk_commands import AfkCommands
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:
def __init__(self, bot):
self.bot = bot
# Initialize command handlers
self.afk_commands = AfkCommands(bot)
self.utility_commands = UtilityCommands(bot)
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
bot.utility_commands = self.utility_commands
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(
r'in\s+((?:\d+\s*(?:seconds?|minutes?|hours?|days?|s|m|h|d)\s*)+)',
re.IGNORECASE
)
def parse_relative_time(self, time_str):
"""
@ -79,25 +57,28 @@ class MessageHandler:
# Skip bot messages
if message.author.bot:
return
# Skip command messages (they start with . usually)
if message.content.startswith('.'):
return
# Look for and replace time patterns
original_content = message.content
modified_content = self.replace_time_patterns(original_content)
# Look for and replace time patterns (only for non-command messages)
if not message.content.startswith('.'):
original_content = message.content
modified_content = self.replace_time_patterns(original_content)
# If the content was modified, edit the original message
if modified_content != original_content:
try:
await message.edit(content=modified_content)
except Exception as e:
# If we don't have permission to edit, just ignore
pass
# If the content was modified, edit the original message
if modified_content != original_content and message.author == self.bot.user:
try:
await message.edit(content=modified_content)
except Exception as e:
# If we don't have permission to edit, just ignore
pass
# Handle special responses
for user_id, data in SPECIAL_RESPONSES.items():
if message.author.id == user_id and data["trigger"] in message.content:
await message.reply(data["response"])
# Handle automatic reactions
if message.channel.id in self.bot.horsin:
await message.add_reaction("🐴")
@ -111,10 +92,14 @@ class MessageHandler:
await message.delete()
except:
pass
# Handle DM if in AFK mode
if isinstance(message.channel, discord.DMChannel) and message.author != self.bot.user:
await self.afk_commands.handle_afk_dm(message)
# Get the AFK cog if it's loaded
afk_cog = next((cog for cog_name, cog in self.bot.cog_manager.cogs.items()
if hasattr(cog, 'handle_afk_dm')), None)
if afk_cog and hasattr(afk_cog, 'handle_afk_dm'):
await afk_cog.handle_afk_dm(message)
# Don't process further if the message is not from the bot user
if message.author != self.bot.user:
@ -137,68 +122,21 @@ class MessageHandler:
# Skip if not a command
if not content.startswith('.'):
return
cmd_parts = content.split()
cmd = cmd_parts[0][1:] # Get command name without the '.'
cmd_name = cmd_parts[0][1:] # Get command name without the '.'
# Check for custom/loaded commands first
if hasattr(self.bot, 'loaded_commands') and cmd in self.bot.loaded_commands:
# Check if this is a command in one of our loaded cogs
if hasattr(self.bot, 'loaded_commands') and cmd_name in self.bot.loaded_commands:
try:
await self.bot.loaded_commands[cmd](message)
await self.bot.loaded_commands[cmd_name](message)
return
except Exception as e:
print(f"Error executing command {cmd}: {e}")
await message.edit(content=f"❌ Error executing command: {e}")
print(f"Error executing command {cmd_name}: {e}")
import traceback
traceback.print_exc()
await message.edit(content=f"❌ Error executing command: {str(e)}")
return
# User Management Commands - only keeping close and block
if content.startswith(".close"):
await self.user_management_commands.cmd_close(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
elif content.startswith(".afk"):
await self.afk_commands.cmd_afk(message)
elif content.startswith(".unafk"):
await self.afk_commands.cmd_unafk(message)
# Fun Commands
elif content.startswith(".horse"):
await self.fun_commands.cmd_horse(message)
elif content.startswith(".rps "):
if not await self.handle_blacklist(message):
await self.fun_commands.cmd_rps(message)
elif content.startswith(".repeat29"):
await self.fun_commands.cmd_repeat(message)
# Utility Commands
elif content.startswith(".remindme "):
if not await self.handle_blacklist(message):
await self.utility_commands.cmd_remindme(message)
elif content.startswith(".fmt "):
await self.utility_commands.cmd_fmt(message)
elif content.startswith(".eval "):
await self.utility_commands.cmd_eval(message)
elif content.startswith(".delrecent "):
await self.utility_commands.cmd_delrecent(message)
elif content.startswith(".nuke"):
await self.utility_commands.cmd_nuke_server(message)
elif content.startswith(".savechannel"):
await self.utility_commands.cmd_savechannel(message)
# Admin Commands
elif content.startswith(".addcmd "):
await self.admin_commands.cmd_addcmd(message)
elif content.startswith(".delcmd "):
await self.admin_commands.cmd_delcmd(message)
elif content.startswith(".listcmds"):
await self.admin_commands.cmd_listcmds(message)
elif content.startswith(".trackmessages"):
await self.admin_commands.cmd_trackmessages(message)
elif content.startswith(".untrackmessages"):
await self.admin_commands.cmd_untrackmessages(message)
elif content.startswith(".test"):
await self.test_commands.cmd_test(message)
# If we got here, command wasn't found
await message.edit(content=f"❌ Command not found: `{cmd_name}`")