-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
407 lines (357 loc) · 12.8 KB
/
main.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import discord
from discord_slash import SlashCommand
import os
from discord.ext import commands
import requests
from bs4 import BeautifulSoup
client = commands.Bot(command_prefix=[' '])
client.remove_command('help')
slash = SlashCommand(client, sync_commands=True) # Declares slash commands through the client.
token = os.environ.get('token')
guild_ids = [945418074121715782, 814607316107395093, 704482683857928222,961863482867847188, 962033686046466070]
roboteventskey = os.environ.get('robotevents_key')
def get_current_season():
iq_seasons = 'https://www.robotevents.com/api/v2/seasons?program%5B%5D=41'
headers = {"Authorization": f"Bearer {roboteventskey}"}
seasondata = requests.get(iq_seasons, headers=headers).json()
currentseasonid = seasondata['data'][1]['id']
currentseasonname = seasondata['data'][1]['name']
return currentseasonid, currentseasonname
currentseasonid, currentseasonname = get_current_season()
async def get_ranking(team):
url = f'https://www.robotevents.com/teams/VIQC/{team.upper()}'
res = requests.get(url)
html_page = res.content
soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
output = ''
blacklist = [
'[document]',
'noscript',
'header',
'html',
'meta',
'head',
'input',
'script',
]
for t in text:
if t.parent.name not in blacklist:
output += '{} '.format(t)
index = output.find("World Skills Rank:")
rank = ''
for x in range(0, 4):
if output[index+21+x].isnumeric():
rank+=output[index+21+x]
return rank
@client.event
async def on_ready():
print("Ready!")
# channel = client.get_guild(945418074121715782).get_channel(992529252652158977)
# e = await channel.create_webhook(name='counting').url
# print(e)
game = discord.Game("a skills run")
await client.change_presence(activity=game)
# Put your server ID in this array.
guild_ids = [945418074121715782, 814607316107395093, 704482683857928222,961863482867847188, 962033686046466070]
# This command is used to get the ranking of a VIQC team.
@slash.slash(name='skills', description='World Skills Rank')
async def skills(ctx, team):
team = team.upper()
await ctx.defer()
rank = await get_ranking(team)
teaminfo = f'https://www.robotevents.com/api/v2/teams?number={team}'
headers = {"Authorization": f"Bearer {roboteventskey}"}
teamdata = requests.get(teaminfo, headers=headers).json()
try:
for allteam in teamdata['data']:
if allteam['program']['code'] == 'VIQC':
id = allteam['id']
teamname = allteam['team_name']
teaminfo = f'https://www.robotevents.com/api/v2/teams/{id}/skills'
except:
await ctx.send(f"Sorry, we couldn't find team {team}.")
return
driverlist = []
autonlist = []
events = {}
def eventdata(teaminfo, driverlist, autonlist, events):
teamdata = requests.get(teaminfo, headers=headers).json()
for event in teamdata['data']:
if event['season']['id'] == currentseasonid:
if event['event']['name'] not in events:
events[event['event']['name']] = {}
events[event['event']['name']]['total'] = 0
events[event['event']['name']]['driver'] = 0
events[event['event']['name']]['auton'] = 0
if event['type'] == 'driver':
driverlist.append(event['score'])
events[event['event']['name']]['total'] += event['score']
events[event['event']['name']]['driver'] = event['score']
elif event['type'] == 'programming':
autonlist.append(event['score'])
events[event['event']['name']]['total'] += event['score']
events[event['event']['name']]['auton'] = event['score']
if isinstance(teamdata['meta']['next_page_url'], str):
teaminfo=teamdata['meta']['next_page_url']
eventdata(teaminfo, driverlist, autonlist, events)
return driverlist, autonlist, events
eventdata(teaminfo, driverlist, autonlist, events)
if driverlist:
driverHigh = max(driverlist)
else:
driverHigh=0
if autonlist:
autonomousHigh = max(autonlist)
else:
autonomousHigh=0
score = 0
for total in events:
if events[total]['total'] > score:
score = events[total]['total']
event = total
try:
autonomous = events[event]['auton']
except:
autonomous = 0
try:
driver = events[event]['driver']
except:
driver = 0
embed=discord.Embed(title="Current VIQC Event", url="https://www.vexrobotics.com/iq/competition/viqc-current-game", color=0x24c9ff)
embed.set_author(name=f"VIQC {team} ({teamname})", url=f"https://www.robotevents.com/teams/VIQC/{team}")
embed.add_field(name="Rank", value=rank, inline=True)
embed.add_field(name="Score", value=score, inline=True)
embed.add_field(name="Programming", value=autonomous, inline=True)
embed.add_field(name="Driver", value=driver, inline=True)
embed.add_field(name="Highest Programming", value=autonomousHigh, inline=True)
embed.add_field(name="Highest Driver", value=driverHigh, inline=True)
print(embed)
try:
await ctx.send(embed=embed)
except:
await ctx.send(f"Hmm, we couldn't fetch your skills rank. Check https://www.robotevents.com/robot-competitions/vex-iq-challenge/standings/skills to see if {team} ({teamname}) is on there.")
@slash.slash(name='about', description='Bot credits')
async def credits(ctx):
await ctx.send('''Made by VIQC 84C in 2022:
<@!697535361315766322>
<@!697913907528073296>
<@!475315771086602241>
Special thanks to makers of <@!329389228398215179>
If you would like to add the slash commands to your server, click on my profile and press "Add to Server"!''',
allowed_mentions=discord.AllowedMentions(
users=False, # Whether to ping individual user @mentions
everyone=False, # Whether to ping @everyone or @here mentions
roles=False, # Whether to ping role @mentions
replied_user=False, # Whether to ping on replies to messages
), hidden = True)
# This command is used to get the awards of a VIQC team.
@slash.slash(name='awards', description='Specified Team\'s awards')
async def awards(ctx, team):
team = team.upper()
await ctx.defer()
teaminfo = f'https://www.robotevents.com/api/v2/teams?number={team}'
headers = {"Authorization": f"Bearer {roboteventskey}"}
teamdata = requests.get(teaminfo, headers=headers).json()
try:
for allteam in teamdata['data']:
if allteam['program']['code'] == 'VIQC':
id = allteam['id']
teamname = allteam['team_name']
awardsUrl = f'https://www.robotevents.com/api/v2/teams/{id}/awards?season={currentseasonid}'
except:
await ctx.send(f"Sorry, we couldn't find team {team}.")
return
headers = {"Authorization": f"Bearer {roboteventskey}"}
# Award Data Loop for Next Page
awardsDict = {}
def awardData(awardsUrl, awardsDict):
teamAwards = requests.get(awardsUrl, headers=headers).json()
print(awardsUrl)
for event in teamAwards['data']:
if event['event']['name'] not in awardsDict:
awardsDict[event['event']['name']] = []
awardsDict[event['event']['name']].append(event['title'])
if isinstance(teamAwards['meta']['next_page_url'], str):
print('sus')
awardsUrl=teamAwards['meta']['next_page_url']
awardData(awardsUrl, awardsDict)
return awardsDict
awardData(awardsUrl, awardsDict)
orderedAwards = ''
for event in awardsDict:
orderedAwards+=f'\n\n**{event}**\n'
for award in awardsDict[event]:
orderedAwards+=f"{award}\n"
if not orderedAwards:
orderedAwards = "\n\nCouldn't find any awards this season!"
await ctx.send(f"**{team.upper()} ({teamname})'s Awards**{orderedAwards}")
@slash.slash(name='compare', description="Compare two teams by skills ranking")
async def compare(ctx, team1, team2):
team1 = team1.upper()
team2 = team2.upper()
await ctx.defer()
rank1 = await get_ranking(team1)
rank2 = await get_ranking(team2)
team1info = f'https://www.robotevents.com/api/v2/teams?number={team1}'
team2info = f'https://www.robotevents.com/api/v2/teams?number={team2}'
headers = {"Authorization": f"Bearer {roboteventskey}"}
team1data = requests.get(team1info, headers=headers).json()
team2data = requests.get(team2info, headers=headers).json()
try:
for allteam in team1data['data']:
if allteam['program']['code'] == 'VIQC':
id1 = allteam['id']
team1name = allteam['team_name']
for allteam2 in team2data['data']:
if allteam['program']['code'] == 'VIQC':
id2 = allteam2['id']
team2name = allteam2['team_name']
except:
await ctx.send("Sorry, we couldn't find a team specified.")
return
team1info = f'https://www.robotevents.com/api/v2/teams/{id1}/skills'
driverlist1 = []
autonlist1 = []
events1 = {}
team2info = f'https://www.robotevents.com/api/v2/teams/{id2}/skills'
driverlist2 = []
autonlist2 = []
events2 = {}
def eventdata(teaminfo, driverlist, autonlist, events):
teamdata = requests.get(teaminfo, headers=headers).json()
for event in teamdata['data']:
if event['season']['id'] == currentseasonid:
if event['event']['name'] not in events:
events[event['event']['name']] = {}
events[event['event']['name']]['total'] = 0
events[event['event']['name']]['driver'] = 0
events[event['event']['name']]['auton'] = 0
if event['type'] == 'driver':
driverlist.append(event['score'])
events[event['event']['name']]['total'] += event['score']
events[event['event']['name']]['driver'] = event['score']
elif event['type'] == 'programming':
autonlist.append(event['score'])
events[event['event']['name']]['total'] += event['score']
events[event['event']['name']]['auton'] = event['score']
if isinstance(teamdata['meta']['next_page_url'], str):
teaminfo=teamdata['meta']['next_page_url']
eventdata(teaminfo, driverlist, autonlist, events)
return driverlist, autonlist, events
eventdata(team1info, driverlist1, autonlist1, events1)
eventdata(team2info, driverlist2, autonlist2, events2)
if driverlist1:
driverHigh1 = max(driverlist1)
else:
driverHigh1=0
if autonlist1:
autonomousHigh1 = max(autonlist1)
else:
autonomousHigh1=0
score1 = 0
for total in events1:
if events1[total]['total'] > score1:
score1 = events1[total]['total']
event = total
try:
autonomous1 = events1[event]['auton']
except:
autonomous1 = 0
try:
driver1 = events1[event]['driver']
except:
driver1 = 0
if driverlist2:
driverHigh2 = max(driverlist2)
else:
driverHigh2=0
if autonlist2:
autonomousHigh2 = max(autonlist2)
else:
autonomousHigh2=0
score2 = 0
for total in events2:
if events2[total]['total'] > score2:
score2 = events2[total]['total']
event = total
try:
autonomous2 = events2[event]['auton']
except:
autonomous2 = 0
try:
driver2 = events2[event]['driver']
except:
driver2 = 0
rank1 = int(rank1)
rank2 = int(rank2)
if rank1 < rank2:
rankdif = rank2-rank1
r1 = f"{rankdif} ⬆️🟩"
r2 = f"{rankdif} ⬇️🟥"
else:
rankdif = rank1-rank2
r2 = f"{rankdif} ⬆️🟩"
r1 = f"{rankdif} ⬇️🟥"
if driver1 > driver2:
driverdif = driver1-driver2
d1 = f"{driverdif} ⬆️🟩"
d2 = f"{driverdif} ⬇️🟥"
elif driver2 > driver1:
driverdif = driver2-driver1
d2 = f"{driverdif} ⬆️🟩"
d1 = f"{driverdif} ⬇️🟥"
else:
d1 = "↔️⏺️"
d2 = "↔️⏺️"
if autonomous1 > autonomous2:
autonomousdif = autonomous1-autonomous2
a1 = f"{autonomousdif} ⬆️🟩"
a2 = f"{autonomousdif} ⬇️🟥"
elif autonomous2 > autonomous1:
autonomousdif = autonomous2-autonomous1
a2 = f"{autonomousdif} ⬆️🟩"
a1 = f"{autonomousdif} ⬇️🟥"
else:
a1 = "↔️⏺️"
a2 = "↔️⏺️"
if score1 > score2:
scoredif = score1-score2
s1 = f"{scoredif} ⬆️🟩"
s2 = f"{scoredif} ⬇️🟥"
elif score2 > score1:
scoredif = score2-score1
s2 = f"{scoredif} ⬆️🟩"
s1 = f"{scoredif} ⬇️🟥"
else:
s1 = "↔️⏺️"
s2 = "↔️⏺️"
if autonomousHigh1 > autonomousHigh2:
ahdif = autonomousHigh1-autonomousHigh2
ah1 = f"{ahdif} ⬆️🟩"
ah2 = f"{ahdif} ⬇️🟥"
elif autonomousHigh2 > autonomousHigh1:
ahdif = autonomousHigh2-autonomousHigh1
ah2 = f"{ahdif} ⬆️🟩"
ah1 = f"{ahdif} ⬇️🟥"
else:
ah1 = "↔️⏺️"
ah2 = "↔️⏺️"
if driverHigh1 > driverHigh2:
addif = driverHigh1-driverHigh2
ad1 = f"{addif} ⬆️🟩"
ad2 = f"{addif} ⬇️🟥"
elif driverHigh2 > driverHigh1:
addif = driverHigh2-driverHigh1
ad2 = f"{addif} ⬆️🟩"
ad1 = f"{addif} ⬇️🟥"
else:
ad1 = "↔️⏺️"
ad2 = "↔️⏺️"
embed=discord.Embed(title="Current VIQC Event", url="https://www.vexrobotics.com/iq/competition/viqc-current-game", color=0x24c9ff)
embed.set_author(name=f"Comparing VIQC {team1} vs. {team2}")
embed.add_field(name=f"{team1} ({team1name}):", value=f"Rank: {rank1} ({r1})\nScore: {score1} ({s1})\nProgramming: {autonomous1} ({a1})\nDriver: {driver1} ({d1})\nHighest Programming: {autonomousHigh1} ({ah1})\nHighest Driver: {driverHigh1} ({ad1})")
embed.add_field(name=f"{team2} ({team2name}):", value=f"Rank: {rank2} ({r2})\nScore: {score2} ({s2})\nProgramming: {autonomous2} ({a2})\nDriver: {driver2} ({d2})\nHighest Programming: {autonomousHigh2} ({ah2})\nHighest Driver: {driverHigh2} ({ad2})")
await ctx.send(embed=embed)
# keep_alive.keep_alive()
client.run(token)