From 3a1b1558ae33ca0224e210957bbc300a8d73e2c8 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 29 Feb 2024 01:10:06 +0100 Subject: [PATCH 1/8] fix: either i'm high and it won't work, or i'm god and this works --- discord/guild.py | 8 +++----- discord/iterators.py | 38 ++++++++------------------------------ 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index f1bbe14aa2..1f1d9d75e8 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -3266,7 +3266,6 @@ def audit_logs( limit: int | None = 100, before: SnowflakeTime | None = None, after: SnowflakeTime | None = None, - oldest_first: bool | None = None, user: Snowflake = None, action: AuditLogAction = None, ) -> AuditLogIterator: @@ -3274,6 +3273,9 @@ def audit_logs( You must have the :attr:`~Permissions.view_audit_log` permission to use this. + See `gateway `_ + for more information about the `before` and `after` parameters. + Parameters ---------- limit: Optional[:class:`int`] @@ -3286,9 +3288,6 @@ def audit_logs( Retrieve entries after this date or entry. If a datetime is provided, it is recommended to use a UTC aware datetime. If the datetime is naive, it is assumed to be local time. - oldest_first: :class:`bool` - If set to ``True``, return entries in oldest->newest order. Defaults to ``True`` if - ``after`` is specified, otherwise ``False``. user: :class:`abc.Snowflake` The moderator to filter entries from. action: :class:`AuditLogAction` @@ -3333,7 +3332,6 @@ def audit_logs( before=before, after=after, limit=limit, - oldest_first=oldest_first, user_id=user_id, action_type=action, ) diff --git a/discord/iterators.py b/discord/iterators.py index 2f863ddb9d..dedbcf1ed1 100644 --- a/discord/iterators.py +++ b/discord/iterators.py @@ -476,7 +476,6 @@ def __init__( limit=None, before=None, after=None, - oldest_first=None, user_id=None, action_type=None, ): @@ -485,7 +484,6 @@ def __init__( if isinstance(after, datetime.datetime): after = Object(id=time_snowflake(after, high=True)) - self.reverse = after is not None if oldest_first is None else oldest_first self.guild = guild self.loop = guild._state.loop self.request = guild._state.http.get_audit_logs @@ -501,46 +499,28 @@ def __init__( self.entries = asyncio.Queue() - if self.reverse: - self._strategy = self._after_strategy - if self.before: - self._filter = lambda m: int(m["id"]) < self.before.id - else: - self._strategy = self._before_strategy - if self.after and self.after != OLDEST_OBJECT: - self._filter = lambda m: int(m["id"]) > self.after.id + self._strategy = self._strategy_exec - async def _before_strategy(self, retrieve): + async def _strategy_exec(self, retrieve): before = self.before.id if self.before else None - data: AuditLogPayload = await self.request( - self.guild.id, - limit=retrieve, - user_id=self.user_id, - action_type=self.action_type, - before=before, - ) - - entries = data.get("audit_log_entries", []) - if len(data) and entries: - if self.limit is not None: - self.limit -= retrieve - self.before = Object(id=int(entries[-1]["id"])) - return data.get("users", []), entries - - async def _after_strategy(self, retrieve): after = self.after.id if self.after else None data: AuditLogPayload = await self.request( self.guild.id, limit=retrieve, user_id=self.user_id, action_type=self.action_type, + before=before, after=after, ) + entries = data.get("audit_log_entries", []) if len(data) and entries: if self.limit is not None: self.limit -= retrieve - self.after = Object(id=int(entries[0]["id"])) + if self.before or not self.after: + self.before = Object(id=int(entries[-1]["id"])) + if self.after or not self.before: + self.after = Object(id=int(entries[0]["id"])) return data.get("users", []), entries async def next(self) -> AuditLogEntry: @@ -569,8 +549,6 @@ async def _fill(self): if len(data) < 100: self.limit = 0 # terminate the infinite loop - if self.reverse: - data = reversed(data) if self._filter: data = filter(self._filter, data) From 456c1d497d562a87d497b74de6c2102f64ce62de Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 29 Feb 2024 02:12:51 +0100 Subject: [PATCH 2/8] Update iterators.py --- discord/iterators.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/discord/iterators.py b/discord/iterators.py index dedbcf1ed1..8a1a5d74c3 100644 --- a/discord/iterators.py +++ b/discord/iterators.py @@ -557,10 +557,6 @@ async def _fill(self): self._users[u.id] = u for element in data: - # TODO: remove this if statement later - if element["action_type"] is None: - continue - await self.entries.put( AuditLogEntry(data=element, users=self._users, guild=self.guild) ) From 9e0cb93c43633a94c1a49bbdc33774549bc50fdd Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Thu, 29 Feb 2024 20:12:08 +0100 Subject: [PATCH 3/8] Update iterators.py Signed-off-by: Lala Sabathil --- discord/iterators.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/discord/iterators.py b/discord/iterators.py index 8a1a5d74c3..50e2e0bb99 100644 --- a/discord/iterators.py +++ b/discord/iterators.py @@ -494,14 +494,9 @@ def __init__( self.after = after or OLDEST_OBJECT self._users = {} self._state = guild._state - - self._filter = None # entry dict -> bool - self.entries = asyncio.Queue() - self._strategy = self._strategy_exec - - async def _strategy_exec(self, retrieve): + async def _retrieve_auditlogs(self, retrieve): before = self.before.id if self.before else None after = self.after.id if self.after else None data: AuditLogPayload = await self.request( @@ -545,13 +540,10 @@ async def _fill(self): from .user import User if self._get_retrieve(): - users, data = await self._strategy(self.retrieve) + users, data = await self._retrieve_auditlogs(self.retrieve) if len(data) < 100: self.limit = 0 # terminate the infinite loop - if self._filter: - data = filter(self._filter, data) - for user in users: u = User(data=user, state=self._state) self._users[u.id] = u From 614dc7df4aefda00d8e253177602a93719782ecf Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:16:55 +0300 Subject: [PATCH 4/8] chore: clean code Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- discord/iterators.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/discord/iterators.py b/discord/iterators.py index 50e2e0bb99..b43dd53654 100644 --- a/discord/iterators.py +++ b/discord/iterators.py @@ -496,7 +496,7 @@ def __init__( self._state = guild._state self.entries = asyncio.Queue() - async def _retrieve_auditlogs(self, retrieve): + async def _retrieve_entries(self, retrieve): before = self.before.id if self.before else None after = self.after.id if self.after else None data: AuditLogPayload = await self.request( @@ -528,19 +528,15 @@ async def next(self) -> AuditLogEntry: raise NoMoreItems() def _get_retrieve(self): - l = self.limit - if l is None or l > 100: - r = 100 - else: - r = l - self.retrieve = r - return r > 0 + limit = self.limit or 100 + self.retrieve = min(limit, 100) + return self.retrieve > 0 async def _fill(self): from .user import User if self._get_retrieve(): - users, data = await self._retrieve_auditlogs(self.retrieve) + users, data = await self._retrieve_entries(self.retrieve) if len(data) < 100: self.limit = 0 # terminate the infinite loop From a9d879099af3471797a7159121f16fb841707d22 Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:19:02 +0300 Subject: [PATCH 5/8] chore: add changelog entry Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ec3411622..e8e236939f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,8 @@ These changes are available on the `master` branch, but have not yet been releas to `None`. ([#2464](https://github.com/Pycord-Development/pycord/pull/2464)) - Fixed `remove_application_command` causing issues while reloading extensions. ([#2480](https://github.com/Pycord-Development/pycord/pull/2480)) +- Fixed outdated logic for filtering and sorting audit log entries. + ([#2371](https://github.com/Pycord-Development/pycord/pull/2371)) ### Changed From 0ad42ed5a81d404de16eb0dba7c6b6dfd668bceb Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:24:12 +0300 Subject: [PATCH 6/8] chore: add changelog entry Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8e236939f..ccf2d2174f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,9 @@ These changes are available on the `master` branch, but have not yet been releas - Removed the `delete_message_days` parameter from ban methods. Please use `delete_message_seconds` instead. ([#2421](https://github.com/Pycord-Development/pycord/pull/2421)) +- Removed the `oldest_first` parameter from `Guild.audit_logs` in favor of + the `before` and `after` parameters. + ([#2371](https://github.com/Pycord-Development/pycord/pull/2371)) ## [2.5.0] - 2024-03-02 From ceb4ac1bff462bba6a5b40f78865443bad6664ab Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 5 Jul 2024 18:24:32 +0000 Subject: [PATCH 7/8] style(pre-commit): auto fixes from pre-commit.com hooks --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccf2d2174f..d148e47968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,8 +105,8 @@ These changes are available on the `master` branch, but have not yet been releas - Removed the `delete_message_days` parameter from ban methods. Please use `delete_message_seconds` instead. ([#2421](https://github.com/Pycord-Development/pycord/pull/2421)) -- Removed the `oldest_first` parameter from `Guild.audit_logs` in favor of - the `before` and `after` parameters. +- Removed the `oldest_first` parameter from `Guild.audit_logs` in favor of the `before` + and `after` parameters. ([#2371](https://github.com/Pycord-Development/pycord/pull/2371)) ## [2.5.0] - 2024-03-02 From fb15e49399a9f158bbb06c500b3959edf06660b6 Mon Sep 17 00:00:00 2001 From: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:26:19 +0300 Subject: [PATCH 8/8] Update guild.py Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- discord/guild.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/guild.py b/discord/guild.py index 1f1d9d75e8..b92ffefb19 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -3273,7 +3273,7 @@ def audit_logs( You must have the :attr:`~Permissions.view_audit_log` permission to use this. - See `gateway `_ + See `API documentation `_ for more information about the `before` and `after` parameters. Parameters