Discord.py sending multiple embeds

58 Views Asked by At

I'm trying to make a lottery command for my Discord bot. I want to enable the users to buy more than 1 lottery ticket at a time. However, Discord doesn't want to send more than 1 embeds per slash command. Can someone help me?

My current code:

@bot.tree.command(name="lottery", description="Every 1 in 100 lottery wins 1,000,000 chips. Lottery costs 10,000 chips.")
@app_commands.describe(amount="Enter the amount of lotteries you'd like to get (max. is set to 5).")
async def lottery(interaction: discord.Interaction, amount:int=1):

    member = interaction.user 


    #Errors
    db = sqlite3.connect("main.sqlite")
    cursor = db.cursor()
    cursor.execute(f"SELECT wallet FROM main WHERE user_id = {member.id}")
    wallet = cursor.fetchone()

    try:
        wallet = wallet[0]
    except:
        wallet = wallet
    
    if amount > 5:
        error2=discord.Embed(description="**ERROR:** You can't buy more than 5 lotteries per command!", color=discord.Color.red())
        return await interaction.response.send_message(embed=error2)
    
    if wallet < 10000*amount:
        error2=discord.Embed(description="**ERROR:** You don't have enough <:chip:1167488856203984957> to play lottery!", color=discord.Color.red())
        return await interaction.response.send_message(embed=error2)
    

    #Game Logic
    winnumber = 55
    win = 1000000
    formatted_win = "{:,}".format(win) #Format win with commas
    formatted_new_balance = "{:,}".format(wallet + win) #Format wallet + payout with commas
    formatted_new_balance2 = "{:,}".format(wallet - 10000) #Format wallet + payout with commas

    botchoice = random.randint(1,101)

    if wallet >= 10000:
        cursor.execute("UPDATE main SET wallet = ? WHERE user_id = ?", (wallet - 10000, member.id))
        db.commit()

        if botchoice == winnumber:
            cursor.execute("UPDATE main SET wallet = ? WHERE user_id = ?", (wallet + win, member.id))
            db.commit()

            lottery = discord.Embed(description=f"You're insane, you just won **{formatted_win}** <:chip:1167488856203984957>\nNew balance: **{formatted_new_balance}** <:chip:1167488856203984957>", color=discord.Color.green())
            lottery.set_author(name=f"{member.name}'s Lottery ", icon_url = member.avatar)
            lottery.set_thumbnail(url="url")
            await interaction.response.send_message(embed=lottery)
        else:
            lottery = discord.Embed(description=f"Sadly you just lost **10,000** <:chip:1167488856203984957>\nNew balance: **{formatted_new_balance2}** <:chip:1167488856203984957>", color=discord.Color.red())
            lottery.set_author(name=f"{member.name}'s Lottery ", icon_url = member.avatar)
            lottery.set_thumbnail(url="url")
            await interaction.response.send_message(embed=lottery)
        
    cursor.close()
    db.close()

I've tried putting it Game Logic into a loop, but nothing would happen.

for _ in range(amount):

Also tried await interaction.followup.send(embed=lottery), but that would print an The application did not respond error.

1

There are 1 best solutions below

0
On BEST ANSWER

You could use a for loop to create embeds for all the outcomes and append them to a list.

outcomes = []

for i in range(amount):

    botchoice = random.randint(1,101)

    if botchoice == winnumber:
        cursor.execute("UPDATE main SET wallet = ? WHERE user_id = ?", (wallet + win, member.id))
        db.commit()

        lottery = discord.Embed(description=f"You're insane, you just won **{formatted_win}** <:chip:1167488856203984957>\nNew balance: **{formatted_new_balance}** <:chip:1167488856203984957>", color=discord.Color.green())
        lottery.set_author(name=f"{member.name}'s Lottery ", icon_url = member.avatar)
        lottery.set_thumbnail(url="url")
            
        outcomes.append(lottery)
    else:
        lottery = discord.Embed(description=f"Sadly you just lost **10,000** <:chip:1167488856203984957>\nNew balance: **{formatted_new_balance2}** <:chip:1167488856203984957>", color=discord.Color.red())
        lottery.set_author(name=f"{member.name}'s Lottery ", icon_url = member.avatar)
        lottery.set_thumbnail(url="url")

        outcomes.append(lottery)

    await interaction.response.send_message(embeds=outcomes)
            

This will randomise the bot's number every loop and only send the embeds once all lottery outcomes have been considered.

This way, all embeds are sent in one message rather than multiple embeds sent separately.