简体   繁体   中英

Discord.py | Cooldown for events

The task of my code: When the moderator issues or removes roles from any server participant, the bot reads the logs, and then sends a message to the specified channel about which role was changed and to which, and so on.

My problem: When a moderator adds or removes several roles at once, the bot sends a message with information for each role at once. But I want there to be a delay when sending the event. This clogs up the chat logs and is annoying.

For example: I delete 5 roles at once, the bot has a delay of sending a message of 30 seconds. And in this message, he adds all 5 roles, not one at a time.

CODE:

@Bot.event
async def on_member_update(before, after):
    if before.roles != after.roles:
        channel = Bot.get_channel(827986763606786099)
        emb = discord.Embed(description = f'**Updating user roles -  {before.mention}**', colour = discord.Color.red())
        emb.add_field(name = '**Roles before**', value = ", ".join([r.mention for r in before.roles])) 
        emb.add_field(name = '**Roles after**', value = ", ".join([r.mention for r in after.roles])) 
        async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update): 
            if getattr(event.target, "id", None) != before.id:
                continue
            emb.add_field(name="Changed roles", value = ", ".join([getattr(r, "mention", r.id) for r in event.before.roles or event.after.roles]))  
            emb.add_field(name="Moderator", value = event.user)
            break
        await channel.send(embed = emb)

https://i.stack.imgur.com/v3h1v.jpg

Before running be sure to import asyncio import asyncio

    cooldown = []

    @bot.event()
    async def on_member_update(before, after):
        if before.roles != after.roles:
            global cooldown
            if before in cooldown:
                return
            cooldown.append(before)
            await asyncio.sleep(10) #here you can change how long the cooldown should be
            cooldown.remove(before)
            channel = bot.get_channel(688344722082627686)
            emb = discord.Embed(description=f'**Updating user roles -  {before.mention}**', colour=discord.Color.red())
            emb.add_field(name='**Roles before**', value=", ".join([r.mention for r in before.roles]))
            emb.add_field(name='**Roles after**', value=", ".join([r.mention for r in after.roles]))
            changed_roles = []
            for role in before.roles:
                if role in after.roles:
                    pass
                else:
                    changed_roles.append(role)

            for role in after.roles:
                if role in before.roles:
                    pass
                else:
                    if role in changed_roles:
                        pass
                    else:
                        changed_roles.append(role)

            text = ""
            for role in changed_roles:
                text = text + role.mention
            emb.add_field(name="Changed roles", value=text)
            async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
                if getattr(event.target, "id", None) != before.id:
                    continue
                emb.add_field(name="Moderator", value=event.user)
                break
            await channel.send(embed=emb)

Had to change getting the changed roles a bit, since I wasn't able to get how many audit logs I should have fetch in.

So what happens is: user is getting added to cooldown list, bot waits for 10 seconds so that the moderator can finish removing/adding the roles, after that bot gathers them all, removes the user from cooldown and sends the embed.

现在的样子

Here is the fully working code:

@Bot.event
async def on_member_update(before, after):
    if before.roles != after.roles:
        global cooldown
        if before in cooldown:
            return
        cooldown.append(before)
        await asyncio.sleep(5) # here you can change how long the cooldown should be
        cooldown.remove(before)
        channel = Bot.get_channel(ID log channel)
        emb = discord.Embed(description=f'**Updating user roles -  {before.mention}**', colour=discord.Color.orange())
        emb.add_field(name='Roles before', value=", ".join([r.mention for r in before.roles][1:]), inline=False)
        emb.add_field(name='Roles after', value=", ".join([r.mention for r in after.roles][1:]), inline=False)
        changed_roles = []
        for role in before.roles:
            if role in after.roles:
                pass
            else:
                changed_roles.append(role)

        for role in after.roles:
            if role in before.roles:
                pass
            else:
                if role in changed_roles:
                    pass
                else:
                    changed_roles.append(role)

        text = ""
        blacklist=[797920206407598098,817750571330961410,797916381621256202] # list roles, that should not be read
        for role in changed_roles:
            if role.id in blacklist:
                return
            text = text + role.mention
        emb.add_field(name="Changed roles", value=text, inline=False)
        async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
            if getattr(event.target, "id", None) != before.id:
                continue
            emb.add_field(name="Moderator", value=f"{event.user} \n**ID**: {event.user.id}")
            break
        await channel.send(embed=emb)

Thank you very much for helping @NimVrod

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM