コード例 #1
0
    async def dice_95x5(self,
                        ctx: utils.Context,
                        *,
                        bet: localutils.BetAmount = None):
        """
        Place a bet on a dice roll - any number rolled that's higher than 95 will multiply your bet x5.
        """

        await self.roll_dice(ctx, 95, 5, bet or localutils.CurrencyAmount())
コード例 #2
0
    async def slots(self,
                    ctx: utils.Context,
                    *,
                    bet: localutils.BetAmount = None):
        """
        Runs a slot machine.
        """

        # See where our reels ended up
        bet = bet or localutils.CurrencyAmount()
        slot_indexes = [
            random.randint(0,
                           len(self.SLOT_ITEMS[0]) - 1),
            random.randint(0,
                           len(self.SLOT_ITEMS[1]) - 1),
            random.randint(0,
                           len(self.SLOT_ITEMS[2]) - 1),
        ]

        # See what our output for the reels is
        untransposed_lines = []
        for reel_index in range(0, 3):
            line = []
            for offset_index in range(-1, 2):
                try:
                    line.append(
                        self.SLOT_ITEMS[reel_index][slot_indexes[reel_index] +
                                                    offset_index])
                except IndexError:
                    line.append(self.SLOT_ITEMS[reel_index][-1])
            untransposed_lines.append(line)

        # Transpose the fruit
        transposed_lines = []
        for reel_index in range(0, 3):
            line = []
            for fruit_index in range(0, 3):
                line.append(untransposed_lines[fruit_index][reel_index])
            transposed_lines.append(line)

        # Join together the lines
        joined_lines = []
        for line in transposed_lines:
            joined_lines.append("".join(line))

        # Work out what to output
        multiplier = self.get_slots_score(joined_lines[1])
        embed = utils.Embed(use_random_colour=True).add_field(
            "Roll", "\n".join(joined_lines))
        if bet.amount:
            if multiplier == 0:
                embed.add_field(
                    "Result",
                    f"You lost, removed **{bet.amount:,}** from your account :c",
                    inline=False)
            elif multiplier > 0:
                embed.add_field(
                    "Result",
                    f"You won! Added **{bet.amount * multiplier:,}** to your account! :D",
                    inline=False)
        else:
            if multiplier == 0:
                embed.add_field("Result", "You lost :c", inline=False)
            elif multiplier > 0:
                embed.add_field("Result", "You won! :D", inline=False)
        self.bot.dispatch(
            "transaction", ctx.author, bet.currency,
            -bet.amount if multiplier == 0 else bet.amount * multiplier,
            "SLOTS", multiplier != 0)
        return await ctx.send(embed=embed)
