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

2024-04-20 00:15:43 发布

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

我想知道是否有一种方法可以记录在一个特定的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")

Tags: 文件方法代码服务器txtevent消息message
1条回答
网友
1楼 · 发布于 2024-04-20 00:15:43

您需要在代码中创建一个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

相关问题 更多 >