dice: add support for huge die rolls

This commit is contained in:
Denis-Cosmin Nutiu 2024-01-24 17:41:46 +02:00
parent dfab66d5c3
commit 1c7e55eb66
3 changed files with 23 additions and 6 deletions

View file

@ -3,6 +3,7 @@ from datetime import datetime
import disnake import disnake
from disnake.ext import commands from disnake.ext import commands
from disnake.ext.commands import CommandInvokeError
from src.dice.dice import DiceRoller, DieExpressionResult from src.dice.dice import DiceRoller, DieExpressionResult
@ -12,7 +13,7 @@ class DiceCog(commands.Cog):
self.bot = bot self.bot = bot
@staticmethod @staticmethod
def format_die_result_to_fields( def _format_embed_fields(
die_result: DieExpressionResult, die_result: DieExpressionResult,
) -> list[typing.Tuple[str, str]]: ) -> list[typing.Tuple[str, str]]:
roll_fields = [] roll_fields = []
@ -51,9 +52,21 @@ class DiceCog(commands.Cog):
description=f"{dice_expression} = {die_result.total}", description=f"{dice_expression} = {die_result.total}",
timestamp=datetime.now(), timestamp=datetime.now(),
) )
embed_fields = self.format_die_result_to_fields(die_result) embed_fields = self._format_embed_fields(die_result)
for title, value in embed_fields: exceeded_value = False
embed.add_field(title, value, inline=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.",
)
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}**!",
@ -61,3 +74,5 @@ class DiceCog(commands.Cog):
) )
except ValueError as e: except ValueError as e:
await ctx.send(f"Roll failed: {e}") await ctx.send(f"Roll failed: {e}")
except CommandInvokeError as e:
await ctx.send(f"Command failed: {e}")

View file

@ -45,6 +45,8 @@ class DieSemantics:
return ast return ast
# the number of dies is optional; by default, we have one die # the number of dies is optional; by default, we have one die
number_of_dies = ast.get("number_of_dies") or 1 number_of_dies = ast.get("number_of_dies") or 1
if number_of_dies > 10_000:
raise ValueError("invalid number of dies must be <= 10_000.")
die_type = ast.get("die_type") die_type = ast.get("die_type")
die_number = ast.get("die_number") die_number = ast.get("die_number")

View file

@ -2,8 +2,8 @@ from src.bot.discord.commands.dice import DiceCog
from src.dice.dice import DieExpressionResult, DieRollResult from src.dice.dice import DieExpressionResult, DieRollResult
def test_format_die_result_to_message(): def test_format_embed_fields():
message = DiceCog.format_die_result_to_fields( message = DiceCog._format_embed_fields(
DieExpressionResult( DieExpressionResult(
total=25, total=25,
dies=[ dies=[