简体   繁体   中英

How do you send more than one embed per Interaction in Discord.py?

I want to make a Discord interaction that sends a picture for as often as you say in 'howmany', but with my current code it send 1 embed with a picture and the rest without one. How to fix this?

@tree.command(name='embed', description='embed')
async def embed(interaction: discord.Interaction, seeable: bool, howmany: typing.Optional[int]):
                    embed = discord.Embed(title="Here is a title", color=0xff8c00)
                    file = discord.File(f"[file path]", filename="image.png")
                    embed.set_image(url="attachment://image.png")
                    if seeable == True:
                        await interaction.response.send_message(file=file, embed=embed)
                        if howmany >= 2:
                            for i in range(howmany-1):
                                await interaction.followup.send(file=file, embed=embed)
                    if seeable == False:
                        await interaction.response.send_message(file=file, embed=embed, ephemeral=True)
                        if howmany >= 2:
                            for i in range(howmany-1):
                                await interaction.followup.send(file=file, embed=embed, ephemeral=True)

it already works perfectly fine without the interaction, just like the old prefix system. If you remove all the # it will upload the files from a file path. Otherwise it will show a Picture from a website:

if message.content.startswith('+image'):
    count = 0
    args = message.content.split(' ')
    if len(args) < 2:
        count = 1
    else:
        if args[1].isdigit():
            count = int(args[1])
        else:
            await message.channel.send("How many should it be?")
    for i in range(count):
        random = random.randint(1,68)
        embed = discord.Embed(title="Title", color=0xff8c00)
        embed.set_image(url=f"https://www.awebsite/pic{random}.png")
        #file = discord.File(f"C:a/file/path/pic{random}.png", filename="image.png")
        #embed.set_image(url="attachment://image.png")
        #await message.channel.send(file=file, embed=embed)
        await message.channel.send(embed=embed)

Have a close look at the docs for send_message : https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=send_message#discord.InteractionResponse.send_message

Parameters:

  • embeds (List[Embed]) – A list of embeds to send with the content. Maximum of 10. This cannot be mixed with the embed parameter.

  • embed (Embed) – The rich embed for the content to send. This cannot be mixed with embeds parameter.

In other words: if you want to send multiple embeds, use the embeds -kwarg instead of embed , and pass in a list of embeds you want to send.

...send_message(..., embeds=[embed1, embed2, embed3])

You can do the same thing when sending regular messages (not replying to interactions).

Sadly the community hasn't responded yet so I fixed the bug myself. I know it's not the most beautiful code so if you have a prettier way, please let me know.

@tree.command(name='embed', description='embed')
async def embed(interaction: discord.Interaction, seeable: bool, menge: typing.Optional[int]):
        if seeable == True:
            zufall = random.randint(1, 68)
            embed = discord.Embed(title="embed", color=0xff8c00)
            file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
            embed.set_image(url="attachment://image.png")
            await interaction.response.send_message(file=file, embed=embed)
            
            if menge >= 2:
                    for i in range(menge - 1):
                    zufall = random.randint(1, 68)
                    embed = discord.Embed(title="embed", color=0xff8c00)
                    file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
                    embed.set_image(url="attachment://image.png")
                    await interaction.response.send_message(file=file, embed=embed)

        if seeable == False:
            zufall = random.randint(1, 68)
            embed = discord.Embed(title="embed", color=0xff8c00)
            file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
            embed.set_image(url="attachment://image.png")
            await interaction.response.send_message(file=file, embed=embed, ephemeral=True)
            #await interaction.response.send_message(embed=embed)

            if menge >= 2:
                    for i in range(menge - 1):
                    zufall = random.randint(1, 68)
                    embed = discord.Embed(title="embed", color=0xff8c00)
                    file = discord.File(f"C:/file/path/pic{zufall}.png", filename="image.png")
                    embed.set_image(url="attachment://image.png")
                    await interaction.response.send_message(file=file, embed=embed, ephemeral=True)

I hope I'll help someone else with that. Ihr seids soiche Heisln.

You can remove the "if seeable" condition and just put seeable to ephemeral like this:

    @tree.command(name='embed', description='embed')
    async def embed(interaction: discord.Interaction, seeable: bool, menge: typing.Optional[int]):
            embed = discord.Embed(title="Here is a title", color=0xff8c00)
            file = discord.File(f"[file path]", filename="image.png")
            embed.set_image(url="attachment://image.png")
    
            await interaction.response.send_message(file=file, embed=embed, ephemeral=seeable)
            if menge >= 2:
                for i in range(menge - 1):
                    await interaction.followup.send(file=file, embed=embed, ephemeral=seeable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM