Подсчитайте реакцию на сообщение и выполните действие после определенного количества реакций. - PullRequest
1 голос
/ 03 апреля 2020

Я бы хотел, чтобы мой бот забрал чью-то роль после того, как в сообщении для голосования будет достигнуто определенное количество реакций. Любая идея, как проверить, сколько из них в настоящее время добавлено в сообщение?

@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 голосов
/ 04 апреля 2020
@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

...