diff options
| author | 2023-04-28 03:04:40 +0800 | |
|---|---|---|
| committer | 2023-04-28 03:04:40 +0800 | |
| commit | c8c76cb2293001a3f5a8122e581648002033eb15 (patch) | |
| tree | 26a4ded54d50d44ed8d0804f0109d4c38ac511d2 /hydroroll/plugins/HydroRoll_plugin_dice/__init__.py | |
| parent | 57a8f67b27aa617b7a993477231b3d6978f4f842 (diff) | |
| download | HydroRoll-0.1.0.tar.gz HydroRoll-0.1.0.zip | |
Diffstat (limited to 'hydroroll/plugins/HydroRoll_plugin_dice/__init__.py')
| -rw-r--r-- | hydroroll/plugins/HydroRoll_plugin_dice/__init__.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/hydroroll/plugins/HydroRoll_plugin_dice/__init__.py b/hydroroll/plugins/HydroRoll_plugin_dice/__init__.py new file mode 100644 index 0000000..2c9d2b4 --- /dev/null +++ b/hydroroll/plugins/HydroRoll_plugin_dice/__init__.py @@ -0,0 +1,51 @@ +import re +import random + +from iamai.log import logger + +from plugins.hydroroll_plugin_base import CommandPluginBase + +from .config import Config + +__all__ = ["Dice"] + +class Dice(CommandPluginBase[None, Config]): + Config = Config + + def __post_init__(self): + self.re_pattern = re.compile( + r"\s*(?P<dice_times>\d+)d(?P<dice_faces>\d+)([*x](?P<dice_multiply>\d+))?", + flags=re.I, + ) + + async def handle(self) -> None: + dice_times = int(self.msg_match.group("dice_times")) + dice_faces = int(self.msg_match.group("dice_faces")) + if self.msg_match.group("dice_multiply") is None: + dice_multiply = None + else: + dice_multiply = int(self.msg_match.group("dice_multiply")) + + if dice_times > self.config.max_dice_times: + await self.event.reply( + self.format_str(self.config.exceed_max_dice_times_str) + ) + return + + dice = [random.randint(1, dice_faces) for _ in range(dice_times)] + dice_sum = sum(dice) + if dice_multiply is None: + result_str = f"{dice_times}D{dice_faces}=" + if dice_times != 1: + result_str += f"{'+'.join(map(lambda x: str(x), dice))}=" + result_str += str(dice_sum) + else: + result_str = f"{dice_times}D{dice_faces}x{dice_multiply}=" + if dice_times != 1: + result_str += ( + f"({'+'.join(map(lambda x: str(x), dice))})x{dice_multiply}=" + ) + result_str += f"{dice_sum}x{dice_multiply}={dice_sum * dice_multiply}" + + logger.info(f"Dice Plugin: {result_str}") + await self.event.reply(self.format_str(self.config.message_str, result_str)) |
