Skip to content

Commit

Permalink
[Discord] Add brawlhalla player command
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed Jul 30, 2023
1 parent 7b5970b commit 8ec5c27
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Discord/cogs/brawlhalla.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from discord.ext import commands

import sys
from typing import Optional

from utilities import checks
from utilities.converters import SteamID64

sys.path.insert(0, "..")
from units.cache import async_cache
Expand Down Expand Up @@ -102,3 +104,60 @@ async def all_legends(self):

return legends

@brawlhalla.command()
async def player(self, ctx, name: SteamID64, legend: Optional[str] = None):
"""
Show information about a Brawlhalla player
Parameters
----------
name
Name of player to show information about
legend
Specific legend to show player information about
"""
brawlhalla_id = await self.brawlhalla_id(name)
async with self.bot.aiohttp_session.get(
f"https://api.brawlhalla.com/player/{brawlhalla_id}/stats",
params = {"api_key": self.bot.BRAWLHALLA_API_KEY}
) as resp:
stats = await resp.json()

if legend:
for legend_stats in stats["legends"]:
if legend_stats["legend_name_key"] == legend.lower():
await ctx.embed_reply(
title = stats["name"],
fields = (
("Level", legend_stats["level"]),
)
)
return
await ctx.embed_reply(
f"{ctx.bot.error_emoji} {legend} information for {stats['name']} not found"
)
else:
await ctx.embed_reply(
title = stats["name"],
fields = (
("Level", stats["level"]),
("XP", format(stats["xp"], ',')),
(
"Wins",
f"{stats['wins']} / {stats['games']} "
f"({stats['wins'] / stats['games'] * 100:.2f}%)"
),
("Clan", stats["clan"]["clan_name"])
)
)

@async_cache
async def brawlhalla_id(self, steam_id):
async with self.bot.aiohttp_session.get(
"https://api.brawlhalla.com/search",
params = {
"steamid": steam_id, "api_key": self.bot.BRAWLHALLA_API_KEY
}
) as resp:
return (await resp.json())["brawlhalla_id"]

0 comments on commit 8ec5c27

Please sign in to comment.