Can't show image error in case it doesn't exist on discord from python
Question:
I have created a discord bot that shows the skins from an imgur url but I have a small problem and in the event that the image does not exist it shows me an error within discord, but it is giving me an error in the console
This is the error that I get when executing the command on discord:
The code that I am currently using is:
slash = SlashCommand(bot, sync_commands=True)
@slash.slash(
name="vistaprevia", description="Escribe él ID de la url de imgur",
options=[
create_option(
name="id_imgur",
description="Escribe él ID de la url de imgur",
option_type=3,
required=True
),
])
async def vistaprevia(ctx:SlashContext, id_imgur:str):
await ctx.defer()
url = f"https://jose89fcb.es/SkinMinecraftVistaPreviaDiscord/skin.php?skin={id_imgur}"
r = requests.get(url)
if r.status_code ==200:
imagen = Image.open(io.BytesIO(requests.get(url).content))
with io.BytesIO() as imagen_binary:
imagen.save(imagen_binary, 'PNG')
imagen_binary.seek(0)
await ctx.send(file=discord.File(fp=imagen_binary, filename=f"{id_imgur}.png"))
else:
await ctx.send("Error de imagen!")
Where could the error be?
Thank you very much in advance!
Answers:
if r.status_code ==200:
try:
imagen = Image.open(io.BytesIO(requests.get(url).content))
except PIL.UnidentifiedImageError:
await ctx.send("Error de imagen!")
I have created a discord bot that shows the skins from an imgur url but I have a small problem and in the event that the image does not exist it shows me an error within discord, but it is giving me an error in the console
This is the error that I get when executing the command on discord:
The code that I am currently using is:
slash = SlashCommand(bot, sync_commands=True)
@slash.slash(
name="vistaprevia", description="Escribe él ID de la url de imgur",
options=[
create_option(
name="id_imgur",
description="Escribe él ID de la url de imgur",
option_type=3,
required=True
),
])
async def vistaprevia(ctx:SlashContext, id_imgur:str):
await ctx.defer()
url = f"https://jose89fcb.es/SkinMinecraftVistaPreviaDiscord/skin.php?skin={id_imgur}"
r = requests.get(url)
if r.status_code ==200:
imagen = Image.open(io.BytesIO(requests.get(url).content))
with io.BytesIO() as imagen_binary:
imagen.save(imagen_binary, 'PNG')
imagen_binary.seek(0)
await ctx.send(file=discord.File(fp=imagen_binary, filename=f"{id_imgur}.png"))
else:
await ctx.send("Error de imagen!")
Where could the error be?
Thank you very much in advance!
if r.status_code ==200:
try:
imagen = Image.open(io.BytesIO(requests.get(url).content))
except PIL.UnidentifiedImageError:
await ctx.send("Error de imagen!")