简体   繁体   English

不和谐.py | 事件冷却

[英]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.例如:我一次删除5个角色,bot有30秒的延迟发送消息。 And in this message, he adds all 5 roles, not one at a time.在这条消息中,他添加了所有 5 个角色,而不是一次添加一个。

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 https://i.stack.imgur.com/v3h1v.jpg

Before running be sure to import asyncio import asyncio运行前一定要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.所以发生的事情是:用户被添加到冷却列表,bot 等待 10 秒钟,以便主持人可以完成删除/添加角色,在该机器人收集所有角色之后,从冷却中删除用户并发送嵌入。

现在的样子

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非常感谢您帮助@NimVrod

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

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