From 177fc6e7137c180bc4663cfe32cbf3eedb1973e9 Mon Sep 17 00:00:00 2001 From: imtherealF1 Date: Thu, 2 Jan 2025 18:10:29 +0200 Subject: [PATCH 1/4] plural check + a bit more readable code --- ballsdex/packages/balls/cog.py | 53 ++++++++++++++++------------------ 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/ballsdex/packages/balls/cog.py b/ballsdex/packages/balls/cog.py index 1d8dd1c9..e30e6aee 100644 --- a/ballsdex/packages/balls/cog.py +++ b/ballsdex/packages/balls/cog.py @@ -670,57 +670,54 @@ async def duplicate( type: DuplicateType Type of duplicate to check (countryballs or specials). """ - await interaction.response.defer(ephemeral=True) - user_id = interaction.user.id - player, _ = await Player.get_or_create(discord_id=user_id) + await interaction.response.defer(thinking=True, ephemeral=True) + + player, _ = await Player.get_or_create(discord_id=interaction.user.id) await player.fetch_related("balls") is_special = type.value == "specials" queryset = BallInstance.filter(player=player) - - if type.value == "specials": + + if is_special: queryset = queryset.filter(special_id__isnull=False).prefetch_related("special") - annotations = { - "name": "special__name", - "emoji": "special__emoji", - } + annotations = {"name": "special__name", "emoji": "special__emoji"} + title = "special" limit = 5 - title = "specials" else: queryset = queryset.filter(ball__tradeable=True) - annotations = { - "name": "ball__country", - "emoji": "ball__emoji_id", - } + annotations = {"name": "ball__country", "emoji": "ball__emoji_id"} + title = settings.collectible_name limit = 50 - title = settings.plural_collectible_name - - queryset = ( - queryset.annotate(count=Count("id")) - .group_by(*annotations.values()) - .order_by("-count") - .limit(limit) - .values(*annotations.values(), "count") + + results = ( + await queryset.annotate(count=Count("id")).group_by(*annotations.values() + ).order_by("-count").limit(limit).values(*annotations.values(), "count") ) - results = await queryset if not results: await interaction.followup.send( - f"You don't have any {type.value} duplicates in your inventory!", - ephemeral=True, + f"You don't have any {type.value} duplicates in your inventory.", ephemeral=True ) return entries = [ ( f"{i + 1}. {item[annotations['name']]} " - f"{self.bot.get_emoji(item[annotations['emoji']]) or item[annotations['emoji']]}", - f"Count: {item['count']}", + f"{self.bot.get_emoji(item[annotations['emoji']]) or item[annotations['emoji']]}", + f"Count: {item['count']}" ) for i, item in enumerate(results) ] + + embed_title = ( + f"Top {len(results)} duplicate {title}s:" + if len(results) > 1 + else f"Top {len(results)} duplicate {title}" + ) + source = FieldPageSource(entries, per_page=5 if is_special else 10, inline=False) - source.embed.title = f"Top {len(results)} duplicate {title}" + source.embed.title = embed_title source.embed.color = discord.Color.purple() if is_special else discord.Color.blue() source.embed.set_thumbnail(url=interaction.user.display_avatar.url) + paginator = Pages(source, interaction=interaction) await paginator.start(ephemeral=True) From 225d08991d7797cb9177669cba10e902b6243561 Mon Sep 17 00:00:00 2001 From: imtherealF1 Date: Thu, 2 Jan 2025 18:18:14 +0200 Subject: [PATCH 2/4] appease pre-commit --- ballsdex/packages/balls/cog.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ballsdex/packages/balls/cog.py b/ballsdex/packages/balls/cog.py index e30e6aee..84e9ad73 100644 --- a/ballsdex/packages/balls/cog.py +++ b/ballsdex/packages/balls/cog.py @@ -676,7 +676,7 @@ async def duplicate( await player.fetch_related("balls") is_special = type.value == "specials" queryset = BallInstance.filter(player=player) - + if is_special: queryset = queryset.filter(special_id__isnull=False).prefetch_related("special") annotations = {"name": "special__name", "emoji": "special__emoji"} @@ -702,8 +702,8 @@ async def duplicate( entries = [ ( f"{i + 1}. {item[annotations['name']]} " - f"{self.bot.get_emoji(item[annotations['emoji']]) or item[annotations['emoji']]}", - f"Count: {item['count']}" + f"{self.bot.get_emoji(item[annotations['emoji']]) or item[annotations['emoji']]}", + f"Count: {item['count']}", ) for i, item in enumerate(results) ] From a02a89f8254b33dd6fc7aae08da4924297d07561 Mon Sep 17 00:00:00 2001 From: imtherealF1 Date: Thu, 2 Jan 2025 18:23:52 +0200 Subject: [PATCH 3/4] declutter /balls count code --- ballsdex/packages/balls/cog.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ballsdex/packages/balls/cog.py b/ballsdex/packages/balls/cog.py index 84e9ad73..e49d460d 100644 --- a/ballsdex/packages/balls/cog.py +++ b/ballsdex/packages/balls/cog.py @@ -637,6 +637,7 @@ async def count( """ if interaction.response.is_done(): return + assert interaction.guild filters = {} if countryball: @@ -646,12 +647,15 @@ async def count( if current_server: filters["server_id"] = interaction.guild.id filters["player__discord_id"] = interaction.user.id + await interaction.response.defer(ephemeral=True, thinking=True) + balls = await BallInstance.filter(**filters).count() country = f"{countryball.country} " if countryball else "" plural = "s" if balls > 1 or balls == 0 else "" special_str = f"{special.name} " if special else "" guild = f" caught in {interaction.guild.name}" if current_server else "" + await interaction.followup.send( f"You have {balls} {special_str}" f"{country}{settings.collectible_name}{plural}{guild}." From 489c26d522d422d4337d61ef5f1e259ce919192c Mon Sep 17 00:00:00 2001 From: imtherealF1 Date: Thu, 2 Jan 2025 18:25:40 +0200 Subject: [PATCH 4/4] correctly appease pre-commit --- ballsdex/packages/balls/cog.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ballsdex/packages/balls/cog.py b/ballsdex/packages/balls/cog.py index e49d460d..f991759a 100644 --- a/ballsdex/packages/balls/cog.py +++ b/ballsdex/packages/balls/cog.py @@ -693,8 +693,11 @@ async def duplicate( limit = 50 results = ( - await queryset.annotate(count=Count("id")).group_by(*annotations.values() - ).order_by("-count").limit(limit).values(*annotations.values(), "count") + await queryset.annotate(count=Count("id")) + .group_by(*annotations.values()) + .order_by("-count") + .limit(limit) + .values(*annotations.values(), "count") ) if not results: