Search code examples
pythondiscord.pyembed

How would I code an embed that gives a random image from a set of images I have the URLs of? (Discord.py)


Here's what it looks like right now. It doesn't work tho, as when I type the command in (.testyi), my bot just comes up with a thin blank embed.

async def testyi(ctx):
    items = [
    discord.Embed(image = "thaturlwhatever.jpeg", url = "thaturlwhatever.jpg")
    discord.Embed(image = "thatotherurlwhatever.jpeg", url = "thatotherurlwhatever.jpg")]
    randomitem = random.choice(items)
    await ctx.send(embed=randomitem)


Solution

  • you can do like this:

    @client.command() 
    async def hell(ctx):
        items = ['url_0','url_1','url_2']
        
        embed = discord.Embed()
        embed.set_image(url=random.choice(items))
        await ctx.send(embed=embed)
    

    Place all the urls in items list then use random.choice(items) to get random url!
    the embed.set_image function has no argument called name so we can't use name in this method so i just skipped the name!
    and you can't directly add image like this discord.Embed(image='url') you need to use embed.set_image(url='url') [set_image: document]