Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

231 fix birthday #233

Merged
merged 4 commits into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions cogs/commands/birthday.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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}")


Expand Down