【问题标题】:How can I get everyone who reacted to a certain reaction into a list?我怎样才能让对某个反应做出反应的每个人都进入一个列表?
【发布时间】:2020-11-11 05:42:06
【问题描述】:

基本上,我试图将所有对某条消息做出反应的人收集到一个列表中,这样我就可以看到谁参加了活动。我知道我可以看看每个手动做出反应的人,但我想稍后在有时间时对此进行扩展。

我的计划是添加 2 个单独的命令,一个是启动反应的命令,另一个是说明所有做出反应的人(列表)。但到目前为止,我只是坚持使用这个不起作用的单个命令。关于如何将其改造成 2 个命令的任何提示?

无论如何,到目前为止我有这个。

@client.command(pass_context = True)
async def react(ctx):

    msg = await ctx.send('React to this if you will be free for the event!')
    await msg.add_reaction("✅")
    await asyncio.sleep(5)
    cache_msg = discord.utils.get(client.messages, id = msg.id)
    for reactor in cache_msg.reactions:
        reactors = await client.get_reaction_users(reactor)
        for member in reactors:
            await ctx.send(member.name)

完整的追溯:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'messages'

【问题讨论】:

  • 该错误涉及Bot 对象,能否请您分享该对象的代码?

标签: python discord discord.py


【解决方案1】:

要记住的重要一点是message.reactions 给你的是实际反应,而不是成员。如果你有多个反应,你会得到一个包含多个“反应对象”的列表

这是您对同一命令中所有内容的选择。

@client.command()
async def react(ctx):
    msg = await ctx.send('React to this if you will be free for the event!')
    await msg.add_reaction("✅")
    await asyncio.sleep(5)
    
    # Easiest way of getting channel and message id
    channel = ctx.message.channel
    msg_id = msg.id

    # And since we have both channel and message id, we can:
    cache_msg = await channel.fetch_message(msg_id)

    # Now to get the reactions on the message and create
    # a list of users that reacted to a specific reaction
    # Also ignoring the bot
    for reactions in cache_msg.reactions:
        user_list = [user async for user in reactions.users() if user != client.user]
        for user in user_list:
            await ctx.send(user.name)

要在单独的命令中执行此操作,您可以将频道和消息 ID 存储为全局变量,或存储在单独的文件中。全局变量示例。

react_channel = ""
cache_msg_id = ""

@client.command(pass_context = True)
async def react(ctx):
    msg = await ctx.send('React to this if you will be free for the event!')
    await msg.add_reaction("✅")
    global react_channel
    react_channel = ctx.message.channel
    global cache_msg_id
    cache_msg_id = msg.id
    
@client.command()
async def reactors(ctx):
    global react_channel
    global cache_msg_id
    
    cache_msg = await react_channel.fetch_message(cache_msg_id)
    for reactions in cache_msg.reactions:
        user_list = [user async for user in reactions.users() if user != client.user]
        for user in user_list:
            await ctx.send(user.name)

【讨论】:

  • 啊,这就解释了。谢谢!
  • pass_context=True 实际上不再需要了。上下文总是被传递,这就是为什么第二个命令可以工作,即使你没有在那个命令中包含它。
  • @Cloud 哦,是的。我只是从OP中复制的。感谢您指出!现已编辑。
  • @SpaceTurtle0 您可以考虑添加我没有在代码中添加的一件事,它是一个过滤器,用于筛选它将列出​​的表情符号。 if reactions == "✅":。否则,如果有人对消息添加不同的反应,他们的名字也会由机器人在最后发送。
【解决方案2】:

由于显而易见的原因,您无法搜索机器人服务器中发送的所有消息。您可以使用 channel.fetch_message 获取带有反应的消息,并传入您的机器人发送的消息的 ID。

msg = await ctx.send("React to this if you will be free for the event!")
await msg.add_reaction("✅")
await asyncio.sleep(10)

new_message = await ctx.channel.fetch_message(msg.id)

for reaction in new_message.reactions:

    # only get the checkmark emoji users
    if (reaction.emoji == "✅"):

        # iterate to get all users except for bot
        async for user in reaction.users():
            if (user != bot.user):
                await ctx.send(user.name)

【讨论】:

  • 听从了您的建议,但这次我在控制台中没有错误,也没有来自不和谐机器人的任何输出。
  • cache_msg 的不同之处在于它是更新的消息(带有反应信息)。 msg此时没有任何反应。尝试使用for reaction in msg.reactions: 运行代码不会给你任何东西
  • 那么我可以使用其他方法吗?也许就像将成员附加到列表中。
  • @SpaceTurtle0 我已经用一个可行的解决方案编辑了我的答案。很抱歉之前提供了错误的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 2014-09-07
  • 2011-08-09
相关资源
最近更新 更多