dice: add support for huge die rolls
This commit is contained in:
parent
dfab66d5c3
commit
1c7e55eb66
3 changed files with 23 additions and 6 deletions
|
@ -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,13 +52,27 @@ 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_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}**!",
|
||||
embed=embed,
|
||||
)
|
||||
except ValueError as e:
|
||||
await ctx.send(f"Roll failed: {e}")
|
||||
except CommandInvokeError as e:
|
||||
await ctx.send(f"Command failed: {e}")
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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=[
|
||||
|
|
Loading…
Reference in a new issue