Enhancing my Discord bot to perform complex dice roll calculations

Hey everyone! I’ve got a Discord bot that can do simple dice rolls. But now I want to make it smarter. I want it to handle stuff like ‘1d20 + 2d6 + 5’ all in one go. You know, mix different dice and add numbers too.

Right now, my bot can do basic rolls like ‘2d6’. It splits the roll and modifier if there’s a plus or minus. But I’m stuck on how to make it work with multiple dice and math operations.

Here’s what I’m hoping for:

  • Input: !roll 2d8 + 1d6 - 3
  • Output: You rolled: 2d8 (5+7) + 1d6 (4) - 3 = 13

Any ideas on how to upgrade my code to do this? I’m using Python with discord.ext.commands. Thanks for any help!

@bot.command(name='roll')
async def dice_roll(ctx, expression: str):
    # Current simple implementation
    dice, sides = map(int, expression.split('d'))
    result = sum(random.randint(1, sides) for _ in range(dice))
    await ctx.send(f'You rolled {result}')

# Need help expanding this function

I’ve tackled a similar challenge with my Discord bot. Here’s what worked for me:

First, I used regex to parse the input string into separate components (dice rolls and modifiers). Then I created a function to handle each dice roll separately.

For the calculation part, I used the ‘eval’ function cautiously. It lets you perform the math operations dynamically. Just be sure to sanitize the input to prevent any security issues.

To display individual roll results, I kept a list of each die’s outcomes and formatted them in the final output.

The trickiest part was error handling. Users can input all sorts of weird stuff, so robust input validation is crucial.

It took some trial and error, but the end result was worth it. My players love seeing the breakdown of complex rolls. Good luck with your bot upgrade!

hey, i’ve done smthing similar before. u could use regex to break down the input, then make a function for each dice type. for the math part, eval() works but be careful with it. keep track of individual rolls to show in the output. and yeah, watch out for weird inputs from users. good luck with ur bot!

I’ve implemented a similar feature in my bot using a combination of regex and a custom parser. The key is to tokenize the input string into dice rolls and operators, then process each token individually. For dice rolls, I use a separate function that generates random numbers and keeps track of individual results. For operators and static numbers, I store them as-is.

After processing all tokens, I construct the final result string and calculate the total. To handle the math operations safely, I’ve created a simple arithmetic evaluator instead of using eval(). This approach gives you full control over the allowed operations and avoids potential security risks.

Remember to implement thorough error checking and provide helpful error messages for invalid inputs. It’s also worth considering adding support for more advanced features like exploding dice or rerolling 1s if your users would find that useful.