I'm trying to create a blackjack bot in Discord but my field for the actual result of the game isn't updating
I've tried adding print statements to find the error but that was no help. This is what I recieved from my print statements
Embed: Your Cards: 17 3, 3, 1 Dealer's Cards: 24 7, 7, 10 Result In progress
Print statements: Reaction received Player hits Reaction removed Reaction received Player stands
This is where I put the actual prints in my code:
if dealer_total >= 17:
if dealer_total > 21 or (player_total <= 21 and player_total > dealer_total):
await handle_win(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
print("Player won. Game over.")
elif player_total < dealer_total:
await handle_loss(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
print("Dealer won. Game over.")
else:
await handle_tie(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
print("It's a tie. Game over.")
break
Here is my actual code I have currently:
@bot.command(name="blackjack", aliases=["bj"])
async def blackjack_command(ctx, bet: int):
author_id = str(ctx.author.id)
accounts = load_accounts()
if author_id not in accounts:
await ctx.send("You don't have an account. Use the ,open command to create one.")
return
if accounts[author_id]['balance'] < bet:
await ctx.send("You don't have enough balance to place this bet.")
return
accounts[author_id]['balance'] -= bet
save_accounts(accounts)
player_hand = [draw_card(), draw_card()]
dealer_hand = [draw_card(), draw_card()]
player_total = calculate_total(player_hand)
dealer_total = calculate_total(dealer_hand[1:])
embed = discord.Embed(title="", color=0x232428)
embed.add_field(name=f"Your Cards: {player_total}", value=format_hand(player_hand), inline=False)
embed.add_field(name=f"Dealer's Cards: {dealer_total}", value=f"{format_hand(dealer_hand[1:])} ??? " , inline=False)
embed.add_field(name="Result", value="In progress", inline=False)
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
embed.set_thumbnail(url="https://i.imgur.com/JBPnaZz.png")
message = await ctx.send(embed=embed)
if player_total == 21:
await handle_win(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
return
await message.add_reaction("✅")
await message.add_reaction("❌")
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ["✅", "❌"]
while True:
try:
reaction, user = await bot.wait_for("reaction_add", timeout=60, check=check)
if str(reaction.emoji) == "✅":
player_hand.append(draw_card())
player_total = calculate_total(player_hand)
embed.set_field_at(0, name=f"Your Cards: {player_total}", value=format_hand(player_hand), inline=False)
await message.edit(embed=embed)
if player_total > 21:
await handle_loss(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
return
else:
while dealer_total < 17:
dealer_hand.append(draw_card())
dealer_total = calculate_total(dealer_hand)
embed.set_field_at(1, name=f"Dealer's Cards: {dealer_total}", value=format_hand(dealer_hand), inline=False)
await message.edit(embed=embed)
if dealer_total >= 17:
if dealer_total > 21 or player_total > dealer_total:
await handle_win(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
return
elif player_total < dealer_total:
await handle_loss(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
return
else:
await handle_tie(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total)
return
await message.remove_reaction(reaction, user)
except asyncio.TimeoutError:
await message.delete()
break
async def handle_win(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total):
accounts[author_id]['balance'] += bet * 2
save_accounts(accounts)
embed = discord.Embed(title="Blackjack", color=0x00FF00)
embed.add_field(name=f"Your Cards: {format_total(player_hand)}", value=format_hand(player_hand), inline=False)
embed.add_field(name="Dealer's Hand", value=format_hand(dealer_hand), inline=False)
embed.add_field(name="Result", value=f"You won! New Balance: {accounts[author_id]['balance']}", inline=False)
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
embed.set_thumbnail(url="https://i.imgur.com/JBPnaZz.png")
await message.edit(embed=embed)
async def handle_loss(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total):
save_accounts(accounts)
embed = discord.Embed(title="Blackjack", color=0xFF0000)
embed.add_field(name=f"Your Cards: {format_total(player_hand)}", value=format_hand(player_hand), inline=False)
embed.add_field(name="Dealer's Hand", value=format_hand(dealer_hand), inline=False)
embed.add_field(name="Result", value=f"You lost! New Balance: {accounts[author_id]['balance']}", inline=False)
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
embed.set_thumbnail(url="https://i.imgur.com/JBPnaZz.png")
await message.edit(embed=embed)
async def handle_tie(ctx, accounts, author_id, bet, message, dealer_hand, dealer_total):
accounts[author_id]['balance'] += bet
save_accounts(accounts)
embed = discord.Embed(title="Blackjack", color=0xFFFF00)
embed.add_field(name=f"Your Cards: {format_total(player_hand)}", value=format_hand(player_hand), inline=False)
embed.add_field(name="Dealer's Hand", value=format_hand(dealer_hand), inline=False)
embed.add_field(name="Result", value=f"It's a tie! New Balance: {accounts[author_id]['balance']}", inline=False)
embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar.url)
embed.set_thumbnail(url="https://i.imgur.com/JBPnaZz.png")
await message.edit(embed=embed)
def draw_card():
return random.randint(1, 10)
def calculate_total(hand):
total = sum(hand)
if total <= 11 and 1 in hand:
total += 10
return total
def format_hand(hand):
return ", ".join(str(card) for card in hand)