【问题标题】:Log all messages ever sent in a specific channel discord.py记录在特定频道 discord.py 中发送的所有消息
【发布时间】:2021-04-12 12:05:36
【问题描述】:

我想知道是否有一种方法可以记录在一个特定的不和谐服务器中发送的每条消息并将它们记录到一个 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")

【问题讨论】:

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


【解决方案1】:

您需要在代码中创建一个读取消息历史记录的 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

【讨论】:

  • 所以我现在有了这个:@bot.event async def on_message(message): await bot.process_commands(message) if message.channel.id == '754386245429362778': counter = 0 async for message在 message.channel.history(limit=100): if message.author == bot.user: counter += 1 f=open("speclog.txt", "a+") for i in range(1): f. write(counter + "\r\n") 没有错误,但它没有记录任何内容
  • @haloper 可能会删除范围内的 i
  • @haloper 尝试将 a+ 更改为 w
猜你喜欢
  • 2021-05-03
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 2020-12-03
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
相关资源
最近更新 更多