FEAT: Added a Birthday Module

This commit is contained in:
Dan 2024-06-21 13:22:13 -04:00
parent 15ed750f8b
commit f5872626a3
4 changed files with 127 additions and 1 deletions

1
.gitignore vendored
View File

@ -160,3 +160,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/birthdays.db

View File

@ -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:

View File

@ -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')

View File

@ -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)