2024-02-14 05:39:17 +00:00
|
|
|
"""
|
|
|
|
This plugin provides a command to get the current date
|
|
|
|
"""
|
|
|
|
|
2024-02-12 13:20:31 +00:00
|
|
|
# plugins/date.py
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import simplematrixbotlib as botlib
|
|
|
|
|
2024-02-14 07:12:40 +00:00
|
|
|
async def handle_command(room, message, bot, prefix, config):
|
2024-02-12 13:20:31 +00:00
|
|
|
"""
|
|
|
|
Function to handle the !date command.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room (Room): The Matrix room where the command was invoked.
|
|
|
|
message (RoomMessage): The message object containing the command.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
2024-02-14 07:12:40 +00:00
|
|
|
match = botlib.MessageMatch(room, message, bot, prefix)
|
2024-02-12 13:20:31 +00:00
|
|
|
if match.is_not_from_this_bot() and match.prefix() and match.command("date"):
|
|
|
|
logging.info("Fetching current date and time")
|
|
|
|
current_datetime = datetime.datetime.now()
|
2024-02-17 14:04:35 +00:00
|
|
|
|
|
|
|
# Extract individual date components
|
2024-02-12 13:20:31 +00:00
|
|
|
day_of_week = current_datetime.strftime("%A")
|
2024-02-17 14:04:35 +00:00
|
|
|
date_of_month = current_datetime.strftime("%d") # Day with leading zero
|
2024-02-12 13:20:31 +00:00
|
|
|
month = current_datetime.strftime("%B")
|
|
|
|
year = current_datetime.strftime("%Y")
|
|
|
|
time = current_datetime.strftime("%I:%M:%S %p")
|
2024-02-17 14:04:35 +00:00
|
|
|
|
|
|
|
# Format date with ordinal suffix
|
|
|
|
date_with_ordinal = f"{date_of_month}{get_ordinal_suffix(date_of_month)}"
|
|
|
|
|
|
|
|
# Construct the message
|
|
|
|
date_message = f"Date: **{day_of_week}** the {date_with_ordinal}, **{month} {year}**. \nTime: **⏰ {time}**"
|
|
|
|
|
2024-02-12 13:20:31 +00:00
|
|
|
await bot.api.send_markdown_message(room.room_id, date_message)
|
|
|
|
logging.info("Sent current date and time to the room")
|
2024-02-17 14:04:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_ordinal_suffix(day_of_month):
|
|
|
|
"""
|
|
|
|
Helper function to get the ordinal suffix for a day number.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
day_of_month (str): The day number as a string (e.g., "1", "13")
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The ordinal suffix (e.g., "st", "th", "nd")
|
|
|
|
"""
|
|
|
|
if day_of_month.endswith("11") or day_of_month.endswith("12") or day_of_month.endswith("13"):
|
|
|
|
return "th"
|
|
|
|
elif day_of_month.endswith("1") or day_of_month.endswith("3"):
|
|
|
|
return "st"
|
|
|
|
elif day_of_month.endswith("2"):
|
|
|
|
return "nd"
|
|
|
|
else:
|
|
|
|
return "th"
|