【问题标题】:Discord python export an entire chat into .txt fileDiscord python将整个聊天导出到.txt文件
【发布时间】:2020-08-18 17:15:11
【问题描述】:

我已经进行了广泛的研究,但仍然没有找到一种方法来做到这一点,我基本上想要的是机器人在听完命令后,抓取某人在上下文通道中发送的每条消息并将其转换为一个 .txt 文件,我什至不知道这是否可以使用 python,因为我对 discord.py 世界还比较陌生,任何帮助都将不胜感激,提前谢谢你。

【问题讨论】:

  • 嗨@Sp0t!有人对此表示反对,可能是因为问题不够具体。但这仍然是一个好问题,可能还有其他人想要一个简单的解决方案。问题是,可能没有(因此太不具体)。但我用谷歌搜索了and found this。这不是你要找的东西吗?祝你好运找到解决方案!

标签: python discord discord.py


【解决方案1】:

您可以像这样在 for 循环中使用 channel.history()

@client.command()
async def test(ctx):
    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")

entry 将是一个discord.Message 对象,因此您将能够访问它的作者、内容、提及...但是,由于 API 限制,这种方法在大频道时可能会很慢,所以请确保来限制这个命令。

PS:msg.created_at 以 UTC 格式返回消息的创建时间。这是datetime.datetime 类型,因此您可以使用format it with the datetime library

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 2016-05-08
    • 2021-06-24
    • 2021-12-23
    • 2021-10-06
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多