From d08630da75562437306d198277771215268dbb70 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 18 Oct 2016 19:58:26 -0400 Subject: [PATCH] Fix up bomb regexes --- plugins/bomb.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/plugins/bomb.py b/plugins/bomb.py index 12a8036..ffc31b8 100644 --- a/plugins/bomb.py +++ b/plugins/bomb.py @@ -34,11 +34,8 @@ def process_hello(data): with open(c, 'r') as f: COLLECTIONS[parts[1]] = json.load(f) - collections_re = '|'.join(COLLECTIONS.keys()) - collections_re = r'(?P{})'.format(collections_re) - global CMD_RE - CMD_RE = re.compile(r'!{}(\s+(me|(?P\d+)))?'.format(collections_re)) - ADD_RE = re.compile(r'!bombadd\s+{}\s+(?P\S+)'.format(collections_re)) + _build_regexes(COLLECTIONS.keys()) + def process_message(data): try: @@ -47,17 +44,31 @@ def process_message(data): LOGGER.error('Missing "text" key in data.') return - match = CMD_RE.match(text) - if match: - _handle_bomb(match, data['channel']) - match = ADD_RE.match(text) - if match: - _handle_add(match, data['channel']) + try: + match = CMD_RE.match(text) + if match: + _handle_bomb(match, data['channel']) + match = ADD_RE.match(text) + if match: + _handle_add(match, data['channel']) + except AttributeError: + if not CMD_RE or not ADD_RE: + LOGGER.debug("Regexes weren't loaded. Something went wrong.") + # Attempt to rebuild the regexes again... + _build_regexes(COLLECTIONS.keys()) # # Bomb bomb bomb # +def _build_regexes(collections): + collections_re = '|'.join(collections) + collections_re = r'(?P{})'.format(collections_re) + global CMD_RE + CMD_RE = re.compile(r'!{}(\s+(me|(?P\d+)))?'.format(collections_re)) + global ADD_RE + ADD_RE = re.compile(r'!bombadd\s+{}\s+(?P\S+)'.format(collections_re)) + def _handle_bomb(match, channel): collection = match.group('collection')