@Firepup650 @Sky
Here is the code:
import discord
from discord.ext import tasks
from discord import AuditLogAction, Client, Intents
from discord import app_commands
import os
intents = discord.Intents.all()
client = discord.Client(intents=intents)
guild_id = 786934479812952085
tree = app_commands.CommandTree(client)
# Initialize a variable to store the last processed timestamp
last_processed_timestamp = None
@tasks.loop(seconds = 10) # repeats after every 10 seconds. CHange it to adjust the speed. (YOu could try: 2628000 for a monthly check.
async def myLoop():
global last_processed_timestamp
# Get the guild object
guild = client.get_guild(guild_id)
if not guild:
print(f"Guild with ID {guild_id} not found.")
return
# Fetch the audit logs for bans
async for entry in guild.audit_logs(action=discord.AuditLogAction.ban, oldest_first=True): #set oldest_first=True to False if you only want to get the last log. Change action=discord.AuditLogAction.ban to anything else like .kick
# Check if the entry's timestamp is greater than the last processed timestamp
if last_processed_timestamp is None or entry.created_at > last_processed_timestamp:
print(f'{entry.user} banned {entry.target} at {entry.created_at} for {entry.reason}')
last_processed_timestamp = entry.created_at
@client.event
async def on_ready():
myLoop.start()
client.run(os.environ['TOKEN'])