disnake
disnake copied to clipboard
Object doesn't have name and type attributes
Summary
Object doesn't have name and type attributes when it's provided in AuditLogEntry as extra and AuditLogEntry action is connected with overwrites
Reproduction Steps
- Catch the
disnake.Event.audit_log_entry_createevent with action connected with overwrites. - Check the type of AuditLogEntry.extra and if it's the Object, move to the next step.
- Try to get Object.name or Object.type and get the AttributeError exception.
Minimal Reproducible Code
def extra_to_json(extra, action: AuditLogAction):
match action:
case AuditLogAction.overwrite_create | AuditLogAction.overwrite_update | AuditLogAction.overwrite_delete:
if isinstance(extra, Role):
return {
"id": extra.id,
"name": extra.name,
"type": "role"
}
elif isinstance(extra, Member):
return {
"id": extra.id,
"name": extra.name,
"type": "member"
}
elif isinstance(extra, Object):
return {
"id": extra.id,
"name": extra.name,
"type": extra.type
}
Expected Results
The extra of type Object has the name and type attributes.
Actual Results
AttributeError exception.
Ignoring exception in on_audit_log_entry_create
Traceback (most recent call last):
File "C:\Users\Dmitriy\Documents\dev\python\EAL\venv\Lib\site-packages\disnake\client.py", line 703, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Dmitriy\Documents\dev\python\EAL\src\cogs\audit_entry_event_handlers.py", line 12, in add_audit_log_entry_to_database
return await entries_methods.add_entry(entry=entry)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Dmitriy\Documents\dev\python\EAL\src\database\methods\entries_methods.py", line 14, in add_entry
extra = extra_to_json(entry.extra, action=entry.action)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Dmitriy\Documents\dev\python\EAL\src\utils\entries_fields_to_json.py", line 207, in extra_to_json
"name": extra.name,
^^^^^^^^^^
AttributeError: 'Object' object has no attribute 'name'
Intents
guilds, message_content, messages, moderation
System Information
- Python v3.11.4-final
- disnake v2.9.1-final
- disnake importlib.metadata: v2.9.1
- aiohttp v3.9.3
- system info: Windows 10 10.0.22631 AMD64
Checklist
- [x] I have searched the open issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have removed my token from display, if visible.
Additional Context
The docs says that name and type must be defined in extra of Object type if the action of AuditLogEntry is connected with overwrites.
https://docs.disnake.dev/en/stable/api/audit_logs.html#disnake.AuditLogAction.overwrite_create
hmm. The documentation here has been slightly incorrect for a surprisingly long time, essentially since audit logs got implemented initially years ago, but the issue really only surfaced with the new audit log event.
A name field only exists if the Object refers to a role; the API doesn't provide a name in the member case. A type: "role" | "member" field only existed in API responses prior to v8, but wasn't exposed by the library in any way.
Adjusting the documentation here probably makes the most sense. The current solution already isn't pretty, and adding new hacks on top of existing hacks doesn't sound amazing.
For what it's worth, there have been plans in the past to add a type attribute to Objects in general, but it's not trivial.