woodburn-bot/tools.py
Nathan Woodburn cd3a61f542
All checks were successful
Build Docker / Build Docker (push) Successful in 51s
feat: Update reminders to have public ones
2024-02-08 11:22:23 +11:00

75 lines
2.4 KiB
Python

import datetime
import re
import discord
import json
REMINDERS_FILE_PATH = '/mnt/reminders.txt'
def parse_time(time_str):
# Parse the time string and return a timedelta
try:
time_components = re.findall(r'(\d+)\s*([dDhHrRmMsS]?)', time_str)
total_seconds = 0
for value, unit in time_components:
value = int(value)
if unit.lower() in ('d', 'day', 'days'):
total_seconds += value * 86400 # seconds in a day
elif unit.lower() in ('h', 'hr', 'hour', 'hours'):
total_seconds += value * 3600 # seconds in an hour
elif unit.lower() in ('m', 'min', 'minute', 'minutes'):
total_seconds += value * 60 # seconds in a minute
elif unit.lower() in ('s', 'sec', 'second', 'seconds'):
total_seconds += value
return datetime.timedelta(seconds=total_seconds)
except ValueError:
return None
def read_reminders():
try:
with open(REMINDERS_FILE_PATH, 'r') as file:
return json.load(file)
except FileNotFoundError:
return []
def write_reminders(reminders):
with open(REMINDERS_FILE_PATH, 'w') as file:
json.dump(reminders, file)
def store_reminder(user_id, reminder_time, reminder_text, public=False, channel_id=None):
reminders = read_reminders()
reminders.append({
'user_id': str(user_id),
'time': reminder_time.strftime("%Y-%m-%d %H:%M:%S"),
'text': reminder_text,
'public': public,
'channel_id': channel_id
})
write_reminders(reminders)
def embed(title, description):
embed = discord.Embed(
title=title,
description=description,
color=discord.Color.dark_purple()
)
# Set a footer for the embed
embed.set_footer(text='Powered by Nathan.Woodburn/')
return embed
def timestamp_relative(date_time):
return "<t:"+str(int(date_time.timestamp()))+":R>"
def timestamp_all_raw(date_time):
timestamps = ""
options = ['R', 't', 'T', 'd', 'D', 'f', 'F']
for option in options:
timestamps += "<t:"+str(int(date_time.timestamp()))+":"+option+"> "
timestamps += "`<t:"+str(int(date_time.timestamp()))+":"+option+">`\n"
return timestamps
if __name__ == '__main__':
print(parse_time('1d'))
print(timestamp_relative(datetime.datetime.now()+parse_time('1d')))
print(parse_time('1s'))
print(timestamp_relative(datetime.datetime.now()+parse_time('1s')))