-1

I would like to know if there's a way to download a pdf file from URL and send it right away as an attachment to discord change.?

urls = [f'https://documents.pse.com.ph/market_report/{monthf}%20{dayf},%20{year}-EOD.pdf']
for eodfiles in urls:
    sendAttachement = requests.get(eodfiles)
    if sendAttachement.status_code == 200:
           
      #await ctx.send(file)
      await ctx.send(url=discord.File(file, filename="EOD.pdf"))
    else:
      await ctx.send("No file available")
Shunya
  • 2,344
  • 4
  • 16
  • 28
awt
  • 3
  • 1
  • Welcome to Stack Overflow, could you please explain what is the problem you're finding with the code you've attached? If you see an error message or an unexpected behavior, it should be in your question. See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Marc Sances Oct 21 '21 at 10:06

1 Answers1

0

As the documentation shows, Context implements the send() method as:

send(content=None, *, tts=False, embed=None, file=None, files=None, delete_after=None, nonce=None, allowed_mentions=None, reference=None, mention_author=None)

Notice the file parameter. If we look into its description we see that it indeed does what you're looking to do, and it accepts an argument of type File. If you manage your downloaded pdf into Discord.py's File format, you can send it along (or you can put multiple such files in a list and send it with the files parameter).

Looking into the documentation should always be the first thing you try when you need to learn more about a library.

The minimal way to make a File object is to pass a file pointer (the thing you get from open()ing a file) or a file name, if you downloaded the pdf already. So you should take the result of requests.get(eodfiles) and save them to file names you can then use like

file = discord.File("EOD.pdf")
# You only need to customise the `filename` parameter if you want the name to appear different

and then call

await ctx.send(file=file)

The details on how to save a file downloaded through requests depends on requests, and you can easily research that on their documentation, or see this question that seems to address the problem.

theberzi
  • 2,142
  • 3
  • 20
  • 34
  • so sorry just a newbie to python. I was just thinking if there's a way of getting the URL that is a pdf and directly attaching it to discord without saving it to my local. – awt Oct 21 '21 at 14:59
  • @awt Since discord.File requires a path or a file pointer, I don't think there's any way around it. What you can do is temporarily save the file locally, send it, and then delete it. At that point it will have been uploaded to Discord as an attachment, so your copy won't matter anymore. – theberzi Oct 21 '21 at 17:06