From b46720e99183feb4ad2bef3f3a18d98af2dc7909 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:50:38 +0530 Subject: [PATCH 1/3] Add `framework.isobot.CurrencyAPI` method to fetch all cached user ids from currency database --- framework/isobot/currency.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/framework/isobot/currency.py b/framework/isobot/currency.py index 4d28ccf..5363156 100644 --- a/framework/isobot/currency.py +++ b/framework/isobot/currency.py @@ -31,7 +31,8 @@ class CurrencyAPI(Colors): - get_user_count - new_wallet(user) - new_bank(user) - - delete_user(user)""" + - delete_user(user) + - fetch_all_cached_user_ids()""" def __init__(self, db_path: str, log_path: str): self.db_path = db_path @@ -206,3 +207,11 @@ def delete_user(self, user: int) -> int: f.write(f'{self.get_time()} framework.isobot.currency User({user}): Successfully deleted all user data from currency database.\n') f.close() return 0 + + def fetch_all_cached_user_ids(self) -> list: + """Fetches the ids of all cached users in the currency database, and returns it as a `list`.\n\n(uses database's `wallet` property to fetch ids)""" + currency = self.load() + all_user_ids = list() + for uid in currency["wallet"]: + all_user_ids.append(str(uid)) + return all_user_ids From 3348daebd0b574dca201c00e2b3d7687651f0552 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:54:44 +0530 Subject: [PATCH 2/3] Fix fetching of user context data from API and re-enable `/leaderboard_nw` --- cogs/economy.py | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/cogs/economy.py b/cogs/economy.py index 7d98c23..69c8929 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -642,31 +642,29 @@ async def treasury(self, ctx: ApplicationContext): description="View the global leaderboard for net worth." ) async def leaderboard_nw(self, ctx: ApplicationContext): - await ctx.respond("This command is currently disabled due to an internal issue. I apologize for the inconvenience.", ephemeral=True) - #await ctx.defer() - #nw_dict = dict() - #for person in currency["wallet"]: - # nw_dict[str(person)] = int(currency["wallet"][str(person)]) + int(currency["bank"][str(person)]) - #undicted_leaderboard = sorted(nw_dict.items(), key=lambda x:x[1], reverse=True) - #dicted_leaderboard = dict(undicted_leaderboard) - #parsed_output = str() - #y = 1 - #for i in dicted_leaderboard: - # if y < 10: - # try: - # if nw_dict[i] != 0: - # user_context = await discord.ext.commands.Bot.fetch_user(self, int(i)) - # if not user_context.bot: - # print(i, nw_dict[i]) - # if y == 1: yf = ":first_place:" - # elif y == 2: yf = ":second_place:" - # elif y == 3: yf = ":third_place:" - # else: yf = f"#{y}" - # parsed_output += f"{yf} **{user_context.name}:** {nw_dict[i]} coins\n" - # y += 1 - # except discord.errors.NotFound: continue - #localembed = discord.Embed(title="Global net worth leaderboard", description=parsed_output, color=color) - #await ctx.respond(embed=localembed) + await ctx.defer() + nw_dict = dict() + for person in currency.fetch_all_cached_user_ids(): + nw_dict[str(person)] = currency.get_wallet(person) + currency.get_bank(person) + undicted_leaderboard = sorted(nw_dict.items(), key=lambda x:x[1], reverse=True) + dicted_leaderboard = dict(undicted_leaderboard) + parsed_output = str() + y = 1 + for i in dicted_leaderboard: + if y < 10: + try: + if nw_dict[i] != 0: + user_context = await ctx.bot.fetch_user(i) + if not user_context.bot: + if y == 1: yf = ":first_place:" + elif y == 2: yf = ":second_place:" + elif y == 3: yf = ":third_place:" + else: yf = f"#{y}" + parsed_output += f"{yf} **{user_context.name}:** {nw_dict[i]} coins\n" + y += 1 + except discord.errors.NotFound: continue + localembed = discord.Embed(title="Global net worth leaderboard", description=parsed_output, color=color) + await ctx.respond(embed=localembed) @commands.slash_command( name="hack", From 0112062b8cf89da5897dd6d27c666cc0cad6b72c Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:59:31 +0530 Subject: [PATCH 3/3] Make variables for `/leaderboard_nw` make more sense --- cogs/economy.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cogs/economy.py b/cogs/economy.py index 69c8929..765089c 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -649,19 +649,19 @@ async def leaderboard_nw(self, ctx: ApplicationContext): undicted_leaderboard = sorted(nw_dict.items(), key=lambda x:x[1], reverse=True) dicted_leaderboard = dict(undicted_leaderboard) parsed_output = str() - y = 1 - for i in dicted_leaderboard: - if y < 10: + user_count = 1 + for uid in dicted_leaderboard: + if user_count < 10: try: - if nw_dict[i] != 0: - user_context = await ctx.bot.fetch_user(i) + if nw_dict[uid] != 0: + user_context = await ctx.bot.fetch_user(uid) if not user_context.bot: - if y == 1: yf = ":first_place:" - elif y == 2: yf = ":second_place:" - elif y == 3: yf = ":third_place:" - else: yf = f"#{y}" - parsed_output += f"{yf} **{user_context.name}:** {nw_dict[i]} coins\n" - y += 1 + if user_count == 1: yf = ":first_place:" + elif user_count == 2: yf = ":second_place:" + elif user_count == 3: yf = ":third_place:" + else: yf = f"#{user_count}" + parsed_output += f"{yf} **{user_context.name}:** {nw_dict[uid]} coins\n" + user_count += 1 except discord.errors.NotFound: continue localembed = discord.Embed(title="Global net worth leaderboard", description=parsed_output, color=color) await ctx.respond(embed=localembed)