1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 22:23:13 +00:00
Lumi/services/birthday_service.py

55 lines
1.5 KiB
Python
Raw Normal View History

2023-07-31 15:16:04 +00:00
import datetime
import pytz
from db import database
class Birthday:
2024-03-04 10:25:00 +00:00
def __init__(self, user_id, guild_id):
2023-07-31 15:16:04 +00:00
self.user_id = user_id
2024-03-08 11:16:42 +00:00
self.guild_id = guild_id
2023-07-31 15:16:04 +00:00
def set(self, birthday):
query = """
2024-03-04 10:25:00 +00:00
INSERT INTO birthdays (user_id, guild_id, birthday)
VALUES (%s, %s, %s)
2023-10-23 16:39:15 +00:00
ON DUPLICATE KEY UPDATE birthday = VALUES(birthday);
2023-07-31 15:16:04 +00:00
"""
2024-03-04 10:25:00 +00:00
database.execute_query(query, (self.user_id, self.guild_id, birthday))
2023-07-31 15:16:04 +00:00
2024-06-19 19:07:49 +00:00
def delete(self):
query = """
DELETE FROM birthdays
WHERE user_id = %s AND guild_id = %s;
"""
database.execute_query(query, (self.user_id, self.guild_id))
2023-07-31 15:16:04 +00:00
@staticmethod
2024-03-08 11:16:42 +00:00
def get_birthdays_today():
2023-07-31 15:16:04 +00:00
query = """
2024-03-08 11:16:42 +00:00
SELECT user_id, guild_id
2023-07-31 15:16:04 +00:00
FROM birthdays
2024-02-27 09:53:31 +00:00
WHERE DATE_FORMAT(birthday, '%m-%d') = %s
2023-07-31 15:16:04 +00:00
"""
2024-03-08 11:16:42 +00:00
tz = pytz.timezone("US/Eastern")
2024-03-04 10:25:00 +00:00
today = datetime.datetime.now(tz).strftime("%m-%d")
2023-07-31 15:16:04 +00:00
return database.select_query(query, (today,))
2024-02-28 11:09:04 +00:00
@staticmethod
2024-03-08 11:16:42 +00:00
def get_upcoming_birthdays(guild_id):
2024-02-28 11:09:04 +00:00
query = """
SELECT user_id, DATE_FORMAT(birthday, '%m-%d') AS upcoming_birthday
FROM birthdays
WHERE guild_id = %s
ORDER BY (DAYOFYEAR(birthday) - DAYOFYEAR(now()) + 366) % 366;
2024-02-28 11:09:04 +00:00
"""
2024-03-08 11:16:42 +00:00
data = database.select_query(query, (guild_id,))
2024-02-28 11:09:04 +00:00
return [(row[0], row[1]) for row in data]