不和谐机器人python

不和谐机器人python,python,discord,discord.py,bots,Python,Discord,Discord.py,Bots,我需要python中discord bot的帮助。 我需要一个bot来保存通过dm发送给它的每条消息 我试过了,但它只节省了时间和邮件数量,而没有节省内容 async def update_stats(): 等待机器人。等待直到准备就绪() 全球信息 而不是bot.is_closed(): 尝试: 以open(“stats.txt”、“a”)作为f: f、 写入(f“Czas:{int(time.time())},Wiad:{messages}\n”) 消息=0 等待asyncio.sleep(

我需要python中discord bot的帮助。 我需要一个bot来保存通过dm发送给它的每条消息

我试过了,但它只节省了时间和邮件数量,而没有节省内容

async def update_stats():
等待机器人。等待直到准备就绪()
全球信息
而不是bot.is_closed():
尝试:
以open(“stats.txt”、“a”)作为f:
f、 写入(f“Czas:{int(time.time())},Wiad:{messages}\n”)
消息=0
等待asyncio.sleep(30)
例外情况除外
打印(e)
等待asyncio.sleep(30)
@机器人事件
异步def on_消息(消息):
全球信息
消息+=1
bot.loop.create_任务(更新_stats())

是的,这是完全有效的,因为您只保存消息的数量。你也应该保存这些消息

未测试代码(我现在没有机器人的完整环境):

消息=0
消息列表=列表()
异步def更新_stats():
等待机器人。等待直到准备就绪()
而不是bot.is_closed():
尝试:
以open(“stats.txt”、“a”)作为f:
f、 写入(f“Czas:{int(time.time())}\n”)
f、 写入(“消息:\n”)
对于消息列表中的消息:
f、 写(信息)
消息\u list.clear()
消息=0
等待asyncio.sleep(30)
例外情况除外
打印(e)
等待asyncio.sleep(30)
@机器人事件
异步def on_消息(消息):
消息+=1
messages\u list.append(消息)
bot.loop.create_任务(更新_stats())
但是,如果要在邮件到达时保存邮件,则只能编写一个更简单的函数:

@bot.event
异步def on_消息(消息):
以open(“stats.txt”、“a”)作为f:
f、 写入(f“Czas:{int(time.time())}\n”)
f、 写入(“新消息:\n”)
f、 写(信息)
另一个编辑:正如我所记得的,bot不仅获取原始格式的消息,还获取具有消息“上下文”的对象,因此函数应该如下所示:

@bot.event
异步def on_消息(消息):
如果message.channel.id==message.author.dm_channel.id:
以open(“stats.txt”、“a”)作为f:
f、 写入(f“Czas:{int(time.time())}\n”)
f、 写入(“新消息:\n”)
f、 写入(message.content)