Fix up bomb regexes

This commit is contained in:
Eryn Wells 2016-10-18 19:58:26 -04:00
parent 956e540fed
commit d08630da75

View file

@ -34,11 +34,8 @@ def process_hello(data):
with open(c, 'r') as f: with open(c, 'r') as f:
COLLECTIONS[parts[1]] = json.load(f) COLLECTIONS[parts[1]] = json.load(f)
collections_re = '|'.join(COLLECTIONS.keys()) _build_regexes(COLLECTIONS.keys())
collections_re = r'(?P<collection>{})'.format(collections_re)
global CMD_RE
CMD_RE = re.compile(r'!{}(\s+(me|(?P<count>\d+)))?'.format(collections_re))
ADD_RE = re.compile(r'!bombadd\s+{}\s+(?P<item>\S+)'.format(collections_re))
def process_message(data): def process_message(data):
try: try:
@ -47,17 +44,31 @@ def process_message(data):
LOGGER.error('Missing "text" key in data.') LOGGER.error('Missing "text" key in data.')
return return
match = CMD_RE.match(text) try:
if match: match = CMD_RE.match(text)
_handle_bomb(match, data['channel']) if match:
match = ADD_RE.match(text) _handle_bomb(match, data['channel'])
if match: match = ADD_RE.match(text)
_handle_add(match, data['channel']) 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 # Bomb bomb bomb
# #
def _build_regexes(collections):
collections_re = '|'.join(collections)
collections_re = r'(?P<collection>{})'.format(collections_re)
global CMD_RE
CMD_RE = re.compile(r'!{}(\s+(me|(?P<count>\d+)))?'.format(collections_re))
global ADD_RE
ADD_RE = re.compile(r'!bombadd\s+{}\s+(?P<item>\S+)'.format(collections_re))
def _handle_bomb(match, channel): def _handle_bomb(match, channel):
collection = match.group('collection') collection = match.group('collection')