From e37b809938aeb6d09c28eb7788d11b1ec0133b62 Mon Sep 17 00:00:00 2001 From: Mole1424 Date: Mon, 11 Sep 2023 11:43:11 +0000 Subject: [PATCH 1/4] fixed leaderboard name issues --- cogs/commands/birthday.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/commands/birthday.py b/cogs/commands/birthday.py index 5d5110e..d43fdc4 100644 --- a/cogs/commands/birthday.py +++ b/cogs/commands/birthday.py @@ -85,7 +85,7 @@ async def leaderboard(self, ctx: Context): ) 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'}" + 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]) ] ) From 0b968e03815ba2c97a477914db0b1c5c6b022192 Mon Sep 17 00:00:00 2001 From: Mole1424 Date: Mon, 11 Sep 2023 12:15:26 +0000 Subject: [PATCH 2/4] updated pings and when a user is added to db --- cogs/commands/birthday.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cogs/commands/birthday.py b/cogs/commands/birthday.py index d43fdc4..ccf5077 100644 --- a/cogs/commands/birthday.py +++ b/cogs/commands/birthday.py @@ -41,18 +41,21 @@ async def birthday(self, ctx: Context): @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(): + 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() + # update the database 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") From 7af4886831173248dc28057b54a04aedcc8fa732 Mon Sep 17 00:00:00 2001 From: Mole1424 Date: Mon, 11 Sep 2023 12:23:49 +0000 Subject: [PATCH 3/4] made !birthday work --- cogs/commands/birthday.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cogs/commands/birthday.py b/cogs/commands/birthday.py index ccf5077..7dff16d 100644 --- a/cogs/commands/birthday.py +++ b/cogs/commands/birthday.py @@ -34,11 +34,9 @@ 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 @@ -55,7 +53,7 @@ async def wish(self, ctx: Context): db_session.commit() # update the database await ctx.reply( - f"Happy birthday!!!! <@{CONFIG.LORD_CHANCELLOR_ID}>{f', you are now {self.age} years old' if first else ''}" + 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") From 48addcbedffc086cbcab003352a5f819025115ea Mon Sep 17 00:00:00 2001 From: Mole1424 Date: Mon, 11 Sep 2023 16:18:43 +0000 Subject: [PATCH 4/4] made leaderboard less cursed added db bypass to wish --- cogs/commands/birthday.py | 40 +++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/cogs/commands/birthday.py b/cogs/commands/birthday.py index 7dff16d..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 @@ -44,14 +46,16 @@ async def wish(self, ctx: Context): timezone("Europe/London") ) # gets the current date if current_date.date() > self.date.date(): - 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() - # update the database + 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}>!!!!! {f' You are now {self.age} years old' if first else ''}" ) @@ -84,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}. {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]) - ] - ) + 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}")