Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.84k stars 3.76k forks source link

Error during fetching audit logs #6784

Closed TheShubhendra closed 3 years ago

TheShubhendra commented 3 years ago

Summary

Unable to fetch audit logs

Reproduction Steps

from discord import Client
import threading
import asyncio
import logging
logging.basicConfig(
    format="%(asctime)s - %(name)s - %(message)s",
    level=logging.INFO,
)
client = Client()
DISCORD_TOKEN="<TOKEN>"
async def save_audit_logs(guild):
     with open(f'audit_logs_{guild.name}', 'w+') as f:
          async for entry in guild.audit_logs(limit=100):
               f.write('{0.user} did {0.action} to {0.target}'.format(entry))

@client.event
async def on_message(message):
     if message.content.startswith('Hmmm'):
        channel = client.get_channel(<CHANNEL ID>)
        await save_audit_logs(channel.guild)

client.run(DISCORD_TOKEN)

Minimal Reproducible Code

No response

Expected Results

Audit logs

Actual Results

Ignoring exception in on_message
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/client.py", line 360, in _run_event
    await coro(*args, **kwargs)
  File "/storage/emulated/0/shubhendra/bot/discord/x.py", line 21, in on_message
    await save_audit_logs(channel.guild)
  File "/storage/emulated/0/shubhendra/bot/discord/x.py", line 15, in save_audit_logs
    f.write('{0.user} did {0.action} to {0.target}'.format(entry))
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/utils.py", line 60, in __get__
    value = self.function(instance)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/audit_logs.py", line 318, in target
    return converter(self._target_id)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/audit_logs.py", line 375, in _convert_target_invite
    changeset = self.before if self.action is enums.AuditLogAction.invite_delete else self.after
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/utils.py", line 60, in __get__
    value = self.function(instance)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/audit_logs.py", line 352, in after
    return self.changes.after
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/utils.py", line 60, in __get__
    value = self.function(instance)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/audit_logs.py", line 334, in changes
    obj = AuditLogChanges(self, self._changes)
  File "/data/data/com.termux/files/usr/lib/python3.9/site-packages/discord/audit_logs.py", line 169, in __init__
    if key in ['allow', 'deny', 'permissions']:
UnboundLocalError: local variable 'key' referenced before assignment

Intents

All

System Information

Checklist

Additional Context

No response

Rapptz commented 3 years ago

Your library version is too old (v1.5.1). The latest version as of this writing is v1.7.1. It is recommended to update.