Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Fix /until crashing if the max rank has already been reached #159

Merged
merged 1 commit into from
Jan 4, 2022
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
11 changes: 8 additions & 3 deletions buttercup/cogs/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ def get_history_data_from_rate_data(
return rate_data.assign(gamma=rate_data.expanding(1).sum() + offset)


def get_next_rank(gamma: int) -> Dict[str, Union[str, int]]:
def get_next_rank(gamma: int) -> Optional[Dict[str, Union[str, int]]]:
"""Determine the next rank based on the current gamma."""
for rank in ranks:
if rank["threshold"] > gamma:
return rank

# TODO: How to handle if the user reached the highest rank?
return None


def parse_goal_str(goal_str: str) -> Tuple[int, str]:
Expand Down Expand Up @@ -858,7 +858,12 @@ async def _until(
elif user:
# Take the next rank for the user
next_rank = get_next_rank(user["gamma"])
goal_gamma, goal_str = parse_goal_str(next_rank["name"])
if next_rank:
goal_gamma, goal_str = parse_goal_str(next_rank["name"])
else:
# If the user has reached the maximum rank, take the next 10,000 tier
goal_gamma = ((user["gamma"] + 10_000) // 10_000) * 10_000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought "surely there's a better way to do this" and then I looked it up, and there is not a better way to do this. Works for me!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It definitely felt stupid to write, but I couldn't think of a better way

goal_str = f"{goal_gamma:,}"
else:
# You can't get the "next rank" of the whole server
raise InvalidArgumentException("goal", "<empty>")
Expand Down