From 63f4c594b7bc85382f7eecfd9fc29b28e052a28f Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 18 Feb 2021 09:31:16 -0600 Subject: [PATCH] Improved 'leaderboard' command embed formatting - Added proper vote count to leaderboard - Supported shared rankings in leaderboard - New generator in helpers 'ending_iterator' --- bot/cogs/contest_commands.py | 17 +++++++++++------ bot/helpers.py | 21 ++++++++++++++++++--- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/bot/cogs/contest_commands.py b/bot/cogs/contest_commands.py index 5176b74..8d9469e 100644 --- a/bot/cogs/contest_commands.py +++ b/bot/cogs/contest_commands.py @@ -259,15 +259,20 @@ class ContestCommandsCog(commands.Cog, name='Contest'): .all() description = '' - for i, submission in enumerate(board, start=1): + position = 0 # It will be incremented on the first step, so we start at 0. + emotes = helpers.ending_iterator([':trophy:', ':second_place:', ':third_place:', '']) + emote, previous_count = None, None + + for submission in board: message = self.bot.get_message(guild.submission_channel, submission.id) - emote = '' - if i == 1: emote = ':trophy:' - elif i == 2: emote = ':second_place:' - elif i == 3: emote = ':third_place:' + if submission.count != previous_count: + emote = next(emotes) + previous_count = submission.count + position += 1 - description += f'`{str(i).zfill(2)}` {emote + " " if emote else ""}<@{submission.user}> [Jump]({message.jump_url})\n' + description += f'`{str(position).zfill(2)}` {emote + " " if emote else ""}<@{submission.user}> with {submission.count} ' \ + f'vote{"s" if submission.count != 1 else ""} [Jump]({message.jump_url})\n' if not description: description = 'No one has submitted anything yet.' diff --git a/bot/helpers.py b/bot/helpers.py index 484b73f..dab1bba 100644 --- a/bot/helpers.py +++ b/bot/helpers.py @@ -1,5 +1,5 @@ import datetime -from typing import Union +from typing import Any, Generator, List, Union import discord @@ -14,9 +14,11 @@ def is_upvote(emoji: Union[discord.Emoji, discord.PartialEmoji, str]) -> bool: return False -def general_embed(title: str = '', message: str = '', color: discord.Color = constants.GENERAL_COLOR, timestamp: bool = False) -> discord.Embed: +def general_embed(title: str = '', message: str = '', color: discord.Color = constants.GENERAL_COLOR, + timestamp: bool = False) -> discord.Embed: """A generic mostly unstyled embed with a blue color.""" - return discord.Embed(title=title, description=message, timestamp=datetime.datetime.utcnow() if timestamp else discord.Embed.Empty, color=color) + return discord.Embed(title=title, description=message, timestamp=datetime.datetime.utcnow() if timestamp else discord.Embed.Empty, + color=color) def error_embed(*args, **kwargs): @@ -29,3 +31,16 @@ def success_embed(*args, **kwargs): """A generic embed with a light green color.""" kwargs['color'] = constants.SUCCESS_COLOR return general_embed(*args, **kwargs) + + +def ending_iterator(items: List[Any]) -> Generator[Any, None, None]: + """A generator which iterates along the list until it reaches the end, where it continuously yields the final item forever.""" + index = 0 + length = len(items) - 1 + last = items[-1] + while True: + if index == length: + yield last + else: + yield items[index] + index += 1