I'm trying to implement a feature in my program that the user would be able to add or subtract a randomly generated number using a specific sided die. The foundation of my code is posted here:
import discord
import random
DND_1d20 = range(1, 21)
# Roll d20
if message.content == ";roll 1d20":
response = random.choice(DND_1d20)
response_str = "You rolled {0}".format(response)
if response_str == "You rolled 20":
await message.channel.send("**Critical Hit!**\n You rolled 20")
if response_str == "You rolled 1":
await message.channel.send("**Critical Fail!**\n You rolled 1")
I would like the user to be able to specify a dice roll ";1d20" BUT also have the ability to add ";1d20+(x)" or subtract ";1d20-(x)" any number (x) from the generated dice roll. The logic would look something like this
-user ";1d20+2" Lets say the random number generated would be 6. Since the user wants to add 2 to the random number we generated, the outcome would be 8.
-bot "You rolled 8"
# Roll d20
if message.content == ";roll 1d20":
response = random.choice(DND_1d20)
response_str = "You rolled {0}".format(response)
if response_str == "You rolled 20":
await message.channel.send("**Critical Hit!**\n You rolled 20")
if response_str == "You rolled 1":
await message.channel.send("**Critical Fail!**\n You rolled 1")
else:
if message.content == "-":
How would I go about doing this? I am really confused on where to start. I dont think the code above is right, because the message would have to exactly be a "-". Also, how would I incorporate the value (x), since it could be a vast array of numbers, or the +/- signs from the user input?
Any help is appreciated!