Skip to content
Advertisement

Discord.py Sending attachments for ‘say’ command

I have a ‘say’ command for my bot where it repeats what the user writes in the command message:

  @commands.command(name = "say",
                    aliases = ['s'],
                    brief = "Repeats <message>",
                    help = "Repeats <message> passed in after the command."
                    )
  @commands.cooldown(1, cmd_cd, commands.BucketType.user)
  async def say(self, ctx, *, message):
      if ctx.message.attachments:
          await ctx.send(content=message, files=ctx.message.attachments)
      await ctx.send(message)

I want it to also include the attachments if the user included any, but i can’t seem to get it to work. I keep getting the error:

Command raised an exception: InvalidArgument: files parameter must be a list of File

What am i doing wrong? In the discord.py docs it says that the ctx.message.attachments attribute returns a list, so why am I getting this error? I just want it to send the attachments the exact same way as the user sends it. Is it possible to do this without using embed?

Advertisement

Answer

The error is telling you that it is expecting a List of variables of type File, but attachments retrieves a list of type attachment.

If you check the docs for attachment, you can see that it can be easily converted to File using the method to_file().

So for your function to work you need to pass to files a new list containing the Files:

async def say(self, ctx, *, message):
  if ctx.message.attachments:
     new_list = list(map(lambda x:x.to_file(), ctx.message.attachments))
     await ctx.send(content=message, files=new_list)
  await ctx.send(message)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement