This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
discord_logs_uploader.py
executable file
·468 lines (392 loc) · 15.9 KB
/
discord_logs_uploader.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env python3
import os
import re
import sys
import shutil
import logging
import datetime
import tempfile
from typing import IO, cast, List, Tuple, BinaryIO, Optional
from pathlib import Path
from zipfile import ZipFile, BadZipFile, is_zipfile, ZIP_DEFLATED
import aiohttp
import discord
from dotenv import load_dotenv
from discord.ext import commands
# name of the role able to execute the command
ADMIN_ROLE = 'Blue Shirt'
# prefix of team channels
TEAM_PREFIX = 'team-'
# a channel to upload files that are available to all teams
COMMON_CHANNEL = 'general'
# the command options for animation file handling
ANIMATION_OPTIONS = {
'none': None,
'team': True,
'separate': False,
}
logger = logging.getLogger('logs_uploader')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
logger.addHandler(handler)
# Don't post to team channels and force the guild used so testing can you DMs
DISCORD_TESTING = bool(os.getenv('DISCORD_TESTING'))
# Just post all messages to calling channel, allow DMs
DISCORD_DEBUG = bool(os.getenv('DISCORD_DEBUG'))
if DISCORD_TESTING or DISCORD_DEBUG:
# Allow DMs in testing
guild_only = commands.check_any(commands.guild_only(), commands.dm_only()) # type: ignore
# print all debug messages
logger.setLevel(logging.DEBUG)
handler.setLevel(logging.DEBUG)
else:
guild_only = commands.guild_only()
bot = commands.Bot(command_prefix='!')
async def log_and_reply(ctx: commands.Context, error_str: str) -> None:
logger.error(error_str)
await ctx.reply(error_str)
async def get_channel(
ctx: commands.Context,
channel_name: str,
) -> Optional[discord.TextChannel]:
channel_name = channel_name.lower() # all text/voice channels are lowercase
guild = ctx.guild
if DISCORD_DEBUG:
# Always return calling channel
return cast(discord.TextChannel, ctx.channel)
if DISCORD_TESTING:
guild_id = os.getenv('DISCORD_GUILD')
if guild_id is None:
guild = None
else:
guild = bot.get_guild(int(guild_id))
# get team's channel by name
if guild is None:
raise commands.NoPrivateMessage
channel = discord.utils.get(
guild.channels,
name=channel_name,
)
if not channel:
await log_and_reply(
ctx,
f"# Channel {channel_name} not found, unable to send message",
)
return None
elif not isinstance(channel, discord.TextChannel):
await log_and_reply(
ctx,
f"# {channel.name} is not a text channel, unable to send message",
)
return None
return channel
async def get_team_channel(
ctx: commands.Context,
archive_name: str,
zip_name: str,
) -> Tuple[str, Optional[discord.TextChannel]]:
# extract team name from filename
tla_search = re.match(TEAM_PREFIX + r'(.*?)[-.]', archive_name)
if not isinstance(tla_search, re.Match):
await log_and_reply(
ctx,
f"# Failed to extract a TLA from {archive_name} in {zip_name}",
)
return '', None
tla = tla_search.group(1)
channel = await get_channel(ctx, f"{TEAM_PREFIX}{tla}")
return tla, channel
def pre_test_zipfile(archive_name: str, zip_name: str) -> bool:
if not archive_name.lower().endswith('.zip'): # skip non-zips
logger.debug(f"{archive_name} from {zip_name} is not a ZIP, skipping")
return False
# skip files not starting with TEAM_PREFIX
if not archive_name.lower().startswith(TEAM_PREFIX):
logger.debug(
f"{archive_name} from {zip_name} "
f"doesn't start with {TEAM_PREFIX}, skipping",
)
return False
return True
def match_animation_files(log_name: str, animation_dir: Path) -> List[Path]:
match_num_search = re.search(r'match-([0-9]+)', log_name)
if not isinstance(match_num_search, re.Match):
logger.warning(f'Invalid match name: {log_name}')
return []
match_num = match_num_search[1]
logger.debug(f"Fetching animation files for match {match_num}")
match_files = animation_dir.glob(f'match-{match_num}.*')
return [data_file for data_file in match_files if data_file.suffix != '.mp4']
def insert_match_files(archive: Path, animation_dir: Path) -> None:
# append animations to archive
with ZipFile(archive, 'a', compression=ZIP_DEFLATED) as zipfile:
for log_name in zipfile.namelist():
if not log_name.endswith('.txt'):
continue
for animation_file in match_animation_files(log_name, animation_dir):
zipfile.write(animation_file.resolve(), animation_file.name)
# add textures sub-tree
for texture in (animation_dir / 'textures').glob('**/*'):
zipfile.write(
texture.resolve(),
texture.relative_to(animation_dir),
)
async def send_file(
ctx: commands.Context,
channel: discord.TextChannel,
archive: Path,
event_name: str,
msg_str: str = "Here are your logs",
logging_str: str = "Uploaded logs",
) -> bool:
try:
if DISCORD_TESTING: # don't actually send message in testing
if (archive.stat().st_size / 1000**2) > 8:
# discord.HTTPException requires aiohttp.ClientResponse
await log_and_reply(
ctx,
f"# {archive.name} was too large to upload at "
f"{archive.stat().st_size / 1000**2 :.3f} MiB",
)
return False
else:
await channel.send(
content=f"{msg_str} from {event_name if event_name else 'today'}",
file=discord.File(str(archive)),
)
logger.debug(
f"{logging_str} from {event_name if event_name else 'today'}",
)
except discord.HTTPException as e: # handle file size issues
if e.status == 413:
await log_and_reply(
ctx,
f"# {archive.name} was too large to upload at "
f"{archive.stat().st_size / 1000**2 :.3f} MiB",
)
return False
else:
raise e
return True
def extract_animations(zipfile: ZipFile, tmpdir: Path, fully_extract: bool) -> bool:
animation_files = [
name for name in zipfile.namelist()
if name.split('/')[-1].startswith('animations')
and name.endswith('.zip')
]
if not animation_files:
return False
try:
zipfile.extract(animation_files[0], path=tmpdir)
except BadZipFile:
logger.warning("The animations zip was corrupt")
return False
# give the animations archive + folder if fixed name
shutil.move(str(tmpdir / animation_files[0]), str(tmpdir / 'animations.zip'))
if fully_extract:
with ZipFile(tmpdir / 'animations.zip') as animation_zip:
(tmpdir / 'animations').mkdir()
animation_zip.extractall(tmpdir / 'animations')
logger.debug("Extracting animations.zip")
return True
async def logs_upload(
ctx: commands.Context,
file: IO[bytes],
zip_name: str,
event_name: str,
team_animation: Optional[bool] = None, # None = don't upload animations
) -> None:
animations_found = False
try:
with tempfile.TemporaryDirectory() as tmpdir_name:
tmpdir = Path(tmpdir_name)
completed_tlas = []
with ZipFile(file) as zipfile:
if team_animation is not None:
animations_found = extract_animations(zipfile, tmpdir, team_animation)
if not animations_found:
await log_and_reply(ctx, "animations Zip file is missing")
for archive_name in zipfile.namelist():
if not pre_test_zipfile(archive_name, zip_name):
continue
zipfile.extract(archive_name, path=tmpdir)
if not is_zipfile(tmpdir / archive_name): # test file is a valid zip
await log_and_reply(
ctx,
f"# {archive_name} from {zip_name} is not a valid ZIP file",
)
# The file will be removed with the temporary directory
continue
if team_animation and animations_found:
insert_match_files(tmpdir / archive_name, tmpdir / 'animations')
# get team's channel
tla, channel = await get_team_channel(ctx, archive_name, zip_name)
if not channel:
continue
# upload to team channel with message
if not await send_file(
ctx,
channel,
tmpdir / archive_name,
event_name,
logging_str=f"Uploaded logs for {tla}",
):
# try again without animations
# TODO test this clause in unit testing
if team_animation:
# extract original archive, modified version is overwritten
zipfile.extract(archive_name, path=tmpdir)
if await send_file( # retry with original archive
ctx,
channel,
tmpdir / archive_name,
event_name,
logging_str=f"Uploaded only logs for {tla}",
):
await log_and_reply(
ctx,
f"Only able to upload logs for {tla}, "
"no animations were served",
)
continue
completed_tlas.append(tla)
if team_animation is False and animations_found:
common_channel = await get_channel(ctx, COMMON_CHANNEL)
# upload animations.zip to common channel
if common_channel:
await send_file(
ctx,
common_channel,
tmpdir / 'animations.zip',
event_name,
msg_str="Here are the animation files",
logging_str="Uploaded animations",
)
await ctx.reply(
f"Successfully uploaded logs to {len(completed_tlas)} teams: "
f"{', '.join(completed_tlas)}",
)
except BadZipFile:
await log_and_reply(ctx, f"# {zip_name} is not a valid ZIP file")
@bot.event
async def on_ready() -> None:
logger.info(f"{bot.user} has connected to Discord!")
if DISCORD_TESTING:
logger.info("Bot is running in test mode")
if DISCORD_DEBUG:
logger.info("Bot is running in debug mode")
@bot.command(name='logs')
@guild_only
@commands.check_any(commands.has_role(ADMIN_ROLE), commands.is_owner()) # type: ignore
async def _logs_import(
ctx: commands.Context,
animations: str = 'none',
event_name: str = "",
) -> None:
"""
Send a combined logs archive to the bot for distribution to teams
- animations: How the animation files are handled
- none: Ignore the animations file
- team: Insert teams' matches into their archives
- separate: Put the animations archive in the common channel
- event_name: Optionally set the event name used in the bot's message to teams
"""
logger.info(f"{ctx.author} ran '{ctx.message.content}' on {ctx.guild}:{ctx.channel}")
if animations not in ANIMATION_OPTIONS.keys():
await ctx.send(
f"The animations parameter can only be: {', '.join(ANIMATION_OPTIONS.keys())}",
)
await ctx.send_help(_logs_import)
return
for file in ctx.message.attachments:
logger.debug(
f"Files received {file.filename}: "
f"{file.size/1024**2 :.3f}MB, {file.size/1000**2:.3f}MiB",
)
if (
ctx.message.attachments
and ctx.message.attachments[0].filename.lower().endswith('.zip')
):
with tempfile.TemporaryFile(suffix='.zip') as zipfile:
attachment = ctx.message.attachments[0]
filename = attachment.filename
with ctx.typing(): # provides feedback that the bot is processing
await attachment.save(cast(BinaryIO, zipfile), seek_begin=True)
await logs_upload(
ctx,
zipfile,
filename,
event_name,
ANIMATION_OPTIONS[animations],
)
else:
logger.error(
f"ZIP file not attached to '{ctx.message.content}' from {ctx.author}",
)
await ctx.send("This command requires the logs archive to be attached")
await ctx.send_help(_logs_import) # print corresponding command help
@bot.command(name='logs_url')
@guild_only
@commands.check_any(commands.has_role(ADMIN_ROLE), commands.is_owner()) # type: ignore
async def _logs_download(
ctx: commands.Context,
logs_url: str,
animations: str = 'none',
event_name: str = "",
) -> None:
"""
Get combined logs archive from URL for distribution to teams, avoids Discord's size limit
- logs_url: a download link for the combined logs archive to be distributed to teams
- animations: How the animation files are handled
- none: Ignore the animations file
- team: Insert teams' matches into their archives
- separate: Put the animations archive in the common channel
- event_name: Optionally set the event name used in the bot's message to teams
"""
logger.info(f"{ctx.author} ran '{ctx.message.content}' on {ctx.guild}:{ctx.channel}")
if animations not in ANIMATION_OPTIONS.keys():
await ctx.send(
f"The animations parameter can only be: {', '.join(ANIMATION_OPTIONS.keys())}",
)
await ctx.send_help(_logs_download)
return
with tempfile.TemporaryFile(suffix='.zip') as zipfile:
if logs_url.endswith('.zip'):
filename = logs_url.split("/")[-1]
else:
filename = f"logs_upload-{datetime.date.today()}.zip"
with ctx.typing(): # provides feedback that the bot is processing
# download zip, using aiohttp
async with aiohttp.ClientSession() as session:
resp = await session.get(logs_url)
if resp.status >= 400:
logger.error(
f"Download from {logs_url} failed with error "
f"{resp.status}, {resp.reason}",
)
await ctx.reply("Zip file failed to download")
return
zipfile_data = await resp.read()
zipfile.write(zipfile_data)
# start processing from beginning of the file
zipfile.seek(0)
await logs_upload(
ctx,
zipfile,
filename,
event_name,
ANIMATION_OPTIONS[animations],
)
@bot.event
async def on_command_error(ctx: commands.Context, exception: commands.CommandError) -> None:
if isinstance(exception, commands.MissingRequiredArgument):
logger.info(f"{ctx.author} ran '{ctx.message.content}' on {ctx.guild}:{ctx.channel}")
logger.error(f"A required argument '{exception.param}' is missing")
await ctx.send(f"A required argument '{exception.param}' is missing")
await ctx.send_help(ctx.command) # print corresponding command help
else:
raise exception
if __name__ == "__main__":
load_dotenv()
bot.run(os.getenv('DISCORD_TOKEN', ''))