Discord.py kick/ban message

How to make a let my discord.py bot check (maybe in server log) if a player got kicked/banned? (Not necessarily from by bot) I knoe this from the mee6 bot, if you kick/ban (idk if both) someone doesn’t matter if via the command or clicking on the User>kick/ban it sends a message that this user got kicked/banned. (Hope you guys understand me)

2 Likes

You can always check the audit log from your discord server to know who got banned.

But you want a specific bot that will inform you of who got banned in your server, is that it?

3 Likes

Yep, thats what I want. Sorry if i asked on a to complicated way

1 Like

Oh, you can use the on_member_ban event from discord.py to track those bans and message it to you.

For example:

@bot.event
async def on_member_ban(guild, user):
    try:
        fetched_logs = await guild.audit_logs(action=discord.AuditLogAction.ban, limit=1).flatten()
        if fetched_logs:
            ban_entry = fetched_logs[0]
            msg = f'{user.name}#{user.discriminator} was banned by {ban_entry.user.name}#{ban_entry.user.discriminator}'
            user = await bot.fetch_user(user_id) #insert your user id here to be message about
            await user.send(msg)
    except Exception as e:
        print(f"Error: {e}")
3 Likes

Also possible for kicks?

1 Like

Yes, you just need to use the on_member_remove event.

@bot.event
async def on_member_remove(member):
    try:
        fetched_logs = await member.guild.audit_logs(action=discord.AuditLogAction.kick, limit=1).flatten()
        if fetched_logs:
            kick_entry = fetched_logs[0]
            if kick_entry.target == member:
                msg = f'{member.name}#{member.discriminator} was kicked by {kick_entry.user.name}#{kick_entry.user.discriminator}'
                user = await bot.fetch_user(user_id) #you already know
                await user.send(msg)
    except Exception as e:
        print(f"Error: {e}")

(Ps.: remember to give the right permissions for your bot so he’d be able to catch those informations)

4 Likes

I got an error:

1 Like

Oh sorry, I made a mistake, try again

@bot.event
async def on_member_ban(guild, user):
    try:
        fetched_logs = await guild.audit_logs(action=discord.AuditLogAction.ban, limit=1).flatten()
        ban_entry = next(iter(fetched_logs), None)
        if ban_entry:
            msg = f'{user.name}#{user.discriminator} was banned by {ban_entry.user.name}#{ban_entry.user.discriminator}'
            user = await bot.fetch_user(user_id)
            await user.send(msg)
    except Exception as e:
        print(f"Error: {e}")

@bot.event
async def on_member_remove(member):
    try:
        fetched_logs = await member.guild.audit_logs(action=discord.AuditLogAction.kick, limit=1).flatten()
        kick_entry = next(iter(fetched_logs), None)
        if kick_entry and kick_entry.target == member:
            msg = f'{member.name}#{member.discriminator} was kicked by {kick_entry.user.name}#{kick_entry.user.discriminator}'
            user = await bot.fetch_user(user_id)
            await user.send(msg)
    except Exception as e:
        print(f"Error: {e}")

Remember to use the right imports

import discord
from discord.ext import commands
import asyncio
5 Likes

Stop using flatten(). The method audit_logs() returns an AsyncIterator. This feature was removed in newer versions; in older versions, it could work, but not anymore. flatten() is deprecated.

Side Note: It sems to be that you are using ChatGPT; this is an assumption, but it’s valid since ChatGPT is limited to January 2022 knowledge, and flatten() was deprecated around May (of the same year).

Docs: Migrating to v2.0

1 Like

You’d have to check audit logs:

from discord import AuditLogAction

action = AuditLogAction.kick # .ban

async for entry in guild.audit_logs(action=action, limit=1):
    if entry.user_id != bot.user.id:
            return entry
    return None
2 Likes

How can I send this in a channel?

2 Likes

@Sky, Um could u answer this. Or anybody else?

2 Likes

What do you need to send in a channel?

1 Like

The the info that someone got kicked/banned irk how to use return

2 Likes
from discord import AuditLogAction, Client, Intents

client = Client(Intents.all())
action = AuditLogAction.kick # .ban
_id = 123456789

async for entry in guild.audit_logs(action=action, limit=1):
    channel = client.get_channel(_id)
    if entry.user_id != bot.user.id:
       await channel.send(entry)
    return None
3 Likes

I don’t get it. Everything is marked as wrong

2 Likes


It just gives erros, already tried to fix it but idk how to fix some things

1 Like

Your code doesn’t make much sense (async for outside an async method), and your id variable isn’t indented properly.

Have you considered learning Python before making a discord bot?

nvm guys, I figured out how to check it. but still thanks a lot

:wave: Hey @Babychaosfloh!

It’s great to hear that you’ve fixed your problem, but please provide an explanation to how you did, so that other users with the same issue can easily find the solution.

1 Like