85 lines
3.1 KiB
Python
85 lines
3.1 KiB
Python
"""
|
|
Contains commands and views for set commands.
|
|
"""
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
import nextcord
|
|
from nextcord.ext import commands
|
|
from sqlalchemy import delete, select
|
|
from sqlalchemy.exc import IntegrityError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database_setup.sql_data_classes import UserBirthday, engine
|
|
from main import birthday_list
|
|
|
|
|
|
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.
|
|
"""
|
|
# Must be deferred, apparently datetime takes a little bit on invalid dates.
|
|
await interaction.response.defer(ephemeral=True)
|
|
|
|
if month == 0 and day == 0:
|
|
stmt = delete(UserBirthday).where(UserBirthday.UserId == interaction.user.id)
|
|
with Session(engine) as session:
|
|
session.execute(stmt)
|
|
session.commit()
|
|
await interaction.followup.send("Removed birthday.", ephemeral=True)
|
|
return
|
|
|
|
# check if valid day
|
|
today = datetime.now(tz=UTC).date()
|
|
try:
|
|
birthday = datetime(today.year, month, day)
|
|
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(UserId=interaction.user.id, Birthday=f"{month}/{day}")
|
|
with Session(engine) as session:
|
|
try:
|
|
session.add(new_birthday)
|
|
session.commit()
|
|
except IntegrityError:
|
|
# User already has birthday, change it instead.
|
|
session.rollback()
|
|
stmt = select(UserBirthday).where(UserBirthday.UserId == interaction.user.id)
|
|
new_birthday = session.scalar(stmt)
|
|
new_birthday.Birthday = f"{month}/{day}"
|
|
session.commit()
|
|
|
|
await interaction.followup.send(f"Birthday set to {month}/{day}.", ephemeral=True)
|