如何使用 discord.py 获取审计日志条目并分析其中的数据

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

我正在尝试制作一个 discord 机器人来获取审计日志条目并将它们打印出来给我,但我不知道该怎么做。

这是我目前拥有的:

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)

它抛出这个错误: TypeError: audit_logs() missing 1 required positional argument: 'self'

我已经阅读了 documentation,但我离解决这个问题还差得很远。

如何让此机器人读取审计日志中的每个条目,然后将其打印到控制台?

如果你想访问调用命令的公会的审计日志,你应该通过上下文访问,所以使用这个:

@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}')

如果您想获取特定公会的审核日志,您可以使用:

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