aboutsummaryrefslogtreecommitdiff
path: root/minecrafttrivia/utils.py
blob: 89f650de21338a018e8872567a7cc4dc06c7cee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import typing

import discord

from . import constants


async def get_participants(reactions: typing.List[discord.Reaction]) -> typing.List[discord.User]:
	for r in reactions:
		if r.emoji == constants.POSITIVE_REACTION:
			users = []
			async for u in r.users():
				if not u.bot:
					users.append(u)
			return users
	return []


def format_leaderboard(points: typing.List[typing.Tuple[int, typing.Tuple[typing.Union[discord.User, int], int]]]) -> str:
	return "\n".join(f"**{rank + 1}.** {user.mention if hasattr(user, 'mention') else '<@' + str(user) + '>'} - {points}" for rank, (user, points) in points[:20])


_T = typing.TypeVar("_T", discord.User, int)


def create_leaderboard(points: typing.Dict[_T, int]) -> typing.List[typing.Tuple[int, typing.Tuple[_T, int]]]:
	return list(enumerate(reversed(sorted(points.items(), key=lambda x: x[1]))))