pinhook/examples/twitch/plugins/dice.py

30 lines
979 B
Python
Raw Normal View History

2018-02-07 00:30:54 +00:00
import random
import re
import pinhook.plugin
dicepattern = re.compile('(?P<amount>\d+)d(?P<sides>\d+)\+?(?P<modifier>\d+)?')
def build_output(rolls, modifier):
if len(rolls) == 1:
start = str(sum(rolls))
else:
all_rolls = ''.join([str(i)+', ' for i in rolls]).strip(', ')
start = '{} = {}'.format(all_rolls, sum(rolls))
if modifier:
2018-02-07 00:43:45 +00:00
output = start + ' + {} = {}'.format(modifier, sum(rolls) + int(modifier))
2018-02-07 00:30:54 +00:00
else:
output = start
return output
@pinhook.plugin.command('!roll')
2018-02-07 00:30:54 +00:00
def roll(msg):
2018-02-07 00:39:50 +00:00
matches = dicepattern.match(msg.arg)
2018-02-07 00:30:54 +00:00
if matches:
msg.logger.info('Valid dice roll: {}'.format(msg.arg))
rolls = [random.randrange(1, int(matches.group('sides'))+1) for i in range(int(matches.group('amount')))]
output = build_output(rolls, matches.group('modifier'))
else:
output = '{}: improper format, should be NdN+N'.format(msg.nick)
return pinhook.plugin.message(output)