Search code examples
pythonimagediscordcommandecho

Discord bot python echo command image


I have a command that bot echo my messages, but if I paste the image, bot has an error and I dont know how to fix it. I want my bot will be able to use the echo command and insert the photo I pasted.

I tried to fix this, but i don't know how to do it with images, there's no tutorials with echo command and images

that's my code:

@bot.command(aliases=['echo'])
@commands.is_owner()
async def e(ctx, *, message=None):
    message = message or "Napisz co chcesz powtórzyć"
    await ctx.message.delete()
    await ctx.send(message)

Solution

  • You need to include some more logic within your command.

    What you are looking for is either a new parameter, called image or some checking within your code.

    Option 1: Make a new parameter called image: discord.Attachment = None

    This way it is optional, and you don't need to include an image in every command. You can then check for the image within your code by using if image and include the logic to send it.

    Option 2: Include some logic within your code and just check if the message contains an image.

    if ctx.message.attachments: # If the message contains an image
        attachment = ctx.message.attachments[0]
        await ctx.message.delete()
        await ctx.send(content=message, file=discord.File(attachment.url))
    else:
       await ctx.message.delete()
       await ctx.send(content=message)