/
faceit.py
385 lines (350 loc) · 16.1 KB
/
faceit.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from datetime import datetime
from io import BytesIO
from typing import Literal
import aiohttp
import discord
from redbot.core import Config, commands
from redbot.core.utils.chat_formatting import humanize_timedelta
from redbot.core.utils.menus import DEFAULT_CONTROLS, close_menu, menu, next_page, prev_page
from .converters import StrUser
from .funcs import account_matches, account_ongoing, account_stats, match_info
async def tokencheck(ctx):
token = await ctx.bot.get_shared_api_tokens("faceit")
return bool(token.get("authorization"))
controls = {
"⬅": prev_page,
"❌": close_menu,
"➡": next_page,
"\N{INFORMATION SOURCE}\N{VARIATION SELECTOR-16}": match_info,
}
profile_controls = {
"\N{SPORTS MEDAL}": account_stats,
"\N{CROSSED SWORDS}\N{VARIATION SELECTOR-16}": account_matches,
"❌": close_menu,
}
profile_controls_ongoing = {
"\N{SPORTS MEDAL}": account_stats,
"\N{CROSSED SWORDS}\N{VARIATION SELECTOR-16}": account_matches,
"\N{VIDEO GAME}": account_ongoing,
"❌": close_menu,
}
class Faceit(commands.Cog):
"""CS:GO Faceit Statistics."""
__version__ = "0.0.9"
def format_help_for_context(self, ctx):
"""Thanks Sinbad."""
pre_processed = super().format_help_for_context(ctx)
return f"{pre_processed}\nCog Version: {self.__version__}"
def __init__(self, bot):
self.bot = bot
self.api = "https://open.faceit.com/data/v4"
self._session = aiohttp.ClientSession()
self.config = Config.get_conf(self, 95932766180343808, force_registration=True)
self.config.register_user(name=None)
self.token = None
async def red_get_data_for_user(self, *, user_id: int):
name = await self.config.user_from_id(user_id).name()
if name is None:
return {}
contents = f"Faceit Account for Discord user with ID {user_id}:\n- Name: {name}\n"
return {"user_data.txt": BytesIO(contents.encode())}
async def red_delete_data_for_user(
self,
*,
requester: Literal["discord_deleted_user", "owner", "user", "user_strict"],
user_id: int,
):
await self.config.user_from_id(user_id).clear()
def cog_unload(self):
self.bot.loop.create_task(self._session.close())
async def initalize(self):
token = await self.bot.get_shared_api_tokens("faceit")
self.token = token.get("authorization")
async def get(self, url):
async with self._session.get(
self.api + url, headers={"authorization": "bearer {}".format(self.token)}
) as resp:
if resp.status == 200:
return await resp.json()
if resp.status == 401:
return {"error": "Authorization Failed - API Key may be invalid."}
return await resp.json()
async def get_ongoing(self, _id):
async with self._session.get(
"https://api.faceit.com/match/v1/matches/groupByState?userId=" + _id
) as resp:
if resp.status == 200:
return await resp.json()
if resp.status == 401:
return {"error": "Authorization Failed - API Key may be invalid."}
return await resp.json()
async def get_userid(self, username) -> str:
userid = await self.get("/players?nickname={}".format(username))
if userid.get("error"):
return {"failed": userid.get("error")}
if userid.get("errors"):
return {"failed": userid.get("errors")[0]["message"]}
else:
return userid["player_id"]
@commands.Cog.listener()
async def on_red_api_tokens_update(self, service_name, api_tokens):
if service_name == "faceit":
self.token = api_tokens.get("authorization")
async def get_user(self, ctx, user):
if user is None:
name = await self.config.user(ctx.author).name()
if name is None:
await ctx.send(
"You don't have a valid account linked, check {}faceit set.".format(ctx.prefix)
)
return False
elif isinstance(user, discord.User):
name = await self.config.user(user).name()
if name is None:
name = await self.get_userid(user.name)
if isinstance(name, dict):
await ctx.send(name["failed"])
return False
else:
name = await self.get_userid(user)
if isinstance(name, dict):
await ctx.send(name["failed"])
return False
return name
@commands.is_owner()
@commands.command()
async def faceitset(self, ctx):
"""Help on setting up faceit cog."""
msg = (
"How to setup the faceit cog.\n"
"**1** - Visit https://developers.faceit.com and login or create a faceit account.\n"
"**2** - Create an application at https://developers.faceit.com/apps\n"
"**3** - Head over to the app you just created and click on the 'API KEYS' tab. Under this, click create then create a **SERVER SIDE** key.\n"
"**4** - Use the following command with your key.\n{}set api faceit authorization <API_KEY_HERE>".format(
ctx.prefix
)
)
await ctx.maybe_send_embed(msg)
@commands.group()
@commands.check(tokencheck)
async def faceit(self, ctx):
"""Faceit Commands."""
@faceit.command(name="set")
async def _set(self, ctx, *, name: str = None):
"""Set your faceit username."""
if name is None:
await self.config.user(ctx.author).name.set(name)
await ctx.send("Your account link has been reset.")
uname = await self.get_userid(name)
if isinstance(uname, dict):
await ctx.send(uname["failed"])
return
await ctx.send(
"Your account {} has been linked to the Faceit ID `{}`.".format(name, uname)
)
await self.config.user(ctx.author).name.set(uname)
@faceit.command()
async def profile(self, ctx, *, user: StrUser = None):
"""Faceit Profile Stats."""
name = await self.get_user(ctx, user)
if name is False:
return
profilestats = await self.get("/players/{}".format(name))
if profilestats.get("error"):
return await ctx.send(profilestats.get("error"))
if profilestats.get("errors"):
return await ctx.send(profilestats.get("errors")[0]["message"])
ongoing = await self.is_ongoing(ctx, name, False)
msg = "\nPress the \N{SPORTS MEDAL} button for the first game statistics.\nPress the \N{CROSSED SWORDS}\N{VARIATION SELECTOR-16} button for the most recent matches."
if ongoing:
msg += "\nPress the \N{VIDEO GAME}\N{VARIATION SELECTOR-16} button for information about the current ongoing match"
embed = discord.Embed(
color=ctx.author.color,
title="Faceit Profile for {}".format(profilestats["nickname"]),
description=msg,
url=profilestats["faceit_url"].format(lang=profilestats["settings"]["language"]),
)
embed.set_thumbnail(url=profilestats["avatar"])
accinfo = f"**Nickname**: {profilestats['nickname']}\n**Membership**: {profilestats['membership_type'].title()}"
embed.add_field(name="Account Information", value=accinfo)
steaminfo = f"**Steam Nickname**: {profilestats['steam_nickname']}\n**Steam ID 64**: {profilestats['steam_id_64']}\n**New Steam ID**: {profilestats['new_steam_id']}"
embed.add_field(name="Steam Information", value=steaminfo)
infractioninfo = f"**AFK(s)**: {profilestats['infractions']['afk']}\n**Leaves**: {profilestats['infractions']['leaver']}\n**Not Checked In**: {profilestats['infractions']['qm_not_checkedin']}\n**Not Voted**: {profilestats['infractions']['qm_not_voted']}"
if profilestats["infractions"]["last_infraction_date"] != "":
infractioninfo += f"\n**Last Infraction Date**: {profilestats['infractions']['last_infraction_date']}"
embed.add_field(name="Infraction Information", value=infractioninfo)
for game in profilestats["games"]:
embed.add_field(
name=game.title(),
value=f"**Region**: {profilestats['games'][game]['region']}\n**Skill Level**: {profilestats['games'][game]['skill_level']}\n**ELO**: {profilestats['games'][game]['faceit_elo']}",
)
embed.set_author(name=profilestats["nickname"], icon_url=profilestats["avatar"])
await menu(
ctx, [embed], profile_controls if not ongoing else profile_controls_ongoing, timeout=30
)
@faceit.command()
async def matches(self, ctx, *, user: StrUser = None):
"""Faceit Match Stats."""
name = await self.get_user(ctx, user)
if name is False:
return
profilestats = await self.get("/players/{}/history".format(name))
if profilestats.get("error"):
return await ctx.send(profilestats.get("error"))
if profilestats.get("errors"):
return await ctx.send(profilestats.get("errors")[0]["message"])
embeds = []
for i, game in enumerate(profilestats["items"], 1):
teams = {
"faction1": game["teams"]["faction1"]["nickname"],
"faction2": game["teams"]["faction2"]["nickname"],
}
embed = discord.Embed(
title=f"{game['competition_name']} - {teams['faction1']} vs {teams['faction2']}",
description=f"Winner: **{teams[game['results']['winner']]}**\n[Match Room - Click Here]({game['faceit_url']})\n\nClick on the \N{INFORMATION SOURCE}\N{VARIATION SELECTOR-16} button below for more detailed statistics.",
timestamp=datetime.fromtimestamp(game["started_at"]),
)
embed.add_field(name="Match ID", value=game["match_id"], inline=False)
embed.set_footer(
text=f"Game {i}/20 - Duration: {humanize_timedelta(seconds=game['finished_at'] - game['started_at'])}"
)
for team in teams:
players = []
for player in game["teams"][team]["players"]:
players.append(
f"[{player['nickname']}]({player['faceit_url'].format(lang='en')})"
)
embed.add_field(name=f"{teams[team]} Players", value="\n".join(players))
embeds.append(embed)
if embeds:
await menu(ctx, embeds, controls, timeout=30)
else:
await ctx.send("No information for previous matches found.")
@faceit.command()
async def match(self, ctx, match_id):
"""In-depth stats for a match."""
match = await self.get("/matches/{}/stats".format(match_id))
if match.get("error"):
return await ctx.send(match.get("error"))
if match.get("errors"):
return await ctx.send(match.get("errors")[0]["message"])
match = match["rounds"]
embeds = []
teams = {
match[0]["teams"][0]["team_id"]: match[0]["teams"][0]["team_stats"]["Team"],
match[0]["teams"][1]["team_id"]: match[0]["teams"][1]["team_stats"]["Team"],
}
embed = discord.Embed(
title=" vs ".join(teams.values()) + " Statistics",
description=f"**Winner**: {teams[match[0]['round_stats']['Winner']]}\n**Map**: {match[0]['round_stats']['Map']}\n**Score**: {match[0]['round_stats']['Score']}",
)
team1, team2 = [team for team in teams.values()]
team1stats = sorted(match[0]["teams"][0]["team_stats"].items())
team2stats = sorted(match[0]["teams"][1]["team_stats"].items())
embed.add_field(
name=f"{team1} Team Stats",
value="\n".join([f"**{item[0]}**: {item[1]}" for item in team1stats]),
)
embed.add_field(
name=f"{team2} Team Stats",
value="\n".join([f"**{item[0]}**: {item[1]}" for item in team2stats]),
)
embed.set_footer(text="Page 1/3")
embeds.append(embed)
for i, team in enumerate(match[0]["teams"], 2):
embed = discord.Embed(title=team["team_stats"]["Team"] + " Player Statistics")
for player in team["players"]:
playerstats = sorted(player["player_stats"].items())
embed.add_field(
name=player["nickname"],
value="\n".join([f"**{item[0]}**: {item[1]}" for item in playerstats]),
)
embed.add_field(name="\u200b", value="\u200b")
embed.set_footer(text=f"Page {i}/3")
embeds.append(embed)
if embeds:
await menu(ctx, embeds, DEFAULT_CONTROLS, timeout=60)
else:
await ctx.send("No information for match found.")
@faceit.command()
async def stats(self, ctx, game, *, user: StrUser = None):
"""In-depth stats for any faceit supported game."""
name = await self.get_user(ctx, user)
if name is False:
return
stats = await self.get("/players/{}/stats/{}".format(name, game))
if stats.get("error"):
return await ctx.send(stats.get("error"))
if stats.get("errors"):
return await ctx.send(stats.get("errors")[0]["message"])
embeds = []
recent_results = [
("W" if int(game) else "L") for game in stats["lifetime"]["Recent Results"]
]
msg = "**Recent Results**: " + " ".join(recent_results) + "\n"
msg += "\n".join(
[
f"**{item[0]}**: {item[1]}"
for item in sorted(stats["lifetime"].items())
if item[0] != "Recent Results"
]
)
embed = discord.Embed(title=f"{game.title()} stats for {name}", description=msg)
embed.set_footer(text=f"Page 1/{len(stats['segments']) + 1}")
embeds.append(embed)
for i, segment in enumerate(stats["segments"], 2):
embed = discord.Embed(
title=f"{segment['label']} statistics",
description="\n".join(
[f"**{item[0]}**: {item[1]}" for item in sorted(segment["stats"].items())]
),
)
embed.set_thumbnail(url=segment["img_regular"])
embed.set_footer(text=f"Page {i}/{len(stats['segments']) + 1}")
embeds.append(embed)
if embeds:
await menu(ctx, embeds, DEFAULT_CONTROLS, timeout=30)
else:
await ctx.send("No information found.")
@faceit.command()
async def ongoing(self, ctx, *, user: StrUser = None):
"""Check if a user has an ongoing game."""
name = await self.get_user(ctx, user)
if name is False:
return
ongoing = await self.is_ongoing(ctx, name)
if ongoing is False:
return
ongoing = ongoing[0]
team1, team2 = ongoing["teams"]["faction1"], ongoing["teams"]["faction2"]
embed = discord.Embed(
title=f"{team1['name']} vs {team2['name']}",
timestamp=datetime.strptime(ongoing["createdAt"], "%Y-%m-%dT%H:%M:%S%z"),
)
embed.set_author(name=f"Ongoing {ongoing['entity']['name']}")
embed.set_footer(text="Started:")
embed.add_field(
name=f"{team1['name']} Roster",
value="\n".join([player["nickname"] for player in team1["roster"]]),
)
embed.add_field(
name=f"{team2['name']} Roster",
value="\n".join([player["nickname"] for player in team2["roster"]]),
)
await ctx.send(embed=embed)
async def is_ongoing(self, ctx, name, messages=True):
stats = await self.get_ongoing(name)
if stats.get("error"):
if messages:
await ctx.send(stats.get("error"))
return False
if stats.get("errors"):
if messages:
await ctx.send(stats.get("errors")[0]["message"])
return False
ongoing = stats["payload"].get("ONGOING")
if ongoing is None:
if messages:
await ctx.send("No ongoing game available.")
return False
return ongoing