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)
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?
Yep, thats what I want. Sorry if i asked on a to complicated way
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}")
Also possible for kicks?
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)
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
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
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
How can I send this in a channel?
What do you need to send in a channel?
The the info that someone got kicked/banned irk how to use return
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
I don’t get it. Everything is marked as wrong
It just gives erros, already tried to fix it but idk how to fix some things
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
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.