ubot2/plugins/countdown.py

69 lines
1.5 KiB
Python
Raw Normal View History

2016-09-11 12:56:50 -04:00
# countdown.py
# Count down from the specified number
# Eryn Wells <eryn@erynwells.me>
import logging
import re
LOGGER = logging.getLogger('countdown')
2016-12-04 00:56:40 -05:00
COUNTDOWN_RE = re.compile(r'(!countdown)\s+(\d+|stop)\s*')
2016-09-11 12:56:50 -04:00
# rtmbot interface
crontable = []
outputs = []
# Check countdowns every second.
crontable.append([1, "_do_countdown"])
# countdowns in flight.
COUNTDOWNS = []
def process_message(data):
try:
text = data['text']
except KeyError:
LOGGER.error('Missing "text" key in data.')
return
2016-12-04 00:56:40 -05:00
match = COUNTDOWN_RE.match(text)
2016-09-11 12:56:50 -04:00
if not match:
return
2016-12-04 00:56:40 -05:00
2016-09-11 12:56:50 -04:00
cmd = match.group(1)
if not cmd:
return
2016-12-04 00:56:40 -05:00
arg = match.group(2)
if arg == 'stop':
_kill_countdowns()
outputs.append([data['channel'], 'Stopped all countdowns.'])
else:
try:
n = int(match.group(2))
except ValueError:
LOGGER.error('Expected number but got {}'.format(match.group(2)))
return
_setup_timer(data['channel'], n)
2016-09-11 12:56:50 -04:00
def _setup_timer(channel, time):
COUNTDOWNS.append([channel, time])
2016-12-04 00:56:40 -05:00
def _kill_countdowns():
del COUNTDOWNS[:]
2016-09-11 12:56:50 -04:00
def _do_countdown():
global COUNTDOWNS
if len(COUNTDOWNS) == 0:
return
LOGGER.debug('Processing countdowns: {}'.format(COUNTDOWNS))
for count in COUNTDOWNS:
out = str(count[1]) if count[1] > 0 else "go"
count[1] -= 1
outputs.append([count[0], out])
# Remove expired timers.
COUNTDOWNS = list(filter(lambda c: c[1] >= 0, COUNTDOWNS))