'How to use discord.py in order to get audit log entries and analyze the data inside of it

I'm trying to make a discord bot that gets audit log entries and prints them out to me, but I have no clue how to go about it.

This is what I have so far:

from discord.ext import commands
import discord
import time

client = commands.Bot(command_prefix='!')

for entry in discord.Guild.audit_logs(action=discord.AuditLogAction.ban):
    print(f'{entry.user} banned {entry.target}')

@client.event
async def on_ready():
    print(f'Logged in as {client.user}.  Ready to go.')

client.run(token)

It throws this error: TypeError: audit_logs() missing 1 required positional argument: 'self'

I've read the documentation, but I am still no closer to solving this.

How do I get this bot to read each entry made into the audit log and then print it out to the console?



Solution 1:[1]

If you want to access the audit logs of the guild that a command has been invoked in, you should access through context, so use this:

@client.command()
async get_bans(ctx):
    async for entry in ctx.guild.audit_logs(action=discord.AuditLogAction.ban):
           print(f'{entry.user} banned {entry.target}')

If you want to get a specific guild's audit logs you can use:

async for entry in client.get_guild(guild_id).audit_logs(action=discord.AuditLogAction.ban):
       print(f'{entry.user} banned {entry.target}')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1