Improved 'leaderboard' command embed formatting

- Added proper vote count to leaderboard
- Supported shared rankings in leaderboard
- New generator in helpers 'ending_iterator'
This commit is contained in:
Xevion
2021-02-18 09:31:16 -06:00
parent eb93b9f3bf
commit 63f4c594b7
2 changed files with 29 additions and 9 deletions

View File

@@ -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.'

View File

@@ -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