1

I was doing some research on Discord's capabilities today, and then I noticed that the "upload as file" option is given when a message is more than 2000 characters long. The number plays a role here, but my statement can also be wrong, the fact is that some things are posted only as a link and or then not sent at all if I raise >2000.

Here now the problem:

I am requesting data from Genius for a lyrics command. I already have some kind of override built in for when len(data["lyrics"]) is greater than 2000 "characters", that should be Discord's limit after all, otherwise the bot wouldn't be able to process the embed I am trying to send.

How can I manage that lyrics >2000 are sent as a file?

Relevant code:

if len(data["lyrics"]) > 2000:
    return await ctx.send(f"<{data['links']['genius']}>"))
  • This is the normal request, it works too, but it just throws out the link. I then tried to send this via discord.File with the corresponding piece of code above, does not work. (Something like file=discord.File(data) was tried too!)
  • Something like ctx.file.send does not work in this case either

The following posts were viewed:

What I want my bot to do if data["lyrics"] > 2000:

enter image description here

Dominik
  • 3,612
  • 2
  • 11
  • 44

1 Answers1

3

You can save the text into a io.StringIO buffer and pass that into the discord.File constructor:

from io import StringIO

text = data["lyrics"]
if len(text) > 2000:
    buffer = StringIO(text)
    f = discord.File(buffer, filename="lyrics.txt")
    await ctx.send(file=f)
Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39
  • Works perfectly, I can work with that. At the end I could have also split the embeds, but this is a short and sweet solution, did not know about StringIO to be honest, thanks! – Dominik Sep 23 '21 at 11:08