-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOGClient.py
48 lines (39 loc) · 1.56 KB
/
BOGClient.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
import discord
import os
import hashlib
from discord import Message
from discord.errors import Forbidden, HTTPException, NotFound
from spawningtool.exception import ReadError, ReplayFormatError
import spawningtool.parser
import utils
import constants as const
import io
class BOGClient(discord.Client):
""" Represents the Discord Bot Client """
async def on_ready(self):
print("BOG is now running.")
return
async def on_message(self, message: Message):
if message.author.bot:
return
if not message.attachments:
return
for attachment in filter(utils.is_replay, message.attachments):
try:
replaydata = await attachment.read()
replayhash = hashlib.md5(replaydata).hexdigest()
os.makedirs(const.UPLOAD_DIR, exist_ok=True)
basename = replayhash + const.SC2EXT
filepath = os.path.join(const.UPLOAD_DIR, basename)
f = open(filepath, 'wb')
f.write(replaydata)
f.close()
replay = spawningtool.parser.parse_replay(filepath, cache_dir=const.CACHEDIR)
total = utils.get_replay_strs(replay)
f = io.StringIO(total)
result = discord.File(f, filename='buildorder.txt')
await message.reply(file=result)
except (HTTPException, NotFound, Forbidden) as e:
await message.reply(str(e))
except (ReadError, ReplayFormatError) as e:
await message.reply(str(e))