Small fixes here and there

Use usr bot id to check for recursion instead of hardcoded name
More error handling during log parse
Post log analysis results even if it wasn't finished for some reason (e.g. truncated log)
Fix log summary alignment for GPU settings
Tweak product id reges: allow more formats, restrict to known product IDs
This commit is contained in:
13xforever
2018-02-16 22:57:26 +05:00
parent 9ec8a55d0a
commit b09cea6db6
4 changed files with 30 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
# discord-bot
Dependencies:
* python3.5 or newer
* python3.6 or newer
* pip for python3
* `$ python3 -m pip install -U discord.py`
* pyparsing for python3 (distro package or through pip)

22
bot.py
View File

@@ -20,8 +20,10 @@ from stream_handlers import stream_text_log, stream_gzip_decompress
channel_id = "291679908067803136"
bot_spam_id = "319224795785068545"
bot_admin_id = "267367850706993152"
rpcs3Bot = Bot(command_prefix="!")
id_pattern = '[A-z]{4}\\d{5}'
id_pattern = '((?:[BPSUVX][CL]|P[ETU]|NP)[AEHJKPUIX][A-Z])[ \\-]?(\\d{5})' # see http://www.psdevwiki.com/ps3/Productcode
nsp = NumericStringParser()
file_handlers = (
@@ -50,7 +52,7 @@ async def on_message(message: Message):
:param message: message
"""
# Self reply detect
if message.author.name == "RPCS3 Bot":
if message.author.id == rpcs3Bot.connection.user.id:
return
# Command detect
try:
@@ -61,8 +63,10 @@ async def on_message(message: Message):
# Code reply
code_list = []
for matcher in re.finditer(id_pattern, message.content):
code = str(matcher.group(0)).upper()
for matcher in re.finditer(id_pattern, message.content, flags=re.I):
letter_part = str(matcher.group(1))
number_part = str(matcher.group(2))
code = (letter_part + number_part).upper()
if code not in code_list:
code_list.append(code)
print(code)
@@ -79,6 +83,7 @@ async def on_message(message: Message):
# Log Analysis!
if len(message.attachments) > 0:
log = LogAnalyzer()
sent_log = False
print("Attachments present, looking for log file...")
for attachment in filter(lambda a: any(e['ext'] in a['url'] for e in file_handlers), message.attachments):
for handler in file_handlers:
@@ -102,9 +107,16 @@ async def on_message(message: Message):
message.channel,
log.get_report()
)
sent_log = True
break
elif error_code == LogAnalyzer.ERROR_FAIL:
break
if not sent_log:
print("Log analyzer didn't finish, probably a truncated log")
await rpcs3Bot.send_message(
message.channel,
log.get_report()
)
print("Stopping stream!")
del log
@@ -122,7 +134,7 @@ async def piracy_alert(message: Message, trigger: str):
"or {bot_admin}".format(
author=message.author.mention,
trigger=mask(trigger),
bot_admin=message.server.get_member('267367850706993152').mention
bot_admin=message.server.get_member(bot_admin_id).mention
)
)

View File

@@ -3,7 +3,7 @@ import re
from bot_config import piracy_strings
from bot_utils import get_code
SERIAL_PATTERN = re.compile('Serial: (?P<id>[A-z]{4}\d{5})')
SERIAL_PATTERN = re.compile('Serial: (?P<id>[A-z]{4}\\d{5})')
LIBRARIES_PATTERN = re.compile('Load libraries:(?P<libraries>.*)', re.DOTALL | re.MULTILINE)
@@ -39,7 +39,7 @@ class LogAnalyzer(object):
self.libraries = [lib.strip().replace('.sprx', '')
for lib
in re.search(LIBRARIES_PATTERN, self.buffer).group('libraries').strip()[1:].split('-')]
if len(self.libraries) > 0:
if len(self.libraries) > 0 and self.libraries[0] != "]": # [] when empty
self.report += 'Selected Libraries: ' + ', '.join(self.libraries) + '\n\n'
except KeyError as ke:
print(ke)
@@ -101,9 +101,9 @@ class LogAnalyzer(object):
'Rendering Mode: (?P<strict_rendering_mode>.*?)\n.*?',
flags=re.DOTALL | re.MULTILINE),
'string_format':
'Renderer: {renderer:>21s} | Resolution: {resolution}\n'
'Frame Limit: {frame_limit:>18s} | Write Color Buffers: {write_color_buffers}\n'
'VSync: {vsync:>24s} | Strict Rendering Mode: {strict_rendering_mode}\n'
'Renderer: {renderer:>24s} | Resolution: {resolution}\n'
'Frame Limit: {frame_limit:>21s} | Write Color Buffers: {write_color_buffers}\n'
'VSync: {vsync:>27s} | Strict Rendering Mode: {strict_rendering_mode}\n'
},
{
'end_trigger': 'Log:',

View File

@@ -9,8 +9,11 @@ def stream_text_log(stream):
def stream_gzip_decompress(stream):
dec = zlib.decompressobj(32 + zlib.MAX_WBITS) # offset 32 to skip the header
for chunk in stream:
rv = dec.decompress(chunk)
if rv:
yield rv
del rv
try:
rv = dec.decompress(chunk)
if rv:
yield rv
del rv
except zlib.error as zlr:
pass
del dec