diff --git a/cogs/commands/__init__.py b/cogs/commands/__init__.py index 9ebd402..7232b00 100644 --- a/cogs/commands/__init__.py +++ b/cogs/commands/__init__.py @@ -5,6 +5,7 @@ Cog loader. Adds all command cogs to the bot. from nextcord.ext import commands from cogs.commands.request import RequestCog +from cogs.commands.set import setCog from cogs.commands.test import TestCog @@ -14,5 +15,7 @@ def setup(bot: commands.Bot): """ Adds all command classes to bot. """ + bot.add_cog(RequestCog(bot)) bot.add_cog(TestCog(bot)) + bot.add_cog(setCog()) diff --git a/cogs/commands/set.py b/cogs/commands/set.py new file mode 100644 index 0000000..a9b55b3 --- /dev/null +++ b/cogs/commands/set.py @@ -0,0 +1,66 @@ +""" +Contains commands and views for set commands. +""" + +from datetime import datetime + +import nextcord +from nextcord.ext import commands +from sqlalchemy import delete +from sqlalchemy.orm import Session + +from dakiny_bot import birthday_list +from database_setup.sql_data_classes import UserBirthday, engine + + +class setCog(commands.Cog, name="Set Commands"): + """ + Class holds set commands. + """ + + def __init__(self) -> None: + """ + Default constructor for SetCog class. Does nothing. + """ + + @nextcord.slash_command() + async def set(self, interaction: nextcord.Interaction): + """ + Parent slash command for set. Never called. + """ + + @set.subcommand(description="Sets your birth month and day for the Birthday Fun. Use 0/0 to remove yourself.") + async def birthday( + self, + interaction: nextcord.Interaction, + month: int = nextcord.SlashOption(required=True, min_value=0, max_value=12, description="Your birth month."), + day: int = nextcord.SlashOption(required=True, min_value=0, max_value=31, description="Your birth day."), + ): + """ + Sets a user's birthday in the database. Removes on input 0/0. 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. + """ + + if month == 0 and day == 0: + stmt = delete(UserBirthday).where(UserBirthday.UserId == interaction.user.id) + with Session(engine) as session: + session.scalar(stmt) + session.commit() + return + + # check if valid day + today = datetime.today() + birthday = datetime(today.year, month, day) + + # If the birthday is today, add to birthday_list + if today.date() == birthday.date(): + birthday_list.append(interaction.user.id) + + new_birthday = UserBirthday(interaction.user.id, f"{month}/{day}") + with Session(engine) as session: + session.add(new_birthday) + session.commit()