Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add diceroll module #54

Open
wants to merge 2 commits into
base: bobbit-0.2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/bobbit/modules/roll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# roll.py

import random

# Metadata

NAME = 'roll'
ENABLE = True
PATTERN = r'^!roll\s*(?P<num>\d*)?d(?P<size>\d+)(?P<const>[-+]\d+)?$'
USAGE = '''Usage: !roll <number of dice>d<size of dice>[+-]<constant>
Rolls some dice.
'''

# Command

async def roll(bot, message, size, num, const):
# size must be a number > 0
try:
size = int(size)
if size == 0:
return
except ValueError as e:
return
# number of dice must be 0 < n <= 100
try:
num = int(num)
if num > 100 or num == 0:
return
except ValueError as e:
num = 1

output = f"Rolling {num}d{size}"
rolls = [random.randint(1, size) for d in range(num)]
total = sum(rolls)

if const:
sign = const[0]
const = int(const[1:])
if sign == '-':
total -= const
if sign == '+':
total += const
output += f"{sign}{const}"

output += f": \x02{total}\x02"

if num > 1:
output += f", from [{', '.join(str(r) for r in rolls)}]"
return message.with_body(output)

# Register

def register(bot):
return (
('command', PATTERN, roll),
)

# vim: set sts=4 sw=4 ts=8 expandtab ft=python: