0

I've been thinking of making a Blackjack game in my discord bot, but I've hit a roadblock.

I obviously have the game which is summoned with the command .blackjack, and it works fine in generating the random values and sending the messages. However, I don't know how to make it so the player is able to say hit or stand after the message with the cards dealt is sent, for example.

@client.command()
async def blackjack(ctx):
    # (insert all random number gens, etc. here)
    
    await ctx.send(f"{dface1}{dsuit1} ({dvalue1}), {dface2}{dsuit2} ({dvalue2})")
    await ctx.send(f"(Dealer Total: {dtotal})")
    
    await ctx.send(f"{pface1}{psuit1} ({pvalue1}), {pface2}{psuit2} ({pvalue2})")
    await ctx.send(f"(Total: {ptotal})")

Now what? What do I do to run my next part of the code, which is whether or not the player hit or stands, the dealer hitting and standing, etc.

2
  • check this one line no 76 Commented Dec 15, 2020 at 15:27
  • 1
    you mean subcommands? Commented Dec 15, 2020 at 15:33

2 Answers 2

1

I don't really know how to play Blackjack, so I'm afraid I won't be able to give you a full answer to your question. However I will say how you can achieve what you want. There are two ways you can go about doing this in my opinion.

Method 1

Waiting for the user to react to your bot's message

For this, you have to use:

reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)

For example, say you are waiting for 🇦 or 🅱️ from the user (This can mean hit and stand respectively). The code would look something like this:

@client.command()
async def start(ctx):
    def check(reaction, user):
        return (user == ctx.author) and (str(reaction.emoji) == '🇦' or str(reaction.emoji) == '🅱️')

    async def sendMessage(msg):
        message = await ctx.send(msg)
        await message.add_reaction('🇦')
        await message.add_reaction('🅱️')

        try:
            reaction, user = await client.wait_for('reaction_add', timeout = 60.0, check = check)
        except:
            await message.clear_reactions()
            await ctx.send('No reaction received.')
        else:
            await message.clear_reactions()
            return reaction

        return 0

    reaction = str(await sendMessage('This is my message'))

This is a simple code to check if the user reacts with 🇦 or 🅱️. You'll have to add more conditions and loops to get what you desire.

Method 2

Waiting for the user to send a message

For this, you have to use:

msg = await client.wait_for('message', check = check, timeout = 60.0)

You'll have to then check if msg equals hit or stand or some short form like h or s. Also be sure to write a check(author) function that is called inside the client.wait_for() function (check = check) to check if the author is that same as the one that ran the command.

I hope you'll be able to come up with the code you are looking for after reading this answer.

Sign up to request clarification or add additional context in comments.

Comments

1

discord.py has built-in subcommand support, here's an example:

@commands.group(invoke_without_subcommand=True)
async def your_command_name(ctx):
    # Do something if there's not a subcommand invoked


@your_command_name.command()
async def subcommand_name(ctx, *args):
    # Do something

# To invoke
# {prefix}your_command_name subcommand_name some arguments here

Or you can simply wait for a message

@client.command()
async def blackjack(ctx):
    # ...
    def check(message):
        """Checks if the message author is the same as the one that invoked the 
        command, and if the user chose a valid option"""
        return message.author == ctx.author and message.content.lower() in ['stand', 'hit']

    await ctx.send('Would you like to hit or stand?')

    message = await client.wait_for('message', check=check)
    await ctx.send(f"You chose to `{message.content}`")

# To invoke
# {prefix}blackjack
# Would you like to hit or stand?
# stand
# You chose to `stand`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.