82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
import re
|
|
import time
|
|
import datetime
|
|
from utils.time_parser import parse_time
|
|
|
|
def parse_relative_time(time_str):
|
|
"""
|
|
Parse relative time strings like "in 2 hours", "in 30 minutes"
|
|
|
|
Args:
|
|
time_str: String in format like "in 2 hours", "in 30 minutes"
|
|
|
|
Returns:
|
|
Unix timestamp (seconds since epoch) for the future time
|
|
"""
|
|
# Handle "in X time" format
|
|
in_match = re.match(r'in\s+(.+)', time_str, re.IGNORECASE)
|
|
if in_match:
|
|
time_part = in_match.group(1)
|
|
# Convert to format our parse_time can handle
|
|
time_part = time_part.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)
|
|
|
|
# If not a relative time, try to parse as a specific date/time
|
|
# For simplicity, returning None for now
|
|
return None
|
|
|
|
def create_discord_timestamp(unix_time, format_code='F'):
|
|
"""
|
|
Create a Discord timestamp string
|
|
|
|
Args:
|
|
unix_time: Unix timestamp (seconds since epoch)
|
|
format_code: Discord timestamp format code
|
|
F: Full date and time (default)
|
|
R: Relative time
|
|
t: Short time (HH:MM)
|
|
T: Long time (HH:MM:SS)
|
|
d: Short date (MM/DD/YYYY)
|
|
D: Long date (Month DD, YYYY)
|
|
|
|
Returns:
|
|
Discord timestamp string
|
|
"""
|
|
return f"<t:{unix_time}:{format_code}>"
|
|
|
|
async def run(client, message, args):
|
|
"""
|
|
Create a Discord timestamp from a natural language time description
|
|
|
|
Usage: .timestamp in 2 hours
|
|
.timestamp in 30 minutes R
|
|
"""
|
|
if not args:
|
|
await message.channel.send("Usage: `.timestamp in X hours/minutes/days [format]`\nFormats: F (full), R (relative), t (short time), T (long time), d (short date), D (long date)")
|
|
return
|
|
|
|
# Check if the last argument is a format code
|
|
format_code = 'F' # Default format
|
|
if len(args) > 1 and args[-1] in ['F', 'R', 't', 'T', 'd', 'D']:
|
|
format_code = args[-1]
|
|
time_str = ' '.join(args[:-1])
|
|
else:
|
|
time_str = ' '.join(args)
|
|
|
|
# Parse the time
|
|
unix_time = parse_relative_time(time_str)
|
|
|
|
if unix_time:
|
|
timestamp = create_discord_timestamp(unix_time, format_code)
|
|
# Send the timestamp so user can copy it
|
|
await message.channel.send(f"Discord timestamp: `{timestamp}`\nPreview: {timestamp}")
|
|
else:
|
|
await message.channel.send("I couldn't understand that time format. Try something like 'in 2 hours' or 'in 30 minutes'.") |