简体   繁体   English

Discord.py:我如何让机器人获得在给定时间内对消息做出反应的用户列表

[英]Discord.py: how do i make the bot get a list of users who reacted to a message in a given time

i am making a discord.py bot on python 3.8.6.我正在 python 3.8.6 上制作一个 discord.py 机器人。

i want this particular function to work in the specific way...我希望这个特定的 function 以特定的方式工作......

  • the bot sends a embed message = await ctx.send(embed=e)机器人发送嵌入message = await ctx.send(embed=e)
  • the bot will add a react to the embed await message.add_reaction('✅')机器人将对嵌入的await message.add_reaction('✅')添加一个反应
  • the bot will wait for 30 seconds for other users to react and make a list of who ever reacts within 30 seconds机器人将等待 30 秒让其他用户做出反应,并列出 30 秒内做出反应的人
  • proceed with further commands继续执行进一步的命令

code-代码-

@bot.command()
async def command_name(ctx, function):


    #code for function 1

    #code for function 2

        # making the embed

        message = await ctx.send(embed=e)
        await message.add_reaction('✅')

        def check(reaction, user):
            if user not in players and not user.bot:
                players.append(user.mention)
            return reaction.message == message and str(reaction.emoji) == '✅'

        try:
            await bot.wait_for('reaction_add', timeout=10, check=check)

        except asyncio.TimeoutError:
            await ctx.send('time is up, and not enough players')

        else:
            await ctx.send(players) 
            # further code

issue: the bot instantly sends the list players问题:机器人立即发送玩家列表

question: what can i add for it to wait 30 seconds, append the list with users who react to the embed and then send the list and if after 30 seconds len(players) isn't 3 or more send -问题:我可以添加什么让它等待 30 秒,append 列表与对嵌入做出反应的用户然后发送列表,如果 30 秒后 len(players) 不是 3 或更多发送 -

await ctx.send('time is up, and not enough players')

The problem is that await bot.wait_for('reaction_add', timeout=10, check=check) proceeds at the first reaction made.问题是await bot.wait_for('reaction_add', timeout=10, check=check)在做出第一个反应时继续进行。 In this case, the bot reaction instantly triggers this event, so an empty list will be returned.在这种情况下,机器人反应会立即触发此事件,因此将返回一个空列表。

I thought of two options:我想到了两个选择:

  1. Make the check function always return False, but still append the user.使检查 function 始终返回 False,但仍然是 append 用户。 This way the wait_for function will stay active until the timeout and then raise an Exception.这样 wait_for function 将保持活动状态直到超时,然后引发异常。 Continue your code in the except block or pass it.在 except 块中继续您的代码或传递它。
  2. Wait 30 seconds, then fetch the message again and append all users who have reacted with the checkmark.等待 30 秒,然后再次获取消息和 append 所有对复选标记做出反应的用户。

First option:第一个选项:

@client.command()
async def command_name(ctx):

    players = []

    message = await ctx.send("Message")
    await message.add_reaction('✅')

    def check(reaction, user):
        if user not in players and not user.bot:
            players.append(user.mention)
        return False

    try:
        await client.wait_for('reaction_add', timeout=30, check=check)

    except asyncio.TimeoutError:
        pass

    if len(players) < 3:
        await ctx.send('Time is up, and not enough players')
    else:
        await ctx.send(players) 

Second (preferred) option:第二个(首选)选项:

@client.command()
async def command_name(ctx):

    players = []

    message = await ctx.send("Message")
    await message.add_reaction('✅')
    await asyncio.sleep(30)

    message = await ctx.fetch_message(message.id)

    for reaction in message.reactions:
        if reaction.emoji == '✅':
            async for user in reaction.users():
                if user != client.user:
                    players.append(user.mention)

    if len(players) < 3:
        await ctx.send('Time is up, and not enough players')
    else:
        await ctx.send(players)

References to the API :参考API
fetch_message 获取消息
reaction.users() 反应.users()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让对消息中的表情符号做出反应的用户 (discord.py) - how to get the users who reacted with an emoji in a message (discord.py) 如何获取对discord.py中的消息做出反应的成员列表? - How to get the list of members that reacted to a message in discord.py? 检查用户列表是否对消息 discord.py 重写做出反应 - Check if a list of users reacted to message discord.py rewrite 如何获取对 discord.py 中的反应做出反应的人的 id - how to get id of people who reacted to a reaction in discord.py 如何让 Discord.py 得到有多少用户对特定表情符号做出反应? - How to make Discord.py get how MANY users reacted with a specific emoji? 检查谁对 wait_for 中的消息做出反应检查 discord.py - check who reacted to a message in a wait_for check discord.py 如何获取编写 bot 命令的用户? Discord.py - How do I get the user who wrote the bot command? Discord.py 如何让不和谐机器人使用 discord.py 向消息添加表情符号反应? - How do I get a discord bot to add an emoji reaction to a message using discord.py? Discord.py 如何让我的机器人在重复用户参数时无法 ping 所有人 - Discord.py How do I make my bot not ping everyone when it repeats a users argument 我如何让我的机器人判断给出的数字是高于还是低于答案? [不和谐.py] - How do I make my bot tell if the number given was higher or lower then the answer? [discord.py]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM