24 lines
676 B
Python
24 lines
676 B
Python
import discord
|
|
from datetime import datetime
|
|
from datetime_utils import to_unix
|
|
from loguru import logger
|
|
|
|
|
|
class DateTransformer(discord.app_commands.Transformer):
|
|
"""
|
|
Converts date as 2022-12-24 to unix timestamp
|
|
"""
|
|
|
|
async def transform(self, interaction: discord.Interaction, value: str, /) -> int:
|
|
logger.trace(f'Converting {value} to unix timestamp')
|
|
|
|
date_format = "%Y-%m-%d"
|
|
|
|
try:
|
|
date = to_unix(datetime.strptime(value, date_format))
|
|
return date
|
|
|
|
except Exception:
|
|
logger.opt(exception=True).warning(f"Failed to parse to datetime {value!r}")
|
|
raise
|