Johz

decide and roll phenny commands

Mar 10th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. ## Import and setup lines
  2.  
  3. import random
  4. from operator import add, sub
  5. import re
  6.  
  7. DICE_REGEX = re.compile('^(\d+)?\s*d\s*(\d+)(?:\s*(\+|-)\s*(\d+))?$')
  8.  
  9. ## Function decleration
  10.  
  11. def decide(phenny, input):
  12.     """.decide - Deciding function."""
  13.     msg = input.groups()[1]
  14.     if msg is None:
  15.         msg = ''      # Hack
  16.     if '|' in msg:
  17.         msg = [i.strip() for i in msg.split('|')]
  18.         if not all(msg):
  19.             ret = "I'm sorry {}, but some of those options don't exist."
  20.             ret = ret.format(input.nick)
  21.         else:
  22.             ret = '{}: {}'.format(input.nick, random.choice(msg))
  23.     else:
  24.         ret  = "I'm sorry {}, but you haven't given me anything "
  25.         ret += "to choose from there."
  26.         ret  = ret.format(input.nick)
  27.     phenny.say(ret)
  28.    
  29. decide.commands = ['decide', 'choose']
  30. decide.priority = "low"
  31. decide.example = ".decide option 1 | option 2"
  32.  
  33. def roll(phenny, input):
  34.     msg = input.groups()[1]
  35.     if not msg: # Then there was no message.
  36.         ret = "{}: {}".format(input.nick, random.randrange(1,7))
  37.         phenny.say(ret)
  38.         return
  39.     # Otherwise, deal with a dnd roll.
  40.     msg = msg.strip()
  41.     match = DICE_REGEX.match(msg)
  42.     if match:
  43.         dice_no, sides, op, mod = match.groups()     # Un-group the match
  44.         sides = int(sides)
  45.         dice_no = 1 if dice_no is None else int(dice_no)   # Set optional params
  46.         mod = 0 if mod is None else int(mod)               # to default.
  47.         op = add if op == '+' else sub
  48.  
  49.         if dice_no > 20:  # You cannot be serious.
  50.             phenny.say("There are way too many dice there for me to "
  51.                       "keep track of.  What do you think I am, a robot?")
  52.             return
  53.  
  54.         roll_list = []
  55.         for i in range(dice_no):
  56.             roll_list.append(random.randint(1,sides))
  57.  
  58.         total = op(sum(roll_list), mod)
  59.         ret = "{}: You rolled {}".format(input.nick, total)
  60.         if 1 < dice_no < 10:
  61.             ret += " made from {}".format(str(roll_list))
  62.         phenny.say(ret)
  63.         return
  64.    
  65.     else:
  66.         phenny.say("That doesn't appear to be a dnd roll.  Try just .roll")
  67.  
  68. roll.commands = ['roll']
  69. roll.priority = 'low'
  70. roll.example = '.roll or .roll 3d6 [or .roll d6 + 3 etc]'
Advertisement
Add Comment
Please, Sign In to add comment