add roll_advantage and roll_disadvantage commands
This commit is contained in:
parent
4c8be88d3e
commit
4f9d69a06c
2 changed files with 75 additions and 21 deletions
|
@ -54,4 +54,7 @@ I use the following permissions:
|
||||||
6. Run `python -m src.main`
|
6. Run `python -m src.main`
|
||||||
7. Invite the bot to your Discord server.
|
7. Invite the bot to your Discord server.
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
Made with 💞 by [nuculabs.dev](https://blog.nuculabs.dev)
|
Made with 💞 by [nuculabs.dev](https://blog.nuculabs.dev)
|
|
@ -12,6 +12,31 @@ class DiceCog(commands.Cog):
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
|
||||||
|
def _get_dice_result_embed(self, dice_expression, die_result):
|
||||||
|
"""
|
||||||
|
Returns the dice result embed.
|
||||||
|
"""
|
||||||
|
embed = disnake.Embed(
|
||||||
|
title=f"{die_result.total} !!",
|
||||||
|
description=f"{dice_expression} = {die_result.total}",
|
||||||
|
timestamp=datetime.now(),
|
||||||
|
)
|
||||||
|
embed_fields = self._format_embed_fields(die_result)
|
||||||
|
exceeded_value = False
|
||||||
|
for title, value in embed_fields[:20]:
|
||||||
|
if len(value) > 1024:
|
||||||
|
exceeded_value = True
|
||||||
|
embed.add_field(title, f"{value[:1020]}...", inline=False)
|
||||||
|
else:
|
||||||
|
embed.add_field(title, value, inline=False)
|
||||||
|
# handle large rolls
|
||||||
|
if len(embed_fields) > 20 or exceeded_value:
|
||||||
|
embed.add_field(
|
||||||
|
"huuuuge 🎲",
|
||||||
|
"Due to technical limitations only first 20 partial-rolls are shown.",
|
||||||
|
)
|
||||||
|
return embed
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _format_embed_fields(
|
def _format_embed_fields(
|
||||||
die_result: DieExpressionResult,
|
die_result: DieExpressionResult,
|
||||||
|
@ -47,27 +72,53 @@ class DiceCog(commands.Cog):
|
||||||
|
|
||||||
die_result: DieExpressionResult = DiceRoller.roll(dice_expression)
|
die_result: DieExpressionResult = DiceRoller.roll(dice_expression)
|
||||||
|
|
||||||
embed = disnake.Embed(
|
embed = self._get_dice_result_embed(dice_expression, die_result)
|
||||||
title=f"{die_result.total} !!",
|
await ctx.send(
|
||||||
description=f"{dice_expression} = {die_result.total}",
|
f"The mighty **{ctx.author.name}** has rolled the dice for a total of **{die_result.total}**!",
|
||||||
timestamp=datetime.now(),
|
embed=embed,
|
||||||
)
|
)
|
||||||
embed_fields = self._format_embed_fields(die_result)
|
except ValueError as e:
|
||||||
exceeded_value = False
|
await ctx.send(f"Roll failed: {e}")
|
||||||
for title, value in embed_fields[:20]:
|
except CommandInvokeError as e:
|
||||||
if len(value) > 1024:
|
await ctx.send(f"Command failed: {e}")
|
||||||
exceeded_value = True
|
|
||||||
embed.add_field(title, f"{value[:1020]}...", inline=False)
|
@commands.command(name="roll_advantage", aliases=["ra"])
|
||||||
else:
|
async def roll_advantage(self, ctx, dice_expression: str):
|
||||||
embed.add_field(title, value, inline=False)
|
"""
|
||||||
|
Rolls a simple die with advantage
|
||||||
# handle large rolls
|
"""
|
||||||
if len(embed_fields) > 20 or exceeded_value:
|
try:
|
||||||
embed.add_field(
|
if dice_expression == "":
|
||||||
"huuuuge 🎲",
|
return
|
||||||
"Due to technical limitations only first 20 partial-rolls are shown.",
|
|
||||||
)
|
die_result: DieExpressionResult = DiceRoller.roll(
|
||||||
|
f"{dice_expression} adv {dice_expression}"
|
||||||
|
)
|
||||||
|
|
||||||
|
embed = self._get_dice_result_embed(dice_expression, die_result)
|
||||||
|
await ctx.send(
|
||||||
|
f"The mighty **{ctx.author.name}** has rolled the dice for a total of **{die_result.total}**!",
|
||||||
|
embed=embed,
|
||||||
|
)
|
||||||
|
except ValueError as e:
|
||||||
|
await ctx.send(f"Roll failed: {e}")
|
||||||
|
except CommandInvokeError as e:
|
||||||
|
await ctx.send(f"Command failed: {e}")
|
||||||
|
|
||||||
|
@commands.command(name="roll_disadvantage", aliases=["rd"])
|
||||||
|
async def roll_disadvantage(self, ctx, dice_expression: str):
|
||||||
|
"""
|
||||||
|
Rolls a simple die with disadvantage
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if dice_expression == "":
|
||||||
|
return
|
||||||
|
|
||||||
|
die_result: DieExpressionResult = DiceRoller.roll(
|
||||||
|
f"{dice_expression} dis {dice_expression}"
|
||||||
|
)
|
||||||
|
|
||||||
|
embed = self._get_dice_result_embed(dice_expression, die_result)
|
||||||
await ctx.send(
|
await ctx.send(
|
||||||
f"The mighty **{ctx.author.name}** has rolled the dice for a total of **{die_result.total}**!",
|
f"The mighty **{ctx.author.name}** has rolled the dice for a total of **{die_result.total}**!",
|
||||||
embed=embed,
|
embed=embed,
|
||||||
|
|
Loading…
Reference in a new issue