コード例 #3
0
    async def baccurat(self,
                       ctx: utils.Context,
                       bet_location: str = None,
                       *,
                       bet: localutils.BetAmount = None):
        """
        Plays you a game of baccurat.
        """
        """
        If either the player or banker is dealt a total of eight or nine, both the player and banker stand.
        If the player's total is five or less, then the player will receive another card. Otherwise, the player will stand.
        If the player stands, then the banker hits on a total of 5 or less.
        The final betting option, a tie, pays out 8-to-1.
        """

        # Make sure they set a valid bet location
        bet_location = (bet_location or "-").lower()[0]
        if bet_location not in ["player", "dealer", "tie", "p", "d", "t"]:
            return await ctx.send(
                'Your bet location needs to be one of "player", "dealer", and "tie".'
            )

        # Create the deck and hands used for the game
        deck: BaccuratDeck = BaccuratDeck.create_deck(shuffle=True)
        dealer_hand: BaccuratHand = BaccuratHand(deck)
        dealer_hand.draw(2)
        user_hand: BaccuratHand = BaccuratHand(deck)
        user_hand.draw(2)
        bet = bet or localutils.CurrencyAmount()

        # Play the game
        while True:
            if dealer_hand.get_values() in [
                    8, 9
            ] or user_hand.get_values() in [8, 9]:
                break
            if user_hand.get_values() <= 5:
                user_hand.draw()
            else:
                if dealer_hand.get_values() <= 5:
                    dealer_hand.draw()
                else:
                    break

        # Make the initial embed
        embed = utils.Embed()
        embed.add_field(
            "Dealer Hand",
            f"{dealer_hand.display()} ({dealer_hand.get_values()})",
            inline=True)
        embed.add_field("Your Hand",
                        f"{user_hand.display()} ({user_hand.get_values()})",
                        inline=True)

        # See if they tied
        if dealer_hand.get_values() == user_hand.get_values():
            if bet_location == "t":
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  bet.amount * 8, "BACCURAT", True)
                embed.colour = discord.Colour.green()
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"You tied! Added **{bet.amount * 8:,}** to your account! :D",
                        inline=False)
                else:
                    embed.add_field("Result", "You tied!", inline=False)
                return await ctx.reply(embed=embed)
            else:
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  -bet.amount, "BACCURAT", False)
                embed.colour = discord.Colour.red()
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"You tied, removed **{bet.amount:,}** from your account! :c",
                        inline=False)
                else:
                    embed.add_field("Result", "You tied :c", inline=False)
                return await ctx.reply(embed=embed)

        # See if the dealer won
        if dealer_hand.get_values() > user_hand.get_values():
            if bet_location == "d":
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  bet.amount, "BACCURAT", True)
                embed.colour = discord.Colour.green()
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"The dealer won! Added **{bet.amount:,}** to your account! :D",
                        inline=False)
                else:
                    embed.add_field("Result", "The dealer won!", inline=False)
                return await ctx.reply(embed=embed)
            else:
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  -bet.amount, "BACCURAT", False)
                embed.colour = discord.Colour.red()
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"The dealer won, removed **{bet.amount:,}** from your account! :c",
                        inline=False)
                else:
                    embed.add_field("Result",
                                    "The dealer won :c",
                                    inline=False)
                return await ctx.reply(embed=embed)

        # See if the player won
        if dealer_hand.get_values() < user_hand.get_values():
            if bet_location == "p":
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  bet.amount, "BACCURAT", True)
                embed.colour = discord.Colour.green()
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"You won! Added **{bet.amount:,}** to your account! :D",
                        inline=False)
                else:
                    embed.add_field("Result", "You won!", inline=False)
                return await ctx.reply(embed=embed)
            else:
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  -bet.amount, "BACCURAT", False)
                embed.colour = discord.Colour.red()
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"You won, removed **{bet.amount:,}** from your account! :c",
                        inline=False)
                else:
                    embed.add_field("Result", "You won :c", inline=False)
                return await ctx.reply(embed=embed)
