How do I log if a user has been put into timeout?

172 Views Asked by At

I've tried a lot of things but I just can't do it. How can my Discord bot send a message when a user is timed out?

This code and a lots of other codes:

if before.pending and not after.pending:
        async for entry in after.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
            if entry.target.id == after.id and discord.utils.get(entry.after.roles, name="Timeout") is None:
                channel = bot.get_channel(1234567890) 
                await channel.send(f"{after.mention} wurde aus dem Timeout entfernt.")
    elif not before.pending and after.pending:
        async for entry in after.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
            if entry.target.id == after.id and discord.utils.get(entry.before.roles, name="Timeout") is None:
                channel = bot.get_channel(1234567890)
                await channel.send(f"{after.mention} wurde in den Timeout versetzt.")
1

There are 1 best solutions below

0
On BEST ANSWER

According to the docs, you can check if a Member is timed out by looking at the timed_out_until attribute.

So, if you want to track when a member is timed out, you can use the on_member_update event and check if the new member it's timed out.

@bot.event
async def on_member_update(before: discord.Member, after: discord.Member):
    if before.timed_out_until is None:
        if after.timed_out_until is not None:
            print(f"Member {after} was timed out!")