#Txt file messages

1 messages · Page 1 of 1 (latest)

chilly seal
#

file=discord.File()

#

No, read the docs to discord.File and the send function

chilly seal
#

so you want to send the content of the txt file, but not the file itself?

#

now I'm even more confused, so just so I get it right

  1. read a message
  2. store in file
  3. send content of file
    ?
#

so you want to send the file

#

ok

#

then, it would be something like that

with open("file.txt", "w") as f:
    f.write(message.content)
await ctx.send(file=discord.File("file.txt"))
#

your channel doesn’t exist

#

you sure the id is correct / executed in the correct server?

#

well, idk
it for sure is the chh channel object, get it fixed and it should work

#

can you send the code as text

#

can't copy paste from an image

#

all good

#
messages = await channel.history(limit=2000).flatten()
with open("ticket.txt", "w") as f:
    for message in messages:
         if message.content:
             f.write(message.content)
chh = guild.get_channel(1016279016648474624)
await chh.send(file=discord.File("ticket.txt"))
#

just send outside the loop
and put the file at the outer intend

#

for separate lines

f.write(message.content + "\n")
#

and

for message in reversed(messages):
#

or

for message in messages[::-1]:

for less readability

#

does it work?

#

no problem

#

use /close to archive this thread

#

ah yeah, I guess because of the \n's

#

you could also do it like this

#
f.write("\n".join([message.content for message in reversed(messages)]))
#

no extra \n

#

something interesting to note, if you run the command multiple times at the same time, you might get some unwanted behavior due to all files trying to access the same file

#

even with using this one line?

#

yes, the processes will overwrite each others files, so all processes might output the same data, even tho they shouldn’t

#

you could use random file names and delete the file again afterwards, this way the chance is lower, but not 0
or create no file at all, just with (uhm) IOStream? I don’t remember

#

just looked it up, it's StringIO, not IOStream

#
from io import StringIO
file = StringIO("your text")
await ctx.send(file=file)
#

it's technically not the correct type, but it works 😂

#

👍

#

oh, maybe it doesn’t work anymore

#

did this with pycord 2.0.1

#

let me check

#

hmm, still works for me

#

oh nvm

#

converted it with discord.File(fp=file)

#

totally did not see that line of code

#

ah, who cares 😂
there are other posts with less relevance regarding pycord