【发布时间】:2021-02-26 20:12:18
【问题描述】:
我正在寻找一种导出文本通道的方法。到一个 HTML,以便我可以删除频道。
@bot.command()
async def export(ctx):
# get the html here for ctx.channel
await ctx.send(file=HTML)
【问题讨论】:
标签: python html discord discord.py
我正在寻找一种导出文本通道的方法。到一个 HTML,以便我可以删除频道。
@bot.command()
async def export(ctx):
# get the html here for ctx.channel
await ctx.send(file=HTML)
【问题讨论】:
标签: python html discord discord.py
@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')
无论何时调用export 命令!机器人开始存储特定通道中的所有消息并存储在messages变量n中转换为list和
在open()函数的帮助下,我们打开了HTML文件并编写了\
"[message time] user_name | channel_name | message"
【讨论】:
with open("file.html", "a" as f:,以便它自行关闭文件。
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()
Tyrrrz's DiscordChatExporter(不推荐)从GtiHub下载cli并导出到同一路径
获取聊天导出器的路径:
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
存档频道
# 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()
【讨论】: