示例#1
0
文件: droll.py 项目: dafzor/daf-cogs
    async def droll(self, ctx: Context, exp: str):
        """Rolls a set of dices and/or evaluates a mathematical expression."""

        try:
            ps = xdice.roll(exp)
            await ctx.send("{} `{}` = ({}) = **{}**".format(ctx.author.mention, exp, ps.format(True), ps))
        except:
            await ctx.send("Error evaluating the given expression.")
    async def _roll_private(self, ctx:SlashContext, input:str):
        await ctx.defer(hidden=True)

        try:
            score = xdice.roll(input)
        except Exception as e:
            logger.exception(f"An Error occured throwing dice:\n{e}")
            await ctx.send("Ooops, something went wrong with the throw you passed, please check out this [Dice Notation](https://xdice.readthedocs.io/en/latest/dice_notation.html).\nIf the problem persists please open an Issue [here](https://github.com/Sntx626/Rolling_Discord-A_Discord_Dice_Bot/issues).", hidden=True)
            return
        
        description = ""
        footer = ""
        if len(score.scores()) == 0:
            await ctx.send("Your command doesn't throw any dice!", hidden=True)
            return
        elif len(score.scores()) < 128:
            if len(score.scores()) == 1 and score.format().find("+") == -1 and score.format().find("-") == -1 and score.format().find("*") == -1 and score.format().find("/") == -1 and score.format().find("min") == -1 and score.format().find("max") == -1 and score.format().find("abs") == -1:
                throws = score.format().lstrip('(').rstrip(')').lstrip('[').rstrip(']')
            else:
                throws = score.format().lstrip('(').rstrip(')')
            if len(score.scores()) == 1 and len(score.scores()[0].detail) == 1 and score.format().find("+") == -1 and score.format().find("-") == -1 and score.format().find("*") == -1 and score.format().find("/") == -1 and score.format().find("min") == -1 and score.format().find("max") == -1 and score.format().find("abs") == -1:
                description += f'**{throws}**'
            else:
                description += f'{throws}\n**= {score}**'
            if len(score.scores()[0].dropped) > 0:#
                dropped = []
                for s in score.scores():
                    dropped.append(s.dropped)
                footer = f'Dropped: {dropped}'
        else:
            await ctx.send("I'm sorry you can only throw up to 127 times per command.", hidden=True)
            return

        embed = discord.Embed(
            description = description,
            colour = discord.Colour.from_rgb(0, 0, 0)
        )
        if not footer == "":
            embed.set_footer(text=footer)
        await ctx.send(embed=embed)
示例#3
0
文件: roll.py 项目: cro-ki/xdice
def main():
    parser = argparse.ArgumentParser(
        prog="roll",
        description="Command Line Interface for the xdice library")
    parser.add_argument(
        "expression",
        nargs="+",
        help="mathematical expression(s) containing dice <n>d<s> patterns",
    )
    parser.add_argument(
        "-V",
        "--version",
        action="store_true",
        help="print the xdice version string and exit",
    )
    parser.add_argument(
        "-n",
        "--num_only",
        action="store_true",
        help="print numeric result only",
    )
    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="print a verbose result",
    )
    args = parser.parse_args()

    if args.version:
        print("XDice {}".format(xdice.__VERSION__))
        return

    for expr in args.expression:
        ps = xdice.roll(expr)

        if args.num_only:
            print(ps)
        else:
            print("{}\t({})".format(ps, ps.format(args.verbose)))
示例#4
0
async def roll(message, content):
    dice = xdice.roll(content)
    return "{} = {}".format(dice.format(), dice)
示例#5
0
 async def roll_dice(self, message):
     roller = message.author.mention
     request = message.content.strip('%')
     result = xdice.roll(f'{request}')
     await self.send_message(message.channel,
                             f'{roller}: {result.format()} => {result}')
示例#6
0
文件: test.py 项目: cro-ki/xdice
    def test_patterns_validation(self):
        """check the with behaviour with valid / invalid patterns"""
        # check valid expressions
        xdice.roll("3")
        xdice.roll("0d6")
        xdice.roll("1d6")
        xdice.roll("1d66")
        xdice.roll("1d4+2")
        xdice.roll("1d4-2")
        xdice.roll("1d4 - 2 ")
        xdice.roll("6d102+123")
        xdice.roll("6d102+123+8d6")
        xdice.roll("6d102+123+8d6+2+1+1-8-6d4+8-2d101+100d2")
        xdice.roll("32+3-0-2")
        xdice.roll("1d1-3")
        xdice.roll("1000d1000")
        xdice.roll("10 d 10")
        xdice.roll("10*1d10/2")
        xdice.roll("1d20**2")
        xdice.roll("abs(1d6-1d10)")
        xdice.roll("max(1d6,2d4)")
        xdice.roll("min(1d6,2d4)")
        xdice.roll("d")
        xdice.roll("2d")
        xdice.roll("d6")
        xdice.roll("3d6l")
        xdice.roll("3d6l2")
        xdice.roll("3d6h")
        xdice.roll("3d6h2")
        xdice.roll("6d6lh")
        xdice.roll("6d6lh2")
        xdice.roll("6d6l2h")
        xdice.roll("6d6l2h2")
        xdice.roll("3dlh")
        xdice.roll("1d%")
        xdice.roll("d%")
        xdice.roll("1+R3(1d6+1)")
        xdice.roll("3d6!")
        xdice.roll("3d6x")
        xdice.roll("3d6h1x")
        xdice.roll("3df")
        xdice.roll("3d6+1df")

        # test invalid expressions
        self.assertRaises(ValueError, xdice.roll, "")
        self.assertRaises(ValueError, xdice.roll, "1d0")
        self.assertRaises(TypeError, xdice.roll, "abc")
        self.assertRaises(TypeError, xdice.roll, "1d2,3")
        self.assertRaises(ValueError, xdice.roll, "1d6l2")
        self.assertRaises(ValueError, xdice.roll, "1d6h2")
        self.assertRaises(ValueError, xdice.roll, "1d6lh")
        self.assertRaises(SyntaxError, xdice.roll, "1d6f")
        self.assertRaises(SyntaxError, xdice.roll, "3f")
        self.assertRaises(SyntaxError, xdice.roll, "1+R3(1d6+1")
示例#7
0
文件: test.py 项目: cro-ki/xdice
    def test_roll(self):
        ps1 = xdice.roll("6d1+6")
        ps2 = xdice.PatternScore("{0}+6", [xdice.Score([1, 1, 1, 1, 1, 1])])

        self.assertEqual(ps1, ps2)