1

I am looking for a way to export a text channel. To an HTML so that i can delete the channel.

I have seen this on another bot, Here is a photo. enter image description here

@bot.command()
async def export(ctx):
    # get the html here for ctx.channel
 
    await ctx.send(file=HTML)
Abdulaziz
  • 3,363
  • 1
  • 7
  • 24

2 Answers2

2

Using chat-exporter

# import chat_exporter
# import io
async def archive(channel, archive_channel):
    # channels are not None
    if channel and archive_channel:
        transcript = await chat_exporter.export(channel, set_timezone='UTC')
        transcript_file = discord.File(io.BytesIO(transcript.encode()),
                                       filename=f"{channel.name}.html")

        await archive_channel.send(file=transcript_file)
        # await channel.delete()

Using Tyrrrz's DiscordChatExporter (Not recommended)

Download the cli from GtiHub and export it to the same path

To get the path for chat exporter:

def get_chat_exporter_path():
    if os.name == 'nt': # windows environment
        return f'.{os.sep}DiscordChatExporter.CLI{os.sep}DiscordChatExporter.Cli.exe'
    elif os.name == 'posix': # linux environment
        return f'dotnet .{os.sep}DiscordChatExporter.CLI{os.sep}DiscordChatExporter.Cli.dll'
    else:
        return

To archive a channel

# import subprocess

async def archive(channel, archive_channel):
    path = get_chat_exporter_path()
    if not path:
        return

    file_path = f'.{os.sep}archive{os.sep}{channel.name}.html'
    subprocess.Popen([path, 'export', '-t', DISCORD_TOKEN, '-b', '-c', str(channel.id), '-o', file_path, '--dateformat', 'u'], shell=True).wait()
    await archive_channel.send(file=discord.File(file_path))
    await channel.delete()
Abdulaziz
  • 3,363
  • 1
  • 7
  • 24
0
@bot.command()
async def export(ctx): 
    messages = await ctx.channel.history(limit=None).flatten()

    file = open("file.html", "a")
    for i in messages:
        file.write(f'[{i.created_at}]{i.author} | {i.channel.name} | {i.content}<br> \n')
    file.close()
    await ctx.channel.send(file='file.html')

When ever the export command is called! the bot starts to store all the messages that are in the specific channel and stored in messages variable n converted to list and

with the help of open() function we open the HTML file and written the\

"[message time] user_name | channel_name | message"

[documentation:TextChannel.history]

MAKE SURE THAT THE BOT MUST HAVE THE PERMISSION TO READ MESSAGE HISTORY. enter image description here

Manoj A
  • 424
  • 2
  • 5
  • 13
  • [This ](https://imgur.com/a/RT9ijSE) is not the same as discord layout as shown in photo also embeds will not show correctly. in addition consider using `with open("file.html", "a" as f:` so that it closes the file on its own. – Abdulaziz Nov 15 '20 at 15:02
  • If there is some (html , css) expert we can make it look like that image. My code just gives the time , user , message etc. With html css you can make it look like that !. – Manoj A Nov 15 '20 at 16:45
  • Actully i found [this](https://github.com/Tyrrrz/DiscordChatExporter/releases/) Which is used in the photo. I will mark this as an answer. Thanks – Abdulaziz Nov 15 '20 at 19:17