Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

discord - How do I send random embed images with a python bot

Here is what I used:

@client.command()
async def testcode(ctx):
  mum = random.choice(test.links)
  
  embed = discord.Embed(colour=0xfff300, timestamp=datetime.datetime.utcnow())
  embed.set_image(url=mum)
  embed.set_footer(text=f"Requested by: {ctx.author.name}")
  
  await ctx.send(embed=embed)

I was using a tutorial on YouTube and copied the code but it still didn't work, I put up a separate file to get the links from but it still doesn't work, I'm trying to embed them too so it's a little more complicated, but I need help nonetheless.

question from:https://stackoverflow.com/questions/65865664/how-do-i-send-random-embed-images-with-a-python-bot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A simple and easy way of doing it, it will randomly choose a image link out of the "images" array and display it as an image url in the embed.

images = ['https://i.redd.it/123123.jpg', 'etc', 'image link', 'etc', 'image link', 'etc']

@client.command()
async def image(ctx):
    embed = discord.Embed(
    title="Random Image",
    colour=discord.Colour(0x7289DA),
    description=f"Random Image",
    timestamp=datetime.datetime.utcfromtimestamp(1580842764)
    )
    embed.set_image(url=(random.choice(images)))


    await ctx.send(embed = embed)

Getting more advanced, you can use api's or json file which allow you to take images from other resources online such as:

embed = discord.Embed(title="r/dankmemes", description="https://www.reddit.com")
    async with aiohttp.ClientSession() as cs:
                async with cs.get(
                    'https://www.reddit.com/r/dankmemes/new.json?sort=hot') as r:
                        res = await r.json()
                        embed.set_image(url=res['data']['children'][random.randint(0, 25)]
                                        ['data']['url'])
                        await ctx.send(embed=embed)

But leave that up to you, hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...