【问题标题】:Get a List of Reactions on a Message discord.py获取消息上的反应列表 discord.py
【发布时间】:2020-11-07 22:33:42
【问题描述】:

我一直在尝试获取对不和谐消息的反应列表,但我无法弄清楚我做错了什么。这是我的代码。

async def reactionGetter(ctx):
    msg = await ctx.send('Message to put reactions on')
    await msg.add_reaction("✅")
    time.sleep(5)
    print(msg.reactions)

代码成功添加了反应,但它打印出一个空列表。我错过了什么?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    这是因为msg = await ctx.send('Message to put reactions on') 是临时的,它不是机器人缓存消息中的消息。您只能获得缓存消息的反应,因此,在您的情况下,msg.reactions 将返回一个空白列表。
    另外,您使用的是time.sleep(5),这是错误的,它会使您的整个程序停止 5 秒钟。使用异步函数,您必须导入 asyncio 并使用 asyncio.sleep()

    您必须将您的功能更改为:

    from asyncio import sleep
    
    async def reactionGetter(ctx):
        msg = await ctx.send('Message to put reactions on')
        await msg.add_reaction("✅")
        await sleep(2)
        cache_msg = discord.utils.get(bot.cached_messages, id=msg.id) #or client.messages depending on your variable
        print(cache_msg.reactions)
    

    参考:No reactions in Message.reactions

    【讨论】:

    • 谢谢,这很有用!我仍然对这种方法有疑问。 'Client' 对象没有属性'messages' 或'Bot' 对象没有属性'messages'。我的机器人和客户端如下; bot = commands.Bot(command_prefix='!') client = discord.Client() me = discord.ClientUser msg.channel.fetch_message 方法有效,但我总是很想找到更多解决问题的方法
    • 抱歉回复晚了,应该是bot.cached_messages^^'
    【解决方案2】:

    您可以使用TextChannel.fetch_message,这将强制您的机器人通过 API 调用从 Discord 获取消息信息,而不是依赖 Discord 通过 websocket 更新您的客户端。

    async def reactionGetter(ctx):
        msg = await ctx.send('Message to put reactions on')
        await msg.add_reaction("✅")
        msg = await msg.channel.fetch_message(msg.id)  # Can be None if msg was deleted
        print(msg.reactions)
    

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 2021-06-15
      • 1970-01-01
      • 2021-01-31
      相关资源
      最近更新 更多