【问题标题】:How to properly get user list from reactions?如何从反应中正确获取用户列表?
【发布时间】:2021-07-16 01:41:42
【问题描述】:

我目前正在尝试创建一个 Discord Bot,将人们随机分成 2 个团队。 现在,我能够发送带有反应的消息,然后返回对机器人消息做出反应的用户列表。但我面临以下问题:

  1. 列表包括机器人;
  2. 该列表仅包括机器人和第一个对消息做出反应的人(我想在命中 10 个不包括机器人的玩家后立即随机化)
  3. 我需要将列表分成两个团队
if str(reaction.emoji) == '✅': ## right now this is just selecting the game
    def check(reaction, user): ## this check is here but I was unable to properly use it without breaking the code, or even defining the limit of 10 reactions
        return user != bot.user and (str(reaction.emoji) == '????')

    msg_players1 = await ctx.send('Summoners, react below: ')
    await msg_players1.add_reaction('????')
    msg_players1 = await ctx.channel.fetch_message(msg_players1.id)
    reactions = msg_players1.reactions
    users = set()
    
    for reactions in user, reaction:
        async for user in reaction.users():
            users.add(user)

    await ctx.send(f"time 1: {', '.join(user.name for user in users)}")

我对编码非常陌生,对 Discord 机器人更是如此,非常感谢任何帮助! 谢谢!

编辑:我刚刚测试过,它没有返回对 ???? 做出反应的用户名表情符号,它返回调用命令的用户名

【问题讨论】:

  • 您可以尝试使用while True 循环并等待您的反应。为了排除机器人反应,我们可以检查反应是否来自用户,也可以使用一种循环。 (类似于:while user == self.bot.user

标签: python discord discord.py


【解决方案1】:

您在添加回复后立即获取消息,没有足够的时间让用户对其做出反应。您可以睡x 秒数并获取消息,但这是一个非常糟糕的主意,您必须猜测给 10 个用户多少时间做出反应。

您可以改为使用 while 循环,等待确切的 10 个反应(不包括机器人)并继续。

users = set() 

def check(reaction, user):
    return str(reaction) == "✅" and user != bot.user  # or `client.user`, however you named it

while len(users) < 10:
    reaction, user = await bot.wait_for("reaction_add", check=check)
    users.add(user)

# once here, divide the `users` set into teams

参考:

【讨论】:

  • 非常感谢,我使它与这个变体一起工作,并通过players = list(users) 将集合转换为列表,然后改组。
猜你喜欢
  • 2019-07-10
  • 2020-03-30
  • 2020-05-09
  • 1970-01-01
  • 2020-11-07
  • 2019-09-13
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多