计算消息中的反应并在发生一定量的反应后执行操作

时间:2020-04-03 20:02:03

标签: python discord discord.py

我希望我的机器人在对投票消息做出一定程度的反应后,取消某人的角色。知道如何检查当前在邮件中添加了多少邮件吗?

@client.command()
async def derank(ctx, member: discord.Member, *, reason=None):
    counter = 0
    votes_to_pass = 2
    msg = await ctx.send("Member: " + discord.Member + "\n Reason: " + reason)
    # count reactions on msg
    if counter >= votes_to_pass:
        await ctx.send("User deranked")
        # take the role away from user

1 个答案:

答案 0 :(得分:0)

@client.command()
async def derank(ctx, member: discord.Member, *, reason=None):
    votes_to_pass = 2
    msg = await ctx.send(f"Member: {member}\nReason: {reason}") # This line was erroneous, fixed it. Try to use f-strings quite more than concatinating.
    # count reactions on msg
    try:
        async def reactionFunction():
            while True:
                reaction, user = await client.wait_for('reaction_add', check = lambda reaction, user: str(reaction.emoji) == '?', timeout=60.0) # Wait 1 minute for any reaction
                if reaction.message.id == msg.id: # If the message reacted to is the one we sent (msg)
                    for reaction in reaction.message.reactions:
                        if (reaction.count >= votes_to_pass): # If the reactions of that message are the number we game (votes_to_pass) or more then
                            return # This returns out of the function.
                            # As you can see, the reason I used a function is so I can be able to return
                            # This is because using break will just break out of the for look but not the while loop but return breaks out of the whole thing as it's a function.
                            # If only python made it easy to break twice, but anyway this works
        await reactionFunction()


        # This is the part where it removes the role.
        role = discord.utils.get(ctx.guild.roles, name='ROLE_NAME') # Get a role by name. To get it by ID replace name ='ROLE_NAME' to name = ID
        await member.remove_roles(role) # Remove the role that we got (role) from the member mentioned.

        await ctx.send("User deranked") # Say this after removing the roles