-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
205 lines (137 loc) · 5.73 KB
/
bot.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
import os
from StatsDB import StatsDB
from IScraper import IScraper
import discord
import render
from ids import series_short_hands
scraper = IScraper()
client = discord.Client()
db = StatsDB('stats.db')
def resolve_shorthand(text):
if not text.isnumeric():
if text not in series_short_hands.keys():
return False, f'Series shorthand {text} not recognized. You can add it with "_shorthand {text} <series_id>".'
return True, series_short_hands[text]
return True, text
def register_driver(message):
parts = message.content.split(maxsplit=2)
if len(parts) != 3:
return False, 'Invalid command, try "_register me Max Mustermann"'
_, discord_id, iracing_name = parts
if discord_id == 'me':
discord_id = message.author.id
db.add_driver(iracing_name, discord_id)
return f'Registered driver {iracing_name}.'
def lastrace(message):
result = db.lookup_driver(message.author.id)
if len(result) != 1:
return 'You are currently not registered as a driver, by typing "_register me <Your iRacing display name>".'
driver = result[0]
race_info = scraper.get_driver_recent_races(driver)
subsession_info = scraper.get_subsession_details(race_info[0]['subsession_id'])
image = render.last_race(race_info, subsession_info)
return 'Done.'
def strength_of_field(message):
parts = message.content.split(maxsplit=1)
if len(parts) != 2:
return False, 'Invalid command, try "_sof lmp2"'
season_id = parts[1]
if not season_id.isnumeric():
if season_id not in series_short_hands.keys():
return False, f'Series shorthand {season_id} not recognized. You can add it with "_shorthand {season_id} <series_id>".'
season_id = series_short_hands[season_id]
season_details = scraper.get_season_details(season_id)
return True, render.participation(season_details)
def participation_or_sof(message, p_or_s):
parts = message.content.split(maxsplit=1)
if len(parts) != 2:
if p_or_s == 'p':
return False, 'Invalid command, try "_participation lmp2"'
else:
return False, 'Invalid command, try "_sof lmp2"'
success, response = resolve_shorthand(parts[1])
if not success:
return False, response
season_id = response
season_details = scraper.get_season_details(season_id)
# get car info for series
# get series info to lookup name
meta_info = scraper.get_series_meta()
series_name = [x['season_name'] for x in meta_info if x['season_id'] == season_id][0]
return True, render.participation_or_sof(season_details, p_or_s, series_name)
def schedule(message):
parts = message.content.split(maxsplit=1)
if len(parts) != 2:
return False, 'Invalid command, try "_schedule lmp2"'
success, response = resolve_shorthand(parts[1])
if not success:
return False, response
season_id = response
# get series info
meta_info = scraper.get_series_meta()
# get schedule for series
schedules = [x['schedules'] for x in meta_info if x['season_id'] == season_id][0]
return render.schedule(schedules)
def bop(message):
parts = message.content.split(maxsplit=1)
if len(parts) != 2:
return False, 'Invalid command, try "_bop lmp2"'
season_id = parts[1]
success, response = resolve_shorthand(parts[1])
if not success:
return False, response
season_id = response
season_details = scraper.get_season_details(season_id)
return ''
def series(message):
parts = message.content.split()
if len(parts) != 2 or parts[1] not in ('oval', 'road', 'dirt_oval', 'dirt_road'):
return "No valid category specified, must be one of: 'oval', 'road', 'dirt_oval', 'dirt_road'"
meta_info = scraper.get_series_meta()
series = [x['season_name'] for x in meta_info if x['track_types'][0]['track_type'] == parts[1]]
ret = f'Series for type "{parts[1]}":\n'
for s in sorted(series):
ret += f' - {s}\n'
return ret
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '_hi':
response = 'Hi, I am the iRacing stats bot'
await message.channel.send(response)
if message.content.startswith('_series'):
await message.channel.send(series(message))
if message.content.startswith('_register'):
await message.channel.send(register_driver(message))
if message.content == '_lastrace':
await message.channel.send(lastrace(message))
if message.content.startswith('_participation'):
success, response = participation_or_sof(message, 'p')
if success:
await message.channel.send(file=discord.File(response))
else:
await message.channel.send(response)
if message.content.startswith('_sof'):
success, response = participation_or_sof(message, 's')
if success:
await message.channel.send(file=discord.File(response))
else:
await message.channel.send(response)
if message.content.startswith('_bop'):
await message.channel.send(bop(message))
if message.content.startswith('_schedule'):
await message.channel.send(schedule(message))
if message.content == '_help':
answer = """
Register drivers with '_register me <iracing name>' or '_register <discord id> <iracing name>.
Get Series participation with '_participation <series name>'.
Get info on your latest race with '_lastrace'
Currently tracked series:
- lmp2
"""
await message.channel.send(answer)
client.run(os.environ['TOKEN'])