diff --git a/src/bot/discord/commands/dice.py b/src/bot/discord/commands/dice.py index 4039ac3..b0693e9 100644 --- a/src/bot/discord/commands/dice.py +++ b/src/bot/discord/commands/dice.py @@ -3,6 +3,7 @@ from datetime import datetime import disnake from disnake.ext import commands +from disnake.ext.commands import CommandInvokeError from src.dice.dice import DiceRoller, DieExpressionResult @@ -12,7 +13,7 @@ class DiceCog(commands.Cog): self.bot = bot @staticmethod - def format_die_result_to_fields( + def _format_embed_fields( die_result: DieExpressionResult, ) -> list[typing.Tuple[str, str]]: roll_fields = [] @@ -51,9 +52,21 @@ class DiceCog(commands.Cog): description=f"{dice_expression} = {die_result.total}", timestamp=datetime.now(), ) - embed_fields = self.format_die_result_to_fields(die_result) - for title, value in embed_fields: - embed.add_field(title, value, inline=False) + 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.", + ) await ctx.send( 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: await ctx.send(f"Roll failed: {e}") + except CommandInvokeError as e: + await ctx.send(f"Command failed: {e}") diff --git a/src/dice/semantics.py b/src/dice/semantics.py index 4f2f893..38edeb1 100644 --- a/src/dice/semantics.py +++ b/src/dice/semantics.py @@ -45,6 +45,8 @@ class DieSemantics: return ast # the number of dies is optional; by default, we have one die 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_number = ast.get("die_number") diff --git a/tests/bot/discord/commands/test_dice.py b/tests/bot/discord/commands/test_dice.py index 231034e..e4c39da 100644 --- a/tests/bot/discord/commands/test_dice.py +++ b/tests/bot/discord/commands/test_dice.py @@ -2,8 +2,8 @@ from src.bot.discord.commands.dice import DiceCog from src.dice.dice import DieExpressionResult, DieRollResult -def test_format_die_result_to_message(): - message = DiceCog.format_die_result_to_fields( +def test_format_embed_fields(): + message = DiceCog._format_embed_fields( DieExpressionResult( total=25, dies=[