Dominik
Dominik

Reputation: 3602

How can I properly upload too long messages as a file?

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']}>"))

The following posts were viewed:

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

enter image description here

Upvotes: 2

Views: 5826

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

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)

Upvotes: 4

Related Questions