(不和.py)制作一个记录程序来保存bot所在的对话

2024-04-20 02:55:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我如何在不和.py它将对话保存到文本文件中。在

例如,bot将所有聊天记录保存在一个名为“chatlogs”的文件夹中,而在discordservera中,每当有人说了机器人能看到的东西,机器人就会把它记录在一个名为服务器A.txt服务器B添加我的bot时,它会生成一个名为服务器B.txt并保存所有服务器B其中的对话。在


Tags: py服务器txt文件夹bot记录机器人对话
1条回答
网友
1楼 · 发布于 2024-04-20 02:55:57

^{}事件中,以追加模式打开文件并写入最新消息。在

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_message(message):
    guild = message.guild
    if guild:
        path = "chatlogs/{}.txt".format(guild.id)  
        with open(path, 'a+') as f:
            print("{0.timestamp} : {0.author.name} : {0.content}".format(message), file=f)
    await bot.process_commands(message)

bot.run("token")

相关问题 更多 >