在消息discord.py上获取反应列表 [英] Get a List of Reactions on a Message discord.py

查看:26
本文介绍了在消息discord.py上获取反应列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to get a list of reactions on a discord message and I can't figure out what I'm doing wrong. Here's my code.

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

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

The code successfully adds the reaction, but it prints out an empty list. What am I missing?

推荐答案

之所以会出现这种情况,是因为 msg = await ctx.send('消息来加反应')是临时的,而不是其中的消息机器人的缓存消息.您只能得到缓存邮件的反应,因此,在您的情况下, msg.reactions 将返回一个空白列表.
另外,您正在使用 time.sleep(5),这是错误的,它将使整个程序停止5秒钟.使用异步功能,您必须导入 asyncio 并使用 asyncio.sleep().

This appens because msg = await ctx.send('Message to put reactions on') is temporary, it's not the message in the bot's cached messages. You can only get reactions of cached messages so, in your case msg.reactions will return a blank list.
Also, you were using, time.sleep(5), which is wrong, it would stop your entire program for 5 seconds. With asynchronous functions, you'd have to import asyncio and use 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)

参考:消息中没有反应.reactions

这篇关于在消息discord.py上获取反应列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