From 892f873320d8e9024a276eb2e03a1edc6233af77 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Mon, 17 Mar 2025 18:16:44 -0400 Subject: [PATCH] Create birthday set command. --- cogs/commands/__init__.py | 2 ++ cogs/commands/birthday.py | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 cogs/commands/birthday.py diff --git a/cogs/commands/__init__.py b/cogs/commands/__init__.py index b6739b9..bb5ff0f 100644 --- a/cogs/commands/__init__.py +++ b/cogs/commands/__init__.py @@ -2,6 +2,7 @@ from nextcord.ext import commands +from cogs.commands.birthday import BirthdayCog from cogs.commands.hi import HiCog @@ -9,4 +10,5 @@ from cogs.commands.hi import HiCog # It is necessary to add these functions to the bot. def setup(bot: commands.Bot) -> None: """Add all command classes to bot.""" + bot.add_cog(BirthdayCog()) bot.add_cog(HiCog(bot)) diff --git a/cogs/commands/birthday.py b/cogs/commands/birthday.py new file mode 100644 index 0000000..d31ccef --- /dev/null +++ b/cogs/commands/birthday.py @@ -0,0 +1,67 @@ +"""Contains commands for birthday feature.""" + +from datetime import datetime + +import nextcord +from nextcord.ext import commands +from pytz import UTC +from sqlmodel import Session, select + +from database.db import engine +from database.models import UserBirthday +from main import birthday_list + + +class BirthdayCog(commands.Cog, name="Birthday Commands"): + """Class holds birthday commands.""" + + @nextcord.slash_command() + async def birthday(self, interaction: nextcord.Interaction) -> None: + """Parent slash command for birthday. Never called.""" + + @birthday.subcommand(description="Set your birthday month and day for birthday fun!") + async def set( + self, + interaction: nextcord.Interaction, + month: int = nextcord.SlashOption(required=True, min_value=1, max_value=12, description="Your birth month."), + day: int = nextcord.SlashOption(required=True, min_value=1, max_value=31, description="Your birth day."), + ) -> None: + """ + Set a user's birthday in the database. If birthday is today, adds to the birthday_list. + + Args: + interaction (nextcord.Interaction): Interaction that invoked the command. + month (int, optional): The month a user was born. Is SlashOption. + day (int, optional): The day a user was born. Is SlashOption. + + """ + # Ensure that a person used the command. + if interaction.user is None: + return + + await interaction.response.defer(ephemeral=True) + + # check if valid day + today = datetime.now(tz=UTC) + try: + birthday = datetime(today.year, month, day, tzinfo=UTC) + except ValueError: + await interaction.followup.send(f"Invalid date: {month}/{day}.", ephemeral=True) + return + + # If the birthday is today, add to birthday_list + if today.date() == birthday.date(): + birthday_list.append(interaction.user.id) + + new_birthday = UserBirthday(user_id=interaction.user.id, month=month, day=day) + with Session(engine) as session: + stmt = select(UserBirthday).where(UserBirthday.user_id == interaction.user.id) + if (new_birthday := session.scalar(stmt)) is None: + new_birthday = UserBirthday(user_id=interaction.user.id, month=month, day=day) + session.add(new_birthday) + else: + new_birthday.month = month + new_birthday.day = day + session.commit() + + await interaction.followup.send(f"Birthday set to {month}/{day}.", ephemeral=True)