diff --git a/cogs/commands/birthday.py b/cogs/commands/birthday.py index 5d5110e..743dc2d 100644 --- a/cogs/commands/birthday.py +++ b/cogs/commands/birthday.py @@ -3,7 +3,9 @@ from discord import User from discord.ext import commands from discord.ext.commands import Bot, Context +from psycopg import OperationalError from pytz import timezone, utc +from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.sql import func from config import CONFIG @@ -34,25 +36,28 @@ def __init__(self, bot: Bot): @commands.hybrid_group(help=LONG_HELP_TEXT, brief="HAPPY BIRTHDAY!!!!") async def birthday(self, ctx: Context): - if not ctx.invoked_subcommand: - await ctx.send("Subcommand not found") + await self.wish(ctx) @birthday.command(help=LONG_HELP_TEXT, brief="HAPPY BIRTHDAY!!!!") - @commands.cooldown(1, 86400, commands.BucketType.user) # 1 day cooldown per user async def wish(self, ctx: Context): """Adds 1 to the age of the Lord Chancellor and wishes them a happy birthday""" + first = False current_date = utc.localize(datetime.now()).astimezone( timezone("Europe/London") ) # gets the current date - self.date = current_date - self.age += 1 - db_user = get_database_user(ctx.author) - borth = db_Birthday(date=self.date, age=self.age, user_id=db_user.id) - db_session.add(borth) - db_session.commit() - # update the database + if current_date.date() > self.date.date(): + try: + self.date = current_date + self.age += 1 + first = True + db_user = get_database_user(ctx.author) + borth = db_Birthday(date=self.date, age=self.age, user_id=db_user.id) + db_session.add(borth) + db_session.commit() + except (SQLAlchemyError, OperationalError): + pass await ctx.reply( - f"Happy birthday!!!! <@{CONFIG.LORD_CHANCELLOR_ID}>, you are now {self.age}" + f"Happy birthday <@{CONFIG.LORD_CHANCELLOR_ID}>!!!!! {f' You are now {self.age} years old' if first else ''}" ) @birthday.command(help=LONG_HELP_TEXT, brief="Lord Chancellor age") @@ -83,12 +88,20 @@ async def leaderboard(self, ctx: Context): .order_by(func.count(db_Birthday.user_id).desc()) .all() ) - leaderboard_users = "\n".join( # make list of names - [ - f"{i+1}. {db_session.query(db_user).filter(db_user.id == user.user_id).first().username} with {user[1]} wish {'' if user[1] == 1 else 'es'}" - for i, user in enumerate(leaderboard[:5]) - ] - ) + leaderboard_users = "" + for i in range(5): + if i >= len(leaderboard): + break + user = leaderboard[i] + user_name = self.bot.get_user( + db_session.query(db_user) + .filter(db_user.id == user.user_id) + .first() + .user_uid + ).name + num_wishes = user[1] + leaderboard_users += f"{i+1}. {user_name} with {num_wishes} wish{'' if num_wishes == 1 else 'es'}\n" + # for anyone that cares this can be done in one line: leaderboard_users = "\n".join([f"{i+1}. {self.bot.get_user(db_session.query(db_user).filter(db_user.id == user.user_id).first().user_uid).name} with {user[1]} wish{'' if user[1] == 1 else 'es'}" for i, user in enumerate(leaderboard[:5])] await ctx.reply(f"Leaderboard:\n{leaderboard_users}")