From 8509bcc98fead162ce26ac602fb7fb7c699a4b5c Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 25 Jun 2024 15:35:40 -0400 Subject: [PATCH] FEAT: added birthday module --- config.py | 3 ++ main.py | 7 ++- modules/user/birthday.py | 107 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 modules/user/birthday.py diff --git a/config.py b/config.py index 0bb5a6b..8151e5f 100644 --- a/config.py +++ b/config.py @@ -12,6 +12,9 @@ config = { }, 'xp': { 'enabled': True + }, + 'birthday': { + 'enabled': True } } } diff --git a/main.py b/main.py index c944a1c..e4548e6 100644 --- a/main.py +++ b/main.py @@ -37,12 +37,17 @@ class Selena(discord.Client): xp = XP(self) xp.setup(self.tree) + if config['modules']['birthday']['enabled']: + from modules.user.birthday import Birthday + birthday = Birthday(self) + birthday.setup(self.tree) + bot = Selena() @bot.event async def on_ready(): - logging.info(f'Bot {bot.user.name} has connected to Discord!') + logging.info(f'{bot.user.name} has connected to Discord!') bot.run(TOKEN) diff --git a/modules/user/birthday.py b/modules/user/birthday.py new file mode 100644 index 0000000..88e204f --- /dev/null +++ b/modules/user/birthday.py @@ -0,0 +1,107 @@ +import discord +from discord import app_commands +from datetime import datetime +import sqlite3 +import asyncio + + +class Birthday: + def __init__(self, bot): + self.bot = bot + self.db_path = 'data/selena.db' + self._init_db() + + def _init_db(self): + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS birthdays ( + user_id TEXT PRIMARY KEY, + birthday TEXT + ) + ''') + conn.commit() + + def _set_birthday(self, user_id, birthday): + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute('INSERT OR REPLACE INTO birthdays (user_id, birthday) VALUES (?, ?)', (user_id, birthday)) + conn.commit() + + def _get_birthday(self, user_id): + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute('SELECT birthday FROM birthdays WHERE user_id = ?', (user_id,)) + row = cursor.fetchone() + return row[0] if row else None + + def _remove_birthday(self, user_id): + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute('DELETE FROM birthdays WHERE user_id = ?', (user_id,)) + conn.commit() + + async def set_birthday(self, interaction: discord.Interaction, date: str): + user_id = str(interaction.user.id) + try: + datetime.strptime(date, "%Y-%m-%d") + self._set_birthday(user_id, date) + await interaction.response.send_message(f'{interaction.user.mention}, your birthday has been set to {date}.', ephemeral=True) + except ValueError: + await interaction.response.send_message(f'{interaction.user.mention}, the date format is incorrect. Please use YYYY-MM-DD.', ephemeral=True) + + async def get_birthday(self, interaction: discord.Interaction): + user_id = str(interaction.user.id) + birthday = self._get_birthday(user_id) + if birthday: + await interaction.response.send_message(f'{interaction.user.mention}, your birthday is set to {birthday}.', ephemeral=True) + else: + await interaction.response.send_message(f'{interaction.user.mention}, you have not set your birthday.', ephemeral=True) + + async def remove_birthday(self, interaction: discord.Interaction): + user_id = str(interaction.user.id) + self._remove_birthday(user_id) + await interaction.response.send_message(f'{interaction.user.mention}, your birthday has been removed.', ephemeral=True) + + async def check_birthday(self): + today = datetime.today().strftime("%Y-%m-%d") + with sqlite3.connect(self.db_path) as conn: + cursor = conn.cursor() + cursor.execute('SELECT user_id FROM birthdays WHERE birthday = ?', (today,)) + users = cursor.fetchall() + + for user in users: + user_id = user[0] + user_obj = await self.bot.fetch_user(user_id) + if user_obj: + await user_obj.send(f'🎉🎂 Happy Birthday, {user_obj.mention}! 🎂🎉') + + def setup(self, tree): + @app_commands.command(name='set_birthday', description='Set your birthday') + @app_commands.describe(date='Your birthday in YYYY-MM-DD format') + async def set_birthday_command(interaction: discord.Interaction, date: str): + await self.set_birthday(interaction, date) + + @app_commands.command(name='get_birthday', description='Check your birthday') + async def get_birthday_command(interaction: discord.Interaction): + await self.get_birthday(interaction) + + @app_commands.command(name='remove_birthday', description='Remove your birthday') + async def remove_birthday_command(interaction: discord.Interaction): + await self.remove_birthday(interaction) + + tree.add_command(set_birthday_command) + tree.add_command(get_birthday_command) + tree.add_command(remove_birthday_command) + + async def setup_hook(self): + await self.bot.wait_until_ready() + while not self.bot.is_closed(): + await self.check_birthday() + await asyncio.sleep(86400) # Check once every 24 hours + + +def setup(bot): + birthday = Birthday(bot) + bot.add_cog(birthday) + bot.setup_hook = birthday.setup_hook