コード例 #4
0
    async def blackjack(self,
                        ctx: utils.Context,
                        *,
                        bet: localutils.BetAmount = None):
        """
        Lets you play a blackjack game against the bot.
        """

        # Create the deck and hands used for the game
        deck: localutils.Deck = localutils.Deck.create_deck(shuffle=True)
        dealer_hand: localutils.Hand = localutils.Hand(deck)
        dealer_hand.draw(2)
        user_hand: localutils.Hand = localutils.Hand(deck)
        user_hand.draw(2)
        valid_emojis = ["\N{HEAVY PLUS SIGN}", "\N{HEAVY CHECK MARK}"]
        bet = bet or localutils.CurrencyAmount()

        # Ask the user if they want to hit or stand
        message = None
        while True:

            # See if the user went bust
            if min(user_hand.get_values()) > 21:
                embed = utils.Embed(colour=discord.Colour.red())
                embed.add_field("Dealer Hand",
                                f"{dealer_hand.display(show_cards=1)} (??)",
                                inline=True)
                embed.add_field(
                    "Your Hand",
                    f"{user_hand.display()} ({user_hand.get_values()[-1]} - bust)",
                    inline=True)
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"You lost, removed **{bet.amount:,}** from your account :c",
                        inline=False)
                else:
                    embed.add_field("Result", "You lost :c", inline=False)
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  -bet.amount, "BLACKJACK", False)
                self.bot.loop.create_task(message.clear_reactions())
                return await message.edit(embed=embed)
            if max(user_hand.get_values(max_value=21)) == 21:
                break

            # Output the hands to be used
            embed = utils.Embed(colour=0xfffffe)
            embed.add_field("Dealer Hand",
                            f"{dealer_hand.display(show_cards=1)} (??)",
                            inline=True)
            embed.add_field(
                "Your Hand",
                f"{user_hand.display()} ({', '.join(user_hand.get_values(cast=str, max_value=21))})",
                inline=True)
            embed.set_footer(
                f"{valid_emojis[0]} Hit | {valid_emojis[1]} Stand")
            if message is None:
                message = await ctx.reply(embed=embed)
                for e in valid_emojis:
                    await message.add_reaction(e)
            else:
                await message.edit(embed=embed)

            # See what the user wants to do
            def check(payload):
                return all([
                    payload.user_id == ctx.author.id,
                    payload.message_id == message.id,
                    str(payload.emoji) in valid_emojis,
                ])

            done, pending = await asyncio.wait(
                [
                    self.bot.wait_for("raw_reaction_add", check=check),
                    self.bot.wait_for("raw_reaction_remove", check=check),
                ],
                timeout=120,
                return_when=asyncio.FIRST_COMPLETED,
            )
            if not done:
                for i in pending:
                    i.cancel()
                return await ctx.reply("Timed out waiting for your response.",
                                       ignore_error=True)

            # See if they want to stand
            done = done.pop().result()
            changed_emoji = str(done.emoji)
            if changed_emoji == valid_emojis[1]:
                break

            # See if they want to hit
            user_hand.draw()
            await asyncio.sleep(0.2)

        # Let's draw until we get higher than the user
        user_max_value = max(user_hand.get_values(max_value=21))
        user_has_won = None
        while True:
            try:
                max_dealer_value = max(dealer_hand.get_values(max_value=21))
                if max_dealer_value >= user_max_value:
                    user_has_won = False  # Dealer wins
                    break
            except ValueError:
                user_has_won = True  # Dealer went bust
                break
            dealer_hand.draw()

        # Make sure we got a value and I didn't mess anything up
        if user_has_won is None:
            raise Exception("Failed to run the command properly.")

        # Don't error if the user got a blackjack
        if message:
            send_method = message.edit
            self.bot.loop.create_task(message.clear_reactions())
        else:
            send_method = ctx.send

        # Output something for the user winning
        if user_has_won:
            embed = utils.Embed(colour=discord.Colour.green())
            if min(dealer_hand.get_values()) > 21:
                embed.add_field(
                    "Dealer Hand",
                    f"{dealer_hand.display()} ({dealer_hand.get_values()[-1]} - bust)",
                    inline=True)
            else:
                embed.add_field(
                    "Dealer Hand",
                    f"{dealer_hand.display()} ({dealer_hand.get_values()[0]})",
                    inline=True)
            embed.add_field(
                "Your Hand",
                f"{user_hand.display()} ({user_hand.get_values(max_value=21)[0]})",
                inline=True)
            if bet.amount:
                embed.add_field(
                    "Result",
                    f"You won! Added **{bet.amount:,}** to your account! :D",
                    inline=False)
            else:
                embed.add_field("Result", "You won! :D", inline=False)
            self.bot.dispatch("transaction", ctx.author, bet.currency,
                              bet.amount, "BLACKJACK", True)
            return await send_method(embed=embed)

        # Output something for the dealer winning
        embed = utils.Embed(colour=discord.Colour.red())
        embed.add_field(
            "Dealer Hand",
            f"{dealer_hand.display()} ({dealer_hand.get_values(max_value=21)[0]})",
            inline=True)
        embed.add_field(
            "Your Hand",
            f"{user_hand.display()} ({user_hand.get_values(max_value=21)[0]})",
            inline=True)
        if bet.amount:
            embed.add_field(
                "Result",
                f"You lost, removed **{bet.amount:,}** from your account :c",
                inline=False)
        else:
            embed.add_field("Result", "You lost :c", inline=False)
        self.bot.dispatch("transaction", ctx.author, bet.currency, -bet.amount,
                          "BLACKJACK", False)
        return await send_method(embed=embed)
