Create transcript of discord channel

Viewed 2092

Is it possible to create an archive of a channel using discord.py? I have tried following this: discrod.py Text channel history to HTML file, however it:

  1. Didn't send the file, no matter what I tried;
  2. When I opened the file through the browser, it didn't create the style you see in ticket bots.

I am using discord-components, so therefore chat_exporter doesn't work due to a bug. Does anyone know how to do this?

2 Answers

I think this is what you are looking for.

fileName = f"{ctx.channel.name}.txt"
with open(fileName, "w") as file:
    async for msg in ctx.channel.history(limit=None):
        file.write(f"{msg.created_at} - 
                   {msg.author.display_name}: 
                   {msg.clean_content}\n")

If you want to send the file do:

file = discord.File(fileName)
await ctx.send(file=file)

Of course, you can send the file to wherever you want, but this is just an example.

The answer on The original post is now working however, the answer by Bram does also work. With the solution on the original post, chat-exporter will not work alongside discord-components, though there may be a fix for the issue soon (either by discord-components updating, or discord.py 2.0 releasing)

Related