Home > Back-end >  In Python, how can I reload a page/website?
In Python, how can I reload a page/website?

Time:06-02

I'm trying to make a command that sends an image of a person that doesn't exist. The thing is it keeps sending the same image over and over again. I need it to send a different image, and for that to be sent the website needs to be reloaded.

This is my code so far.

@client.command()
async def tpdne(ctx):
    embed=discord.Embed(colour = discord.Colour.orange(), timestamp=ctx.message.created_at)
    embed.set_image(url=("https://thispersondoesnotexist.com/image"))
    await ctx.send(embed=embed)

CodePudding user response:

its not very clear if you are trying to refresh the page from the client or from the server side

it looks like you want this to happen on the server side, To help you we would need to know what is the framework you are using. django,falsk ...etc

the service of thispersondoesnotexist.com seems to be working on my side, so this might be helpful to you : How to download image using requests

CodePudding user response:

To achieve this you will need to force your bot to load image instead of discord client caching it and refusing to "refresh" the page. You can achieve that using aiohttp module and we gonna need io.BytesIO to convert them into discord.File object

so code would look something like this:

import discord
import aiohttp
from io import BytesIO

# ... 
@client.command()
async def tpdne(ctx):
    embed = discord.Embed(colour=discord.Colour.orange(), timestamp=ctx.message.created_at)
    async with aiohttp.ClientSession() as session:
        async with session.get("https://thispersondoesnotexist.com/image") as resp:
            image = BytesIO(await resp.read())
            image_name = 'person_image.png'
            file = discord.File(image, image_name)
    embed.set_image(url=f'attachment://{image_name}')
    await ctx.send(embed=embed, file=file)

It is pretty much same as in library docs FAQ: How do I upload an image?

  • Related