-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
223 lines (194 loc) · 8.37 KB
/
Copy pathutils.py
File metadata and controls
223 lines (194 loc) · 8.37 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
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
from typing import List, Optional
import aiohttp
import discord
from discord.app_commands import Choice
allowed_tables = ["Sh150", "Ni150", "Sh200", "Ni200"]
speedChoices = [Choice(name="150cc", value="150"), Choice(name="200cc", value="200")]
itemChoices = [Choice(name="Shrooms", value="sh"), Choice(name="No items", value="ni")]
gameChoices = [
Choice(name="Mario Kart 8 Deluxe", value="mk8dx"),
Choice(name="Mario Kart World", value="mkworld"),
]
warGameChoices = [
Choice(name="Mario Kart 8 Deluxe", value="mk8dx"),
Choice(name="Mario Kart World", value="mkworld"),
Choice(name="Mario Kart Wii", value="mkwii"),
]
loungeGameChoices = [
Choice(name="Mario Kart 8 Deluxe", value="mk8dx"),
Choice(name="Mario Kart World (24p)", value="mkworld24p"),
Choice(name="Mario Kart World (12p)", value="mkworld12p"),
]
statChoices = [
Choice(name="mmr", value="mmr"),
Choice(name="peak", value="maxMmr"),
Choice(name="events", value="eventsPlayed"),
]
class LoungeData:
def __init__(self):
self.base_url = "https://lounge.mkcentral.com/api"
async def search_players(
self, search: str = "", game: str = "mkworld", season: int = 0, limit: int = 50
) -> Optional[List[dict]]:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/player/leaderboard?game={game}&season={season}&search={search}&limit={limit}"
) as response:
if response.status == 200:
data = await response.json()
return data.get("data", [])
return None
async def find_player_by_discord_id(
self, discord_id: int, game: str = "mkworld"
) -> Optional[dict]:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/player/details?discordId={discord_id}&game={game}"
) as response:
if response.status == 200:
return await response.json()
return None
async def find_player_by_name(
self, name: str, game: str = "mkworld"
) -> Optional[dict]:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/player/details?name={name}&game={game}"
) as response:
if response.status == 200:
return await response.json()
return None
class MkcData:
def __init__(self):
self.base_url = "https://mkcentral.com/api"
async def search_teams(
self, search: str = "", limit: int = 25
) -> Optional[List[dict]]:
"""Search for teams and return their rosters"""
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/registry/teams?is_historical=false&is_active=true&sort_by_newest=false&min_player_count=5&name_or_tag={search}"
) as response:
if response.status == 200:
data = await response.json()
teams = data.get("teams", [])
rosters = []
for team in teams:
team_rosters = team.get("rosters", [])
for roster in team_rosters:
if roster["game"] in ("mkworld", "mk8dx", "mkw"):
rosters.append(roster)
return rosters[:limit] if limit else rosters
return None
async def get_team_details(self, team_id: int, roster_id: int) -> Optional[dict]:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/registry/teams/{team_id}"
) as response:
if response.status == 200:
data = await response.json()
rosters = data.get("rosters", [])
for roster in rosters:
if int(roster.get("id")) == int(roster_id):
roster["logo"] = data.get("logo")
return roster
return None
class LoungeSeason:
def __init__(self):
self._data = {}
self.base_url = "https://lounge.mkcentral.com/api"
async def lounge_season(self, game: str = "mkworld"):
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/player/details?id=14324&game={game}"
) as response:
if response.status == 200:
_data_full = await response.json()
self._data[game] = _data_full.get("season", 0)
def data(self, game: str = "mkworld"):
return self._data.get(game)
class Paginator(discord.ui.View):
def __init__(self, interaction: discord.Interaction, embeds: List[discord.Embed]):
super().__init__(timeout=600)
self.interaction = interaction
self.embeds = embeds
self.page_num = 1
self.index.label = f"1/{len(embeds)}"
self.first.disabled = True
self.before.disabled = True
if len(embeds) == 1:
self.after.disabled = True
self.last.disabled = True
@discord.ui.button(label="<<", style=discord.ButtonStyle.blurple)
async def first(self, interaction: discord.Interaction, button: discord.ui.Button):
self.page_num = 1
self.index.label = f"{self.page_num}/{len(self.embeds)}"
self.first.disabled = True
self.before.disabled = True
self.after.disabled = False
self.last.disabled = False
await interaction.response.edit_message(
embed=self.embeds[self.page_num - 1], view=self
)
@discord.ui.button(label="<", style=discord.ButtonStyle.red)
async def before(self, interaction: discord.Interaction, button: discord.ui.Button):
self.page_num -= 1
self.index.label = f"{self.page_num}/{len(self.embeds)}"
self.after.disabled = False
self.last.disabled = False
if self.page_num == 1:
self.first.disabled = True
self.before.disabled = True
await interaction.response.edit_message(
embed=self.embeds[self.page_num - 1], view=self
)
@discord.ui.button(style=discord.ButtonStyle.gray, disabled=True)
async def index(self, interaction: discord.Interaction, button: discord.ui.Button):
pass
@discord.ui.button(label=">", style=discord.ButtonStyle.green)
async def after(self, interaction: discord.Interaction, button: discord.ui.Button):
self.page_num += 1
self.index.label = f"{self.page_num}/{len(self.embeds)}"
self.first.disabled = False
self.before.disabled = False
if self.page_num == len(self.embeds):
self.after.disabled = True
self.last.disabled = True
await interaction.response.edit_message(
embed=self.embeds[self.page_num - 1], view=self
)
@discord.ui.button(label=">>", style=discord.ButtonStyle.blurple)
async def last(self, interaction: discord.Interaction, button: discord.ui.Button):
self.page_num = len(self.embeds)
self.index.label = f"{self.page_num}/{len(self.embeds)}"
self.first.disabled = False
self.before.disabled = False
self.after.disabled = True
self.last.disabled = True
await interaction.response.edit_message(
embed=self.embeds[self.page_num - 1], view=self
)
async def on_timeout(self):
self.first.disabled = True
self.before.disabled = True
self.after.disabled = True
self.last.disabled = True
if not self.interaction.is_expired():
await self.interaction.edit_original_response(view=self)
class ConfirmButton(discord.ui.View):
def __init__(self):
super().__init__()
self.answer = None
@discord.ui.button(label="Confirm", style=discord.ButtonStyle.green)
async def confirm(
self, interaction: discord.Interaction, button: discord.ui.Button
):
self.answer = True
self.stop()
@discord.ui.button(label="Cancel", style=discord.ButtonStyle.red)
async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button):
self.answer = False
self.stop()
lounge_season = LoungeSeason()
lounge_data = LoungeData()
mkc_data = MkcData()