diff --git a/.gitignore b/.gitignore index 5d381cc..36b9a8b 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +/birthdays.db \ No newline at end of file diff --git a/main.py b/main.py index 7e85ee0..5e6f112 100644 --- a/main.py +++ b/main.py @@ -8,8 +8,10 @@ class Selena(discord.Client): self.tree = discord.app_commands.CommandTree(self) async def setup_hook(self): + guild = discord.Object(id=config.DISCORD_GUILD_ID) await self.load_extensions() - await self.tree.sync() + self.tree.copy_global_to(guild=guild) + await self.tree.sync(guild=guild) async def load_extension(self, name): module = __import__(name, fromlist=["setup"]) @@ -19,6 +21,7 @@ class Selena(discord.Client): extensions = [ "modules.admin.logger_module", "modules.media.spotify_module", + "modules.user.birthday_module", # Add other modules here ] for extension in extensions: diff --git a/modules/user/birthday_db.py b/modules/user/birthday_db.py new file mode 100644 index 0000000..0d609cd --- /dev/null +++ b/modules/user/birthday_db.py @@ -0,0 +1,18 @@ +import sqlite3 + + +def initialize_db(): + conn = sqlite3.connect('birthdays.db') + cursor = conn.cursor() + cursor.execute(''' + CREATE TABLE IF NOT EXISTS birthdays ( + user_id TEXT PRIMARY KEY, + birthday TEXT + ) + ''') + conn.commit() + conn.close() + + +def get_connection(): + return sqlite3.connect('birthdays.db') diff --git a/modules/user/birthday_module.py b/modules/user/birthday_module.py new file mode 100644 index 0000000..1afd6f0 --- /dev/null +++ b/modules/user/birthday_module.py @@ -0,0 +1,104 @@ +# Flake8: noqa +import discord +from discord import app_commands +import sqlite3 +import logging +from .birthday_db import initialize_db, get_connection + +logger = logging.getLogger(__name__) +initialize_db() + + +class BirthdayModule: + def __init__(self, bot): + self.bot = bot + self.add_commands() + + def add_commands(self): + @app_commands.command( + name="add_birthday", description="Add your birthday" + ) + async def add_birthday(interaction: discord.Interaction, date: str): + await interaction.response.defer() + try: + conn = get_connection() + cursor = conn.cursor() + cursor.execute('INSERT OR REPLACE INTO birthdays (user_id, birthday) VALUES (?, ?)', + (str(interaction.user.id), date)) + conn.commit() + conn.close() + await interaction.followup.send( + embed=discord.Embed( + title="Add Birthday", + description=f"Your birthday {date} has been added.", + color=discord.Color.green() + ) + ) + logger.info(f"Birthday added for user {interaction.user.id}: {date}") + except Exception as e: + await interaction.followup.send(f"An error occurred: {e}") + logger.error(f"Error in add_birthday command: {e}") + + @app_commands.command( + name="view_birthday", description="View your birthday" + ) + async def view_birthday(interaction: discord.Interaction): + await interaction.response.defer() + try: + conn = get_connection() + cursor = conn.cursor() + cursor.execute('SELECT birthday FROM birthdays WHERE user_id = ?', (str(interaction.user.id),)) + result = cursor.fetchone() + conn.close() + if result: + await interaction.followup.send( + embed=discord.Embed( + title="View Birthday", + description=f"Your birthday is {result[0]}.", + color=discord.Color.green() + ) + ) + logger.info(f"Birthday viewed for user {interaction.user.id}: {result[0]}") + else: + await interaction.followup.send( + embed=discord.Embed( + title="View Birthday", + description="You have not set a birthday yet.", + color=discord.Color.red() + ) + ) + logger.info(f"Birthday not found for user {interaction.user.id}") + except Exception as e: + await interaction.followup.send(f"An error occurred: {e}") + logger.error(f"Error in view_birthday command: {e}") + + @app_commands.command( + name="remove_birthday", description="Remove your birthday" + ) + async def remove_birthday(interaction: discord.Interaction): + await interaction.response.defer() + try: + conn = get_connection() + cursor = conn.cursor() + cursor.execute('DELETE FROM birthdays WHERE user_id = ?', (str(interaction.user.id),)) + conn.commit() + conn.close() + await interaction.followup.send( + embed=discord.Embed( + title="Remove Birthday", + description="Your birthday has been removed.", + color=discord.Color.green() + ) + ) + logger.info(f"Birthday removed for user {interaction.user.id}") + except Exception as e: + await interaction.followup.send(f"An error occurred: {e}") + logger.error(f"Error in remove_birthday command: {e}") + + self.bot.tree.add_command(add_birthday) + self.bot.tree.add_command(view_birthday) + self.bot.tree.add_command(remove_birthday) + + +async def setup(bot): + BirthdayModule(bot)