【问题标题】:How to make a discord bot react to PAST private messages?如何让不和谐机器人对过去的私人消息做出反应?
【发布时间】:2020-09-03 08:46:01
【问题描述】:

我制作了一个简单的 discord.py 机器人,只要它被 PM'd 就会做出反应。但是,如果有人在我的机器人离线时向它发送消息,它不会做出反应。如何让它在启动时显示在线时收到的所有消息?

当前代码:

import discord
from discord.ext import commands
import asyncio

client = commands.Bot(command_prefix='.')

@client.event
async def on_message(message):
if message.guild is None and message.author != client.user:

    send_this_console=f'{message.author}: \n{message.content}\n'
    print(send_this_console)
await client.process_commands(message)

【问题讨论】:

    标签: python discord python-asyncio discord.py discord.py-rewrite


    【解决方案1】:

    您需要使用消息历史记录并跟踪最后发送的消息。 获取消息的创建时间:

    lastMessageTimestamp = message.created_at
    

    当你的机器人离线时,你需要以任何你想要的方式存储它(例如使用pickle)并执行以下操作:

    async for message in user.history(after = lastMessageTimestamp):
      print(message.content)
    

    【讨论】:

    • 我尝试了您的解决方案,但我得到了一个我预期的错误:async for message in user.history(after = searchsince): NameError: name 'user' is not defined。我如何遍历所有给我发消息的用户?
    • 你需要先给变量user赋值。也许循环用户列表?机器人不像用户那样有未读消息的概念。
    猜你喜欢
    • 2018-11-19
    • 2020-12-06
    • 2018-12-15
    • 1970-01-01
    • 2020-12-14
    • 2020-11-07
    • 2020-10-03
    • 2021-05-19
    • 2020-01-29
    相关资源
    最近更新 更多