Search code examples
pythondiscordbots

can't use send images in functions in discord bot in cogs


for some reason it seems that i cant send images with this

@commands.Cog.listener()
async def on_message(self, message):
     if str(message.content).lower == "pic" :
         await message.send("text", file=discord.File('picture.png'))

i thougth the problem was in the function on_message itself after i saw the documenttion i figured out that i can send it only with

channel.send(file=discord.file('pic.png'))

so i tried this

@commands.Cog.listener()
async def on_message(self, message):
     channel = self.client.get_channel("channel id")
     if str(message.content).lower == "pic" :
         await channel.send("text", file=discord.File('picture.png'))

didn't work as i thoght


Solution

  • First, when checking if the lowercase version of the message content is equal to "pic," you need to call the lower() method to convert it to lowercase. Currently, you're missing the parentheses, so the comparison won't work correctly. Modify the condition to str(message.content).lower() == "pic".

    Second, when sending a message with a file attachment, you should use the await message.channel.send() method instead of await message.send(). Adjust your code to use await message.channel.send("text", file=discord.File('picture.png')).

    Here is the Fixed version of your code !:

    @commands.Cog.listener()
    async def on_message(self, message):
        if str(message.content).lower() == "pic":
            await message.channel.send("text", file=discord.File('picture.png'))
    
    

    try it and tell me.