Skip to content

Commit d1702cf

Browse files
LulalabyDorukyumpre-commit-ci[bot]
authored
fix: update audit log filtering & sorting (#2371)
* fix: either i'm high and it won't work, or i'm god and this works * Update iterators.py * Update iterators.py Signed-off-by: Lala Sabathil <[email protected]> * chore: clean code Signed-off-by: Dorukyum <[email protected]> * chore: add changelog entry Signed-off-by: Dorukyum <[email protected]> * chore: add changelog entry Signed-off-by: Dorukyum <[email protected]> * style(pre-commit): auto fixes from pre-commit.com hooks * Update guild.py Signed-off-by: Dorukyum <[email protected]> --------- Signed-off-by: Lala Sabathil <[email protected]> Signed-off-by: Dorukyum <[email protected]> Co-authored-by: Dorukyum <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent ec3cf16 commit d1702cf

File tree

3 files changed

+19
-54
lines changed

3 files changed

+19
-54
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ These changes are available on the `master` branch, but have not yet been releas
7676
to `None`. ([#2464](https://github.com/Pycord-Development/pycord/pull/2464))
7777
- Fixed `remove_application_command` causing issues while reloading extensions.
7878
([#2480](https://github.com/Pycord-Development/pycord/pull/2480))
79+
- Fixed outdated logic for filtering and sorting audit log entries.
80+
([#2371](https://github.com/Pycord-Development/pycord/pull/2371))
7981

8082
### Changed
8183

@@ -103,6 +105,9 @@ These changes are available on the `master` branch, but have not yet been releas
103105
- Removed the `delete_message_days` parameter from ban methods. Please use
104106
`delete_message_seconds` instead.
105107
([#2421](https://github.com/Pycord-Development/pycord/pull/2421))
108+
- Removed the `oldest_first` parameter from `Guild.audit_logs` in favor of the `before`
109+
and `after` parameters.
110+
([#2371](https://github.com/Pycord-Development/pycord/pull/2371))
106111

107112
## [2.5.0] - 2024-03-02
108113

discord/guild.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,14 +3266,16 @@ def audit_logs(
32663266
limit: int | None = 100,
32673267
before: SnowflakeTime | None = None,
32683268
after: SnowflakeTime | None = None,
3269-
oldest_first: bool | None = None,
32703269
user: Snowflake = None,
32713270
action: AuditLogAction = None,
32723271
) -> AuditLogIterator:
32733272
"""Returns an :class:`AsyncIterator` that enables receiving the guild's audit logs.
32743273
32753274
You must have the :attr:`~Permissions.view_audit_log` permission to use this.
32763275
3276+
See `API documentation <https://discord.com/developers/docs/resources/audit-log#get-guild-audit-log>`_
3277+
for more information about the `before` and `after` parameters.
3278+
32773279
Parameters
32783280
----------
32793281
limit: Optional[:class:`int`]
@@ -3286,9 +3288,6 @@ def audit_logs(
32863288
Retrieve entries after this date or entry.
32873289
If a datetime is provided, it is recommended to use a UTC aware datetime.
32883290
If the datetime is naive, it is assumed to be local time.
3289-
oldest_first: :class:`bool`
3290-
If set to ``True``, return entries in oldest->newest order. Defaults to ``True`` if
3291-
``after`` is specified, otherwise ``False``.
32923291
user: :class:`abc.Snowflake`
32933292
The moderator to filter entries from.
32943293
action: :class:`AuditLogAction`
@@ -3333,7 +3332,6 @@ def audit_logs(
33333332
before=before,
33343333
after=after,
33353334
limit=limit,
3336-
oldest_first=oldest_first,
33373335
user_id=user_id,
33383336
action_type=action,
33393337
)

discord/iterators.py

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ def __init__(
476476
limit=None,
477477
before=None,
478478
after=None,
479-
oldest_first=None,
480479
user_id=None,
481480
action_type=None,
482481
):
@@ -485,7 +484,6 @@ def __init__(
485484
if isinstance(after, datetime.datetime):
486485
after = Object(id=time_snowflake(after, high=True))
487486

488-
self.reverse = after is not None if oldest_first is None else oldest_first
489487
self.guild = guild
490488
self.loop = guild._state.loop
491489
self.request = guild._state.http.get_audit_logs
@@ -496,51 +494,28 @@ def __init__(
496494
self.after = after or OLDEST_OBJECT
497495
self._users = {}
498496
self._state = guild._state
499-
500-
self._filter = None # entry dict -> bool
501-
502497
self.entries = asyncio.Queue()
503498

504-
if self.reverse:
505-
self._strategy = self._after_strategy
506-
if self.before:
507-
self._filter = lambda m: int(m["id"]) < self.before.id
508-
else:
509-
self._strategy = self._before_strategy
510-
if self.after and self.after != OLDEST_OBJECT:
511-
self._filter = lambda m: int(m["id"]) > self.after.id
512-
513-
async def _before_strategy(self, retrieve):
499+
async def _retrieve_entries(self, retrieve):
514500
before = self.before.id if self.before else None
515-
data: AuditLogPayload = await self.request(
516-
self.guild.id,
517-
limit=retrieve,
518-
user_id=self.user_id,
519-
action_type=self.action_type,
520-
before=before,
521-
)
522-
523-
entries = data.get("audit_log_entries", [])
524-
if len(data) and entries:
525-
if self.limit is not None:
526-
self.limit -= retrieve
527-
self.before = Object(id=int(entries[-1]["id"]))
528-
return data.get("users", []), entries
529-
530-
async def _after_strategy(self, retrieve):
531501
after = self.after.id if self.after else None
532502
data: AuditLogPayload = await self.request(
533503
self.guild.id,
534504
limit=retrieve,
535505
user_id=self.user_id,
536506
action_type=self.action_type,
507+
before=before,
537508
after=after,
538509
)
510+
539511
entries = data.get("audit_log_entries", [])
540512
if len(data) and entries:
541513
if self.limit is not None:
542514
self.limit -= retrieve
543-
self.after = Object(id=int(entries[0]["id"]))
515+
if self.before or not self.after:
516+
self.before = Object(id=int(entries[-1]["id"]))
517+
if self.after or not self.before:
518+
self.after = Object(id=int(entries[0]["id"]))
544519
return data.get("users", []), entries
545520

546521
async def next(self) -> AuditLogEntry:
@@ -553,36 +528,23 @@ async def next(self) -> AuditLogEntry:
553528
raise NoMoreItems()
554529

555530
def _get_retrieve(self):
556-
l = self.limit
557-
if l is None or l > 100:
558-
r = 100
559-
else:
560-
r = l
561-
self.retrieve = r
562-
return r > 0
531+
limit = self.limit or 100
532+
self.retrieve = min(limit, 100)
533+
return self.retrieve > 0
563534

564535
async def _fill(self):
565536
from .user import User
566537

567538
if self._get_retrieve():
568-
users, data = await self._strategy(self.retrieve)
539+
users, data = await self._retrieve_entries(self.retrieve)
569540
if len(data) < 100:
570541
self.limit = 0 # terminate the infinite loop
571542

572-
if self.reverse:
573-
data = reversed(data)
574-
if self._filter:
575-
data = filter(self._filter, data)
576-
577543
for user in users:
578544
u = User(data=user, state=self._state)
579545
self._users[u.id] = u
580546

581547
for element in data:
582-
# TODO: remove this if statement later
583-
if element["action_type"] is None:
584-
continue
585-
586548
await self.entries.put(
587549
AuditLogEntry(data=element, users=self._users, guild=self.guild)
588550
)

0 commit comments

Comments
 (0)