获取消息的反应列表discord.py

2024-02-03

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

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

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


发生这种情况是因为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)

参考: Message.reactions 中没有反应 https://github.com/Rapptz/discord.py/issues/861#issuecomment-338355516

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取消息的反应列表discord.py 的相关文章

随机推荐