[lore] !lore can retrieve more than one bit via a count parameter
This commit is contained in:
parent
9beca45ac7
commit
855625d88c
1 changed files with 25 additions and 9 deletions
|
@ -5,6 +5,7 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@ -12,8 +13,11 @@ from service import slack
|
||||||
|
|
||||||
LOGGER = logging.getLogger('cookie')
|
LOGGER = logging.getLogger('cookie')
|
||||||
MAX_PINS = 100
|
MAX_PINS = 100
|
||||||
|
LORE_FILE = 'lore.json'
|
||||||
CHANNELS = {}
|
CHANNELS = {}
|
||||||
|
|
||||||
|
LORE_RE = re.compile(r'!lore(\s+(?P<count>\d+))')
|
||||||
|
|
||||||
outputs = []
|
outputs = []
|
||||||
|
|
||||||
class Channel(object):
|
class Channel(object):
|
||||||
|
@ -132,13 +136,15 @@ def process_message(data):
|
||||||
|
|
||||||
LOGGER.debug('Received message: {}'.format(text))
|
LOGGER.debug('Received message: {}'.format(text))
|
||||||
|
|
||||||
if text == '!lore':
|
m = LORE_RE.match(text)
|
||||||
|
if m:
|
||||||
try:
|
try:
|
||||||
chid = data['channel']
|
chid = data['channel']
|
||||||
ch = CHANNELS[chid]
|
ch = CHANNELS[chid]
|
||||||
random_pin = _lore(ch)
|
lore = _lore(ch, int(m.group('count')))
|
||||||
if random_pin:
|
if lore:
|
||||||
outputs.append([chid, random_pin])
|
for l in lore:
|
||||||
|
outputs.append([chid, l])
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
LOGGER.error("Couldn't process !lore command: {}".format(e))
|
LOGGER.error("Couldn't process !lore command: {}".format(e))
|
||||||
|
|
||||||
|
@ -146,11 +152,21 @@ def process_message(data):
|
||||||
# Private
|
# Private
|
||||||
#
|
#
|
||||||
|
|
||||||
def _lore(channel):
|
def _lore(channel, count):
|
||||||
pins = channel.saved_pins
|
pins = channel.saved_pins
|
||||||
if not pins:
|
if not pins:
|
||||||
return None
|
return None
|
||||||
random_pin = random.choice(pins)
|
if len(pins) < count:
|
||||||
if random_pin['type'] == 'message':
|
return [_extract_lore(p) for p in pins]
|
||||||
return random_pin['message']['permalink']
|
out_lore = set()
|
||||||
return '```\n' + str(random_pin) + '\n```'
|
while len(out_lore) < count:
|
||||||
|
random_lore = random.choice(pins)
|
||||||
|
lore = _extract_lore(random_lore)
|
||||||
|
out_lore.add(lore)
|
||||||
|
return out_lore
|
||||||
|
|
||||||
|
def _extract_lore(obj):
|
||||||
|
if obj['type'] == 'message':
|
||||||
|
return obj['message']['permalink']
|
||||||
|
# If nothing matches just return the object itself as a preformatted JSON object
|
||||||
|
return '```\n' + obj + '\n```'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue