From 33addf5b5f1309ae0e8812caf690f76cc073fc3f Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 22 Oct 2017 16:46:29 -0400 Subject: [PATCH] Added dice rolling command. --- helpcommand.txt | 3 ++- pocketbot.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/helpcommand.txt b/helpcommand.txt index bf4ac31..b3873b4 100644 --- a/helpcommand.txt +++ b/helpcommand.txt @@ -21,4 +21,5 @@ **!8ball**: You ask a question, it answeres. Simple! **!conch** or **!magicconch**: Same as the 8ball command but waaaaay cooler **!giveaway prize**: Do a giveaway based on users reacting with the emoji you react with secretly. Use in private and replace prize with an actual prize! -**!np**: Posts currently playing info for ToukuFM \ No newline at end of file +**!np**: Posts currently playing info for ToukuFM +**!roll**: Rolls any number of dice! Use like `!roll d20`, `!roll 2d6`, or `!roll 3d6 d20` \ No newline at end of file diff --git a/pocketbot.py b/pocketbot.py index 6446306..9b88093 100644 --- a/pocketbot.py +++ b/pocketbot.py @@ -9,6 +9,7 @@ import praw import os import requests import json +import re #Get script location cwd = os.path.dirname(os.path.realpath(__file__)) @@ -32,6 +33,12 @@ reddit = praw.Reddit(client_id=reddit_client_id,client_secret=reddit_client_secr metadataurl = config.get("configuration", "metadataurl") iceurl = config.get("configuration", "iceurl") +#Dice Rolling Function +def rolldie(x): + sides = int(x[1:]) + roll = random.randint(1,sides) + return roll + client = discord.Client() #Subroutine to get UPS Status @@ -265,6 +272,52 @@ def on_message(message): mixes = json.loads(requests.get("https://maki.jawa.moe/mixesjson/").text) yield from client.send_message(message.channel, "https://maki.jawa.moe/mixes/" + random.choice(mixes)['name']) + #Dice Roller (support rolling multiple dice at once) + elif message.content.lower().startswith('!roll'): + #Set starting Variables + args = message.content.partition(' ')[2].upper() + argsplit = args.split() + output = message.author.display_name + " rolled " + rolltotal = 0 + + #Go through each requested roll + for arg in argsplit: + #Roll once die multiple times (eg 20d20) + if re.match('^[0-9]+D[0-9]+$', arg): + dlocation = arg.index('D') + rolls = int(arg[:dlocation]) + arg = arg[dlocation:] + if rolls <= 25: + while rolls > 0: + rollresult = rolldie(arg) + output = output + arg + ": **" + str(rollresult) + "**, " + rolltotal += rollresult + rolls -= 1 + else: + rollminitotal = 0 + output = output + str(rolls) + '\*' + arg + ": **" + while rolls > 0: + rollresult = rolldie(arg) + rollminitotal += rollresult + rolls -= 1 + output = output + str(rollminitotal) + "**, " + rolltotal += rollminitotal + #Roll a single die (eg d20) + elif re.match('^D[0-9]+$', arg): + rollresult = rolldie(arg) + output = output + arg + ": **" + str(rollresult) + "**, " + rolltotal += rollresult + + #Finish generating message + if rolltotal > 0: + output = output + "for a total of **" + str(rolltotal) + "**!" + #Print error if no dice were rolled (aka if the total is still zero) + else: + output = "Oops, looks like no dice were rolled! Try something like `!roll d20`, `!roll 2d6`, or `!roll 3d6 d20` instead." + + #Send output to chat + yield from client.send_message(message.channel, output) + client.loop.create_task(ups_check())