--- a/rollbot.py Sat May 14 14:05:49 2016 +0800
+++ b/rollbot.py Sat May 14 14:07:31 2016 +0800
from argparse import ArgumentParser, FileType, SUPPRESS
return '(no help available, try experimenting)'
+ regex = re.compile('^(?P<times>\d+)?d(?P<sides>\d+)$', re.IGNORECASE)
+ return '%s <X>D<Y> means roll Y-sided die X times, if X is 1 it can be omitted' % self.name
+ def respond(self, words, message):
+ match = self.regex.match(words[1])
+ times = match.group('times')
+ sides = match.group('sides')
+ rolls, total = self.roll(int(times), int(sides))
+ return self.format(rolls, total)
+ return '%s usage: %s' % (self.name, self.usage())
+ def roll(self, times, sides):
+ results = [random.randint(1, sides) for i in range(times)]
+ def format(self, rolls, total):
+ return '(%s) = %d' % (' + '.join('%d' % r for r in rolls), total)
self.add_event_handler('session_start', self.start)