Repository navigation
Expand file tree
/
Copy pathtespa.py
More file actions
116 lines (92 loc) · 4.67 KB
/
Copy pathtespa.py
File metadata and controls
116 lines (92 loc) · 4.67 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import discord
from discord.ext import commands
import json
from APIConnection import APIConnection
from emotes import Emotes
from tespateam import TespaTeam
class Tespa(commands.Cog):
def __init__(self, client):
self.client = client
self.emotes = Emotes()
def getHighestRole(self, ratingsData):
return max(ratingsData, key=lambda r: r["level"])["role"]
# NOTE the following line takes care of usernames with unicode in them (ex: Phönix#11157)
# the API returns them formatted wrong (Phönix#11157)
# encoding to 'windows-1252' instead of 'ascii' just works
def unicodeFix(self, str):
return str.encode('windows-1252').decode("utf-8")
@commands.command(aliases=["tespa"])
@commands.guild_only()
async def _tespa(self, ctx, url):
await ctx.send('Getting team profile...')
data = APIConnection.getGameBattleTeam(url)
team = TespaTeam()
embed = discord.Embed(
title="__Team Information__",
color=16746752,
)
if "body" in data:
for teamMember in data["body"]:
currentGamerTag = teamMember["teamMember"]["gamertag"]
print('teamMember: ' + currentGamerTag)
if "userGamertag" in teamMember:
currentGamerTag = teamMember["userGamertag"]["gamertag"]
embedName = ""
if(teamMember["teamMember"]["status"] != "ELIGIBLE"):
embedName += " (⚠️ Not Eligible) "
# GameBattles assigns ID 3 to BattleTag gamertags
if (currentGamerTag):
#NOTE I left the rest of the code unchanged except for these couple of lines for the future in case we need to change it
#fixedGamerTag = self.unicodeFix(currentGamerTag)
fixedGamerTag = currentGamerTag
battleTag = fixedGamerTag
btag = battleTag.split('#')
response = APIConnection.SendRequest(btag)
embedName += battleTag
if response != None:
currentPlayer = json.loads(response)
output = ""
if (currentPlayer["private"]):
output = "🔒 Private profile."
output += "\n"
elif (not currentPlayer["ratings"]):
output = "❌ No placements."
output += "\n"
else:
for roleData in currentPlayer["ratings"]:
output += self.emotes.getRoleEmote(roleData["role"])
output += " "
output += self.emotes.getRankEmote(roleData["level"])
output += " "
output += str(roleData["level"])
if(self.getHighestRole(currentPlayer["ratings"]) == roleData["role"]):
output += " ⭐"
team.addPlayer(roleData["level"], roleData["role"])
output += "\n"
output += f"[View Profile ↗️](https://playoverwatch.com/en-us/career/pc/{battleTag.replace('#', '-')})"
embed.add_field(name=embedName, value=output, inline=False)
else:
embed.add_field(name=embedName, value="❔ Couldn't get player data.", inline=False)
else:
embedName += teamMember["teamMember"]["guid"]
embed.add_field(name=embedName, value="❔❔ This GameBattles user doesn't have a valid GamerTag.", inline=False)
topAvg = team.getTopSixAverage()
overallAvg = team.getTeamAverage()
output = ""
output += "Top 6 (top 2 per role): "
output += self.emotes.getRankEmote(topAvg)
output += " "
if(topAvg > 0):
output += str(topAvg)
output += "\n"
output += "Overall team: "
output += self.emotes.getRankEmote(overallAvg)
output += " "
if(overallAvg > 0):
output += str(overallAvg)
embed.insert_field_at(index=0, name='Team Average', value=output, inline=False)
else:
embed.add_field(name="Error.", value="❔ Couldn't get team data. Make sure the link is the team's page.", inline=False)
await ctx.send(embed=embed)
def setup(client):
client.add_cog(Tespa(client))