From 65398ed2b9fb67dbe842996e01142722bd0667f3 Mon Sep 17 00:00:00 2001 From: aru Date: Wed, 1 Dec 2021 17:15:25 -0500 Subject: fix: hanukkah command respects month boundaries rewrote hanukkah to use datetime.strptime left a helper method and some variables in order to allow extending to use a cache in the future, rather than requesting the api every invoke that is out of scope of this commit and pull, since the command is currently broken. I've only kept the same functionality, without trying to rewrite the entire command. --- bot/exts/holidays/hanukkah/hanukkah_embed.py | 84 ++++++++++++---------------- 1 file changed, 36 insertions(+), 48 deletions(-) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/hanukkah/hanukkah_embed.py b/bot/exts/holidays/hanukkah/hanukkah_embed.py index ac3eab7b..5767f91e 100644 --- a/bot/exts/holidays/hanukkah/hanukkah_embed.py +++ b/bot/exts/holidays/hanukkah/hanukkah_embed.py @@ -21,45 +21,41 @@ class HanukkahEmbed(commands.Cog): def __init__(self, bot: Bot): self.bot = bot - self.hanukkah_days = [] - self.hanukkah_months = [] - self.hanukkah_years = [] + self.hanukkah_dates: list[datetime.date] = [] - async def get_hanukkah_dates(self) -> list[str]: + def _parse_time_to_datetime(self, date: list[str]) -> datetime.datetime: + """Format the times provided by the api to datetime forms.""" + try: + return datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S%z") + except ValueError: + # there is a possibility of an event not having a time, just a day + # to catch this, we try again without time information + return datetime.datetime.strptime(date, "%Y-%m-%d") + + async def fetch_hanukkah_dates(self) -> list[datetime.date]: """Gets the dates for hanukkah festival.""" - hanukkah_dates = [] + # clear the datetime objects to prevent a memory link + self.hanukkah_dates = [] async with self.bot.http_session.get(HEBCAL_URL) as response: json_data = await response.json() festivals = json_data["items"] for festival in festivals: if festival["title"].startswith("Chanukah"): date = festival["date"] - hanukkah_dates.append(date) - return hanukkah_dates + self.hanukkah_dates.append(self._parse_time_to_datetime(date).date()) + return self.hanukkah_dates @in_month(Month.NOVEMBER, Month.DECEMBER) @commands.command(name="hanukkah", aliases=("chanukah",)) async def hanukkah_festival(self, ctx: commands.Context) -> None: """Tells you about the Hanukkah Festivaltime of festival, festival day, etc).""" - hanukkah_dates = await self.get_hanukkah_dates() - self.hanukkah_dates_split(hanukkah_dates) - hanukkah_start_day = int(self.hanukkah_days[0]) - hanukkah_start_month = int(self.hanukkah_months[0]) - hanukkah_start_year = int(self.hanukkah_years[0]) - hanukkah_end_day = int(self.hanukkah_days[8]) - hanukkah_end_month = int(self.hanukkah_months[8]) - hanukkah_end_year = int(self.hanukkah_years[8]) - - hanukkah_start = datetime.date(hanukkah_start_year, hanukkah_start_month, hanukkah_start_day) - hanukkah_end = datetime.date(hanukkah_end_year, hanukkah_end_month, hanukkah_end_day) + hanukkah_dates = await self.fetch_hanukkah_dates() + start_day = hanukkah_dates[0] + end_day = hanukkah_dates[-1] today = datetime.date.today() - # today = datetime.date(2019, 12, 24) (for testing) - day = str(today.day) - month = str(today.month) - year = str(today.year) embed = Embed(title="Hanukkah", colour=Colours.blue) - if day in self.hanukkah_days and month in self.hanukkah_months and year in self.hanukkah_years: - if int(day) == hanukkah_start_day: + if start_day <= today <= end_day: + if start_day == today: now = datetime.datetime.utcnow() hours = now.hour + 4 # using only hours hanukkah_start_hour = 18 @@ -77,35 +73,27 @@ class HanukkahEmbed(commands.Cog): ) await ctx.send(embed=embed) return - festival_day = self.hanukkah_days.index(day) + festival_day = hanukkah_dates.index(today) number_suffixes = ["st", "nd", "rd", "th"] suffix = number_suffixes[festival_day - 1 if festival_day <= 3 else 3] message = ":menorah:" * festival_day - embed.description = f"It is the {festival_day}{suffix} day of Hanukkah!\n{message}" - await ctx.send(embed=embed) + embed.description = ( + f"It is the {festival_day}{suffix} day of Hanukkah!\n{message}" + ) + elif today < start_day: + format_start = start_day.strftime("%d of %B") + embed.description = ( + "Hanukkah has not started yet. " + f"Hanukkah will start at sundown on {format_start}." + ) else: - if today < hanukkah_start: - festival_starting_month = hanukkah_start.strftime("%B") - embed.description = ( - f"Hanukkah has not started yet. " - f"Hanukkah will start at sundown on {hanukkah_start_day}th " - f"of {festival_starting_month}." - ) - else: - festival_end_month = hanukkah_end.strftime("%B") - embed.description = ( - f"Looks like you missed Hanukkah!" - f"Hanukkah ended on {hanukkah_end_day}th of {festival_end_month}." - ) - - await ctx.send(embed=embed) + format_end = end_day.strftime("%d of %B") + embed.description = ( + "Looks like you missed Hanukkah! " + f"Hanukkah ended on {format_end}." + ) - def hanukkah_dates_split(self, hanukkah_dates: list[str]) -> None: - """We are splitting the dates for hanukkah into days, months and years.""" - for date in hanukkah_dates: - self.hanukkah_days.append(date[8:10]) - self.hanukkah_months.append(date[5:7]) - self.hanukkah_years.append(date[0:4]) + await ctx.send(embed=embed) def setup(bot: Bot) -> None: -- cgit v1.2.3 From c7f62d3d63b4fe84c8f96bf38b66198838fdb538 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Fri, 3 Dec 2021 17:28:39 -0500 Subject: yank lovefest role management commands --- bot/exts/holidays/valentines/be_my_valentine.py | 31 +++++++++---------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/valentines/be_my_valentine.py b/bot/exts/holidays/valentines/be_my_valentine.py index 4d454c3a..a9a4731f 100644 --- a/bot/exts/holidays/valentines/be_my_valentine.py +++ b/bot/exts/holidays/valentines/be_my_valentine.py @@ -7,14 +7,17 @@ import discord from discord.ext import commands from bot.bot import Bot -from bot.constants import Channels, Colours, Lovefest, Month +from bot.constants import Channels, Colours, Lovefest, Month, PYTHON_PREFIX from bot.utils.decorators import in_month +from bot.utils.exceptions import MovedCommandError from bot.utils.extensions import invoke_help_command log = logging.getLogger(__name__) HEART_EMOJIS = [":heart:", ":gift_heart:", ":revolving_hearts:", ":sparkling_heart:", ":two_hearts:"] +MOVED_COMMAND = f"{PYTHON_PREFIX}subscribe" + class BeMyValentine(commands.Cog): """A cog that sends Valentines to other users!""" @@ -30,7 +33,7 @@ class BeMyValentine(commands.Cog): return loads(p.read_text("utf8")) @in_month(Month.FEBRUARY) - @commands.group(name="lovefest") + @commands.group(name="lovefest", help=f"This command has been moved to {MOVED_COMMAND}") async def lovefest_role(self, ctx: commands.Context) -> None: """ Subscribe or unsubscribe from the lovefest role. @@ -43,27 +46,15 @@ class BeMyValentine(commands.Cog): if not ctx.invoked_subcommand: await invoke_help_command(ctx) - @lovefest_role.command(name="sub") + @lovefest_role.command(name="sub", help=f"This command has been moved to {MOVED_COMMAND}") async def add_role(self, ctx: commands.Context) -> None: - """Adds the lovefest role.""" - user = ctx.author - role = ctx.guild.get_role(Lovefest.role_id) - if role not in ctx.author.roles: - await user.add_roles(role) - await ctx.send("The Lovefest role has been added !") - else: - await ctx.send("You already have the role !") + """NOTE: This command has been moved to bot.""" + raise MovedCommandError(MOVED_COMMAND) - @lovefest_role.command(name="unsub") + @lovefest_role.command(name="unsub", help=f"This command has been moved to {MOVED_COMMAND}") async def remove_role(self, ctx: commands.Context) -> None: - """Removes the lovefest role.""" - user = ctx.author - role = ctx.guild.get_role(Lovefest.role_id) - if role not in ctx.author.roles: - await ctx.send("You dont have the lovefest role.") - else: - await user.remove_roles(role) - await ctx.send("The lovefest role has been successfully removed!") + """NOTE: This command has been moved to bot.""" + raise MovedCommandError(MOVED_COMMAND) @commands.cooldown(1, 1800, commands.BucketType.user) @commands.group(name="bemyvalentine", invoke_without_command=True) -- cgit v1.2.3 From 2d0760821b629bcb3269534ed46c0a9af4026264 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Fri, 3 Dec 2021 17:19:57 -0500 Subject: chore: remove subcommands entirely --- bot/exts/holidays/valentines/be_my_valentine.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/valentines/be_my_valentine.py b/bot/exts/holidays/valentines/be_my_valentine.py index a9a4731f..1e13e424 100644 --- a/bot/exts/holidays/valentines/be_my_valentine.py +++ b/bot/exts/holidays/valentines/be_my_valentine.py @@ -10,7 +10,6 @@ from bot.bot import Bot from bot.constants import Channels, Colours, Lovefest, Month, PYTHON_PREFIX from bot.utils.decorators import in_month from bot.utils.exceptions import MovedCommandError -from bot.utils.extensions import invoke_help_command log = logging.getLogger(__name__) @@ -33,7 +32,7 @@ class BeMyValentine(commands.Cog): return loads(p.read_text("utf8")) @in_month(Month.FEBRUARY) - @commands.group(name="lovefest", help=f"This command has been moved to {MOVED_COMMAND}") + @commands.command(name="lovefest", help=f"NOTE: This command has been moved to {MOVED_COMMAND}") async def lovefest_role(self, ctx: commands.Context) -> None: """ Subscribe or unsubscribe from the lovefest role. @@ -43,17 +42,6 @@ class BeMyValentine(commands.Cog): 1) use the command \".lovefest sub\" to get the lovefest role. 2) use the command \".lovefest unsub\" to get rid of the lovefest role. """ - if not ctx.invoked_subcommand: - await invoke_help_command(ctx) - - @lovefest_role.command(name="sub", help=f"This command has been moved to {MOVED_COMMAND}") - async def add_role(self, ctx: commands.Context) -> None: - """NOTE: This command has been moved to bot.""" - raise MovedCommandError(MOVED_COMMAND) - - @lovefest_role.command(name="unsub", help=f"This command has been moved to {MOVED_COMMAND}") - async def remove_role(self, ctx: commands.Context) -> None: - """NOTE: This command has been moved to bot.""" raise MovedCommandError(MOVED_COMMAND) @commands.cooldown(1, 1800, commands.BucketType.user) -- cgit v1.2.3 From cfb2cc69287f0aefab0937d0e8e8f99811d50948 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Fri, 3 Dec 2021 18:26:33 -0500 Subject: chore: update lovefest docstring to reflect deprecation --- bot/exts/holidays/valentines/be_my_valentine.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/valentines/be_my_valentine.py b/bot/exts/holidays/valentines/be_my_valentine.py index 1e13e424..1572d474 100644 --- a/bot/exts/holidays/valentines/be_my_valentine.py +++ b/bot/exts/holidays/valentines/be_my_valentine.py @@ -35,12 +35,9 @@ class BeMyValentine(commands.Cog): @commands.command(name="lovefest", help=f"NOTE: This command has been moved to {MOVED_COMMAND}") async def lovefest_role(self, ctx: commands.Context) -> None: """ - Subscribe or unsubscribe from the lovefest role. + Deprecated lovefest role command. - The lovefest role makes you eligible to receive anonymous valentines from other users. - - 1) use the command \".lovefest sub\" to get the lovefest role. - 2) use the command \".lovefest unsub\" to get rid of the lovefest role. + This command has been moved to bot, and will be removed in the future. """ raise MovedCommandError(MOVED_COMMAND) -- cgit v1.2.3 From 70b9ec7dbdc346e7ddcac7624968424cf0e66ef6 Mon Sep 17 00:00:00 2001 From: evgriff Date: Fri, 10 Dec 2021 19:09:31 +0000 Subject: Added the user's score to Candy Command according to #947 --- bot/exts/holidays/halloween/candy_collection.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/halloween/candy_collection.py b/bot/exts/holidays/halloween/candy_collection.py index bb9c93be..6edce1a0 100644 --- a/bot/exts/holidays/halloween/candy_collection.py +++ b/bot/exts/holidays/halloween/candy_collection.py @@ -173,6 +173,7 @@ class CandyCollection(commands.Cog): async def candy(self, ctx: commands.Context) -> None: """Get the candy leaderboard and save to JSON.""" records = await self.candy_records.items() + user = await self.bot.fetch_user(ctx.author.id) def generate_leaderboard() -> str: top_sorted = sorted( @@ -187,12 +188,23 @@ class CandyCollection(commands.Cog): for index, record in enumerate(top_five) ) if top_five else "No Candies" + def get_user_candy_score() -> str: + for user_id, score in records: + if user_id == user.id: + return f'<@{user.id}>: {score}' + return f'<@{user.id}>: 0' + e = discord.Embed(colour=discord.Colour.og_blurple()) e.add_field( name="Top Candy Records", value=generate_leaderboard(), inline=False ) + e.add_field( + name=f'{user.name}' + "'s Candy Score", + value=get_user_candy_score(), + inline=False + ) e.add_field( name="\u200b", value="Candies will randomly appear on messages sent. " -- cgit v1.2.3 From 43f1a14765133229053c06bd36517f2f5081f20d Mon Sep 17 00:00:00 2001 From: evgriff Date: Wed, 15 Dec 2021 17:52:49 +0000 Subject: Adding suggestions, removing erroneous commit --- bot/exts/holidays/halloween/candy_collection.py | 9 ++++----- bot/exts/utilities/issues.py | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/halloween/candy_collection.py b/bot/exts/holidays/halloween/candy_collection.py index 6edce1a0..9b16d543 100644 --- a/bot/exts/holidays/halloween/candy_collection.py +++ b/bot/exts/holidays/halloween/candy_collection.py @@ -173,7 +173,6 @@ class CandyCollection(commands.Cog): async def candy(self, ctx: commands.Context) -> None: """Get the candy leaderboard and save to JSON.""" records = await self.candy_records.items() - user = await self.bot.fetch_user(ctx.author.id) def generate_leaderboard() -> str: top_sorted = sorted( @@ -190,9 +189,9 @@ class CandyCollection(commands.Cog): def get_user_candy_score() -> str: for user_id, score in records: - if user_id == user.id: - return f'<@{user.id}>: {score}' - return f'<@{user.id}>: 0' + if user_id == ctx.author.id: + return f'<@{ctx.author.id}>: {score}' + return f'<@{ctx.author.id}>: 0' e = discord.Embed(colour=discord.Colour.og_blurple()) e.add_field( @@ -201,7 +200,7 @@ class CandyCollection(commands.Cog): inline=False ) e.add_field( - name=f'{user.name}' + "'s Candy Score", + name="Your Candy Score", value=get_user_candy_score(), inline=False ) diff --git a/bot/exts/utilities/issues.py b/bot/exts/utilities/issues.py index b3f8340c..b6d5a43e 100644 --- a/bot/exts/utilities/issues.py +++ b/bot/exts/utilities/issues.py @@ -139,7 +139,7 @@ class Issues(commands.Cog): log.trace(f"PR provided, querying GH pulls API for additional information: {pulls_url}") async with self.bot.http_session.get(pulls_url) as p: pull_data = await p.json() - if "draft" in pull_data and pull_data["draft"]: + if pull_data["draft"]: emoji = Emojis.pull_request_draft elif pull_data["state"] == "open": emoji = Emojis.pull_request_open -- cgit v1.2.3 From 85242f87d8c946b645d0ca50ed266a78dd51393d Mon Sep 17 00:00:00 2001 From: evgriff Date: Thu, 16 Dec 2021 20:06:44 +0000 Subject: user.mention --- bot/exts/holidays/halloween/candy_collection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/halloween/candy_collection.py b/bot/exts/holidays/halloween/candy_collection.py index 9b16d543..61dc8f97 100644 --- a/bot/exts/holidays/halloween/candy_collection.py +++ b/bot/exts/holidays/halloween/candy_collection.py @@ -190,8 +190,8 @@ class CandyCollection(commands.Cog): def get_user_candy_score() -> str: for user_id, score in records: if user_id == ctx.author.id: - return f'<@{ctx.author.id}>: {score}' - return f'<@{ctx.author.id}>: 0' + return f'{ctx.author.mention}: {score}' + return f'{ctx.author.mention}: 0' e = discord.Embed(colour=discord.Colour.og_blurple()) e.add_field( -- cgit v1.2.3 From e48fda5a7ae3509dd0ef7e591fedbf3dab125ba1 Mon Sep 17 00:00:00 2001 From: evgriff Date: Thu, 16 Dec 2021 20:09:12 +0000 Subject: Double quotes --- bot/exts/holidays/halloween/candy_collection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/exts/holidays') diff --git a/bot/exts/holidays/halloween/candy_collection.py b/bot/exts/holidays/halloween/candy_collection.py index 61dc8f97..729bbc97 100644 --- a/bot/exts/holidays/halloween/candy_collection.py +++ b/bot/exts/holidays/halloween/candy_collection.py @@ -190,8 +190,8 @@ class CandyCollection(commands.Cog): def get_user_candy_score() -> str: for user_id, score in records: if user_id == ctx.author.id: - return f'{ctx.author.mention}: {score}' - return f'{ctx.author.mention}: 0' + return f"{ctx.author.mention}: {score}" + return f"{ctx.author.mention}: 0" e = discord.Embed(colour=discord.Colour.og_blurple()) e.add_field( -- cgit v1.2.3