Home > Blockchain >  Discord.py Sending attachments for 'say' command
Discord.py Sending attachments for 'say' command

Time:10-19

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?

CodePudding user response:

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)

CodePudding user response:

Thanks to @Shunya for the help. While trying their method, i found a simpler (in my opinion) way to do it:

async def say(self, ctx, *, message):
  if ctx.message.attachments:
     await ctx.send(content=message, files=[await f.to_file() for f in ctx.message.attachments])
  await ctx.send(message)
  • Related