记录在特定频道中发送的所有消息 discord.py

时间:2021-01-06 17:55:04

标签: python python-3.x discord discord.py

我想知道是否有一种方法可以记录在特定 Discord 服务器中发送的每条消息并将它们记录到 txt 文件中。到目前为止我有这个代码,但我认为它只记录正在发送的新消息。我需要它来记录所有内容。

@bot.event
async def on_message(message):
  await bot.process_commands(message)
  if (message.channel.id == '655864692333477926'):
  sentmsg2 = str(message.content)
  f=open("speclog.txt", "a+")
  for i in range(1):
   f.write(sentmsg2 + "\r\n")

1 个答案:

答案 0 :(得分:0)

您需要在代码中创建一个 for 循环来读取消息历史记录。方法如下。

counter = 0
async for message in channel.history(limit=LIMIT_HERE):
    if message.author == client.user:
        counter += 1
        # Write the 'message' variable in to the file

您可以在此处找到更多信息。 https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.history