コード例 #5
0
    async def blackjack(self,
                        ctx: utils.Context,
                        *,
                        bet: localutils.BetAmount = None):
        """
        Lets you play a blackjack game against the bot.
        """

        # Create the deck and hands used for the game
        deck: localutils.Deck = localutils.Deck.create_deck(shuffle=True)
        dealer_hand: localutils.Hand = localutils.Hand(deck)
        dealer_hand.draw(2)
        user_hand: localutils.Hand = localutils.Hand(deck)
        user_hand.draw(2)
        bet = bet or localutils.CurrencyAmount()
        components = utils.MessageComponents(
            utils.ActionRow(
                utils.Button("HIT", "HIT"),
                utils.Button("STAND", "STAND"),
            ), )

        # Ask the user if they want to hit or stand
        message = None
        while True:

            # See if the user went bust
            if min(user_hand.get_values()) > 21:
                embed = utils.Embed(colour=discord.Colour.red())
                embed.add_field("Dealer Hand",
                                f"{dealer_hand.display(show_cards=1)} (??)",
                                inline=True)
                embed.add_field(
                    "Your Hand",
                    f"{user_hand.display()} ({user_hand.get_values()[-1]} - bust)",
                    inline=True)
                if bet.amount:
                    embed.add_field(
                        "Result",
                        f"You lost, removed **{bet.amount:,}** from your account :c",
                        inline=False)
                else:
                    embed.add_field("Result", "You lost :c", inline=False)
                self.bot.dispatch("transaction", ctx.author, bet.currency,
                                  -bet.amount, "BLACKJACK", False)
                return await message.edit(
                    embed=embed, components=components.disable_components())
            if max(user_hand.get_values(max_value=21)) == 21:
                break

            # Output the hands to be used
            embed = utils.Embed(colour=0xfffffe)
            embed.add_field("Dealer Hand",
                            f"{dealer_hand.display(show_cards=1)} (??)",
                            inline=True)
            embed.add_field(
                "Your Hand",
                f"{user_hand.display()} ({', '.join(user_hand.get_values(cast=str, max_value=21))})",
                inline=True)
            if message is None:
                message = await ctx.send(embed=embed, components=components)
            else:
                await message.edit(embed=embed)

            # See what the user wants to do
            def check(payload):
                return all([
                    payload.message.id == message.id,
                    payload.user.id == ctx.author.id,
                ])

            try:
                payload = await self.bot.wait_for("component_interaction",
                                                  check=check,
                                                  timeout=120)
                await payload.ack()
            except asyncio.TimeoutError:
                return await ctx.send("Timed out waiting for your response.")

            # See if they want to stand
            clicked = payload.component
            if clicked.custom_id == "STAND":
                break

            # See if they want to hit
            user_hand.draw()
            await asyncio.sleep(0.2)

        # Let's draw until we get higher than the user
        user_max_value = max(user_hand.get_values(max_value=21))
        user_has_won = None
        while True:
            try:
                max_dealer_value = max(dealer_hand.get_values(max_value=21))
                if max_dealer_value >= user_max_value:
                    user_has_won = False  # Dealer wins
                    break
            except ValueError:
                user_has_won = True  # Dealer went bust
                break
            dealer_hand.draw()

        # Make sure we got a value and I didn't mess anything up
        if user_has_won is None:
            raise Exception("Failed to run the command properly.")

        # Don't error if the user got a blackjack
        if message:
            send_method = message.edit
            components = components.disable_components()
        else:
            send_method = ctx.send
            components = None

        # Output something for the user winning
        if user_has_won:
            embed = utils.Embed(colour=discord.Colour.green())
            if min(dealer_hand.get_values()) > 21:
                embed.add_field(
                    "Dealer Hand",
                    f"{dealer_hand.display()} ({dealer_hand.get_values()[-1]} - bust)",
                    inline=True)
            else:
                embed.add_field(
                    "Dealer Hand",
                    f"{dealer_hand.display()} ({dealer_hand.get_values()[0]})",
                    inline=True)
            embed.add_field(
                "Your Hand",
                f"{user_hand.display()} ({user_hand.get_values(max_value=21)[0]})",
                inline=True)
            if bet.amount:
                embed.add_field(
                    "Result",
                    f"You won! Added **{bet.amount:,}** to your account! :D",
                    inline=False)
            else:
                embed.add_field("Result", "You won! :D", inline=False)
            self.bot.dispatch("transaction", ctx.author, bet.currency,
                              bet.amount, "BLACKJACK", True)
            return await send_method(embed=embed, components=components)

        # Output something for the dealer winning
        embed = utils.Embed(colour=discord.Colour.red())
        embed.add_field(
            "Dealer Hand",
            f"{dealer_hand.display()} ({dealer_hand.get_values(max_value=21)[0]})",
            inline=True)
        embed.add_field(
            "Your Hand",
            f"{user_hand.display()} ({user_hand.get_values(max_value=21)[0]})",
            inline=True)
        if max_dealer_value > user_max_value:
            if bet.amount:
                embed.add_field(
                    "Result",
                    f"You lost, removed **{bet.amount:,}** from your account :c",
                    inline=False)
            else:
                embed.add_field("Result", "You lost :c", inline=False)
        else:
            if bet.amount:
                embed.add_field(
                    "Result",
                    f"You tied, returned **{bet.amount:,}** to your account :<",
                    inline=False)
            else:
                embed.add_field("Result", "You tied :<", inline=False)
        if max_dealer_value > user_max_value:
            self.bot.dispatch("transaction", ctx.author, bet.currency,
                              -bet.amount, "BLACKJACK", False)
        else:
            self.bot.dispatch("transaction", ctx.author, bet.currency, 0,
                              "BLACKJACK", False)
        return await send_method(embed=embed, components=components)