added the timestamp thingye

This commit is contained in:
Xargana 2025-05-08 20:31:41 +03:00
parent 3d14bbe477
commit 4d60c12d17
3 changed files with 148 additions and 1 deletions

View file

@ -1,11 +1,14 @@
import discord
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 utils.time_parser import parse_time
class MessageHandler:
def __init__(self, bot):
@ -24,7 +27,68 @@ class MessageHandler:
bot.admin_commands = self.admin_commands
bot.test_commands = self.test_commands
# Regex for detecting "in X time" patterns
self.time_pattern = re.compile(r'in\s+(\d+\s+(?:hour|hours|minute|minutes|day|days|second|seconds)s?)', re.IGNORECASE)
def parse_relative_time(self, time_str):
"""
Parse relative time strings like "2 hours", "30 minutes"
Args:
time_str: String in format like "2 hours", "30 minutes"
Returns:
Unix timestamp (seconds since epoch) for the future time or None if invalid
"""
# Convert to format our parse_time can handle
time_part = time_str.replace('hours', 'h').replace('hour', 'h')
time_part = time_part.replace('minutes', 'm').replace('minute', 'm')
time_part = time_part.replace('seconds', 's').replace('second', 's')
time_part = time_part.replace('days', 'd').replace('day', 'd')
# Remove spaces to match expected format like "2h30m"
time_part = re.sub(r'\s+', '', time_part)
seconds = parse_time(time_part)
if seconds:
return int(time.time() + seconds)
return None
def create_discord_timestamp(self, unix_time, format_code='R'):
"""Create a Discord timestamp string"""
return f"<t:{unix_time}:{format_code}>"
def replace_time_patterns(self, content):
"""Replace "in X time" patterns with Discord timestamps"""
def replace_match(match):
time_str = match.group(1)
unix_time = self.parse_relative_time(time_str)
if unix_time:
return self.create_discord_timestamp(unix_time)
return match.group(0) # Return original text if parsing fails
return self.time_pattern.sub(replace_match, content)
async def handle_message(self, message):
# 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)
# 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
# Handle special responses
for user_id, data in SPECIAL_RESPONSES.items():
if message.author.id == user_id and data["trigger"] in message.content: