Testing/CI #21
+47
-48
@@ -9,6 +9,7 @@ import os
|
|||||||
import random
|
import random
|
||||||
import caldav
|
import caldav
|
||||||
import warnings
|
import warnings
|
||||||
|
import re
|
||||||
|
|
||||||
class Shitpost(commands.Cog):
|
class Shitpost(commands.Cog):
|
||||||
"""Schijtpaal commando's. Alleen bruikbaar in de daarvoor toegewezen schijtpaal kanalen."""
|
"""Schijtpaal commando's. Alleen bruikbaar in de daarvoor toegewezen schijtpaal kanalen."""
|
||||||
@@ -466,61 +467,59 @@ class Shitpost(commands.Cog):
|
|||||||
else:
|
else:
|
||||||
yield from ctx.channel.send(ctx.author.mention + ' Het is nu ' + datetime.now().strftime('%H:%M'))
|
yield from ctx.channel.send(ctx.author.mention + ' Het is nu ' + datetime.now().strftime('%H:%M'))
|
||||||
|
|
||||||
|
def get_dice_roll(self, nrOfDice, dieType, modifier=None):
|
||||||
|
if nrOfDice > 99 or dieType > 9999 or dieType < 2 or (modifier and abs(modifier) > 9999):
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Do the rolling
|
||||||
|
random.seed(datetime.now())
|
||||||
|
total = 0
|
||||||
|
rolls = []
|
||||||
|
for x in range(0, nrOfDice):
|
||||||
|
roll = random.randint(1, dieType)
|
||||||
|
total += roll
|
||||||
|
rolls.append(roll)
|
||||||
|
total += modifier if modifier else 0
|
||||||
|
|
||||||
|
#Tally results
|
||||||
|
modifierText = "%+d" % modifier if modifier else "" # print modifier with sign
|
||||||
|
returnText = f"Rolling {nrOfDice}d{dieType}{modifierText}...\n"
|
||||||
|
rolls = [f"[**{roll}**]" if dieType >= 10 and (roll == 1 or roll == dieType) else f"[{roll}]" for roll in rolls]
|
||||||
|
returnText += "+".join(rolls)
|
||||||
|
returnText += modifierText
|
||||||
|
if nrOfDice > 1 or modifier: # only show calculation if it makes sense:
|
||||||
|
returnText += " = **" + str(total) + "**"
|
||||||
|
return returnText
|
||||||
|
|
||||||
|
def get_dice_parameters(self, dice):
|
||||||
|
match = re.match(r"(\d+)d(\d+)(\s*([+-])\s*(\d+))?\s*", dice.lower())
|
||||||
|
if not match:
|
||||||
|
return None
|
||||||
|
numDice = int(match.group(1))
|
||||||
|
die = int(match.group(2))
|
||||||
|
modifier = None
|
||||||
|
|
||||||
|
modified = match.group(3)
|
||||||
|
if modified:
|
||||||
|
modifierType = match.group(4)
|
||||||
|
modifier = int(match.group(5))
|
||||||
|
if "-" in modifierType:
|
||||||
|
modifier = -modifier
|
||||||
|
return (numDice, die, modifier)
|
||||||
|
|
||||||
@commands.command(pass_context=True)
|
@commands.command(pass_context=True)
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def roll(self, ctx, dice="1d20", modifier1="", modifier2=""):
|
def roll(self, ctx, *, dice):
|
||||||
"""Roll een dobbelsteen. Of een paar. Bijvoorbeeld:
|
"""Roll een dobbelsteen. Of een paar. Bijvoorbeeld:
|
||||||
!roll 4d20
|
!roll 4d20
|
||||||
!roll 1d6 + 5
|
!roll 1d6 + 5
|
||||||
"""
|
"""
|
||||||
diceSplit = dice.lower().split('d')
|
(nrOfDice, dieType, modifier) = self.get_dice_parameters(dice)
|
||||||
if len(diceSplit) != 2:
|
message = self.get_dice_roll(nrOfDice, dieType, modifier)
|
||||||
return
|
if message:
|
||||||
modifier1 = modifier1.replace("+", "") #this is both possible: "1d20 + 1" and "1d20 +1"
|
yield from ctx.channel.send(message)
|
||||||
modifier2 = modifier2.replace("+", "")
|
else:
|
||||||
if modifier1 == "-":
|
yield from ctx.channel.send("Die roll vind ik echt een beetje te mal..")
|
||||||
modifier1 = ""; #this is also possible: "1d20 - 1"
|
|
||||||
modifier2 = "-" + modifier2
|
|
||||||
try: #check if they are numbers:
|
|
||||||
nrOfDice = 1 if (diceSplit[0]=="") else int(diceSplit[0]) #allow input like: "!roll d20"
|
|
||||||
dieType = int(diceSplit[1])
|
|
||||||
mod1 = 0 if (modifier1=="") else int(modifier1)
|
|
||||||
mod2 = 0 if (modifier2=="") else int(modifier2)
|
|
||||||
except ValueError:
|
|
||||||
return
|
|
||||||
#validate values:
|
|
||||||
if nrOfDice < 1 or nrOfDice > 99 or dieType < 2 or dieType > 100:
|
|
||||||
return
|
|
||||||
if mod1 < -999 or mod1 > 999 or mod2 < -999 or mod2 > 999:
|
|
||||||
return
|
|
||||||
|
|
||||||
#roll:
|
|
||||||
random.seed(datetime.now())
|
|
||||||
total = 0
|
|
||||||
returnText = "Rolling " + str(nrOfDice) + "d" + str(dieType) + "...\n"
|
|
||||||
for x in range(0, nrOfDice):
|
|
||||||
if x != 0:
|
|
||||||
returnText += "+ "
|
|
||||||
roll = random.randint(1, dieType)
|
|
||||||
total += roll
|
|
||||||
if dieType >= 10 and (roll == 1 or roll == dieType):
|
|
||||||
returnText += "[**" + str(roll) + "**] " #make significant numbers bold
|
|
||||||
else:
|
|
||||||
returnText += "[" + str(roll) + "] "
|
|
||||||
total += mod1 + mod2
|
|
||||||
if mod1 != 0:
|
|
||||||
if mod1 > 0:
|
|
||||||
returnText += "+"
|
|
||||||
returnText += str(mod1) + " "
|
|
||||||
if mod2 != 0:
|
|
||||||
if mod2 > 0:
|
|
||||||
returnText += "+"
|
|
||||||
returnText += str(mod2) + " "
|
|
||||||
if nrOfDice > 1 or mod1 != 0 or mod2 != 0: #only show calculation if it makes sense:
|
|
||||||
returnText += " = __**" + str(total) + "**__"
|
|
||||||
|
|
||||||
yield from ctx.channel.send(returnText)
|
|
||||||
|
|
||||||
def get_weerbericht(self, wData, verbose=None):
|
def get_weerbericht(self, wData, verbose=None):
|
||||||
if not wData:
|
if not wData:
|
||||||
|
|||||||
Reference in New Issue
Block a user