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

[balls duplicate] Add plural check, remove redundant variables/logic #495

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 24 additions & 20 deletions ballsdex/packages/balls/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ async def count(
"""
if interaction.response.is_done():
return

assert interaction.guild
filters = {}
if countryball:
Expand All @@ -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}."
Expand All @@ -670,43 +674,35 @@ 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"))
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

Expand All @@ -718,9 +714,17 @@ async def duplicate(
)
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)
Loading