Browse Source

simplify AuditLogIterator ordering

rename reverse to oldest_first and remove cases where messages would
be ordered differently from the order we fetch audit log entries,
similar to HistoryIterator
pull/2048/head
khazhyk 6 years ago
parent
commit
52c45da0bc
  1. 12
      discord/guild.py
  2. 33
      discord/iterators.py

12
discord/guild.py

@ -1437,7 +1437,7 @@ class Guild(Hashable):
raise ClientException('Must not be a bot account to ack messages.')
return state.http.ack_guild(self.id)
def audit_logs(self, *, limit=100, before=None, after=None, reverse=None, user=None, action=None):
def audit_logs(self, *, limit=100, before=None, after=None, oldest_first=None, user=None, action=None):
"""Return an :class:`AsyncIterator` that enables receiving the guild's audit logs.
You must have the :attr:`~Permissions.view_audit_log` permission to use this.
@ -1470,11 +1470,9 @@ class Guild(Hashable):
after: Union[:class:`abc.Snowflake`, datetime]
Retrieve entries after this date or entry.
If a date is provided it must be a timezone-naive datetime representing UTC time.
reverse: :class:`bool`
If set to true, return entries in oldest->newest order. If unspecified,
this defaults to ``False`` for most cases. However if passing in a
``after`` parameter then this is set to ``True``. This avoids getting entries
out of order in the ``after`` case.
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`
@ -1499,7 +1497,7 @@ class Guild(Hashable):
action = action.value
return AuditLogIterator(self, before=before, after=after, limit=limit,
reverse=reverse, user_id=user, action_type=action)
oldest_first=oldest_first, user_id=user, action_type=action)
async def widget(self):
"""|coro|

33
discord/iterators.py

@ -32,7 +32,7 @@ from .utils import DISCORD_EPOCH, time_snowflake, maybe_coroutine
from .object import Object
from .audit_logs import AuditLogEntry
OLDEST_MESSAGE = Object(id=0)
OLDEST_OBJECT = Object(id=0)
class _AsyncIterator:
__slots__ = ()
@ -221,7 +221,7 @@ class HistoryIterator(_AsyncIterator):
self.messageable = messageable
self.limit = limit
self.before = before
self.after = after or OLDEST_MESSAGE
self.after = after or OLDEST_OBJECT
self.around = around
self._filter = None # message dict -> bool
@ -254,7 +254,7 @@ class HistoryIterator(_AsyncIterator):
self._filter = lambda m: int(m['id']) < self.before.id
else:
self._retrieve_messages = self._retrieve_messages_before_strategy
if (self.after and self.after != OLDEST_MESSAGE):
if (self.after and self.after != OLDEST_OBJECT):
self._filter = lambda m: int(m['id']) > self.after.id
async def next(self):
@ -352,13 +352,18 @@ class HistoryIterator(_AsyncIterator):
return []
class AuditLogIterator(_AsyncIterator):
def __init__(self, guild, limit=None, before=None, after=None, reverse=None, user_id=None, action_type=None):
def __init__(self, guild, limit=None, before=None, after=None, oldest_first=None, user_id=None, action_type=None):
if isinstance(before, datetime.datetime):
before = Object(id=time_snowflake(before, high=False))
if isinstance(after, datetime.datetime):
after = Object(id=time_snowflake(after, high=True))
if oldest_first is None:
self.reverse = after is not None
else:
self.reverse = oldest_first
self.guild = guild
self.loop = guild._state.loop
self.request = guild._state.http.get_audit_logs
@ -366,30 +371,24 @@ class AuditLogIterator(_AsyncIterator):
self.before = before
self.user_id = user_id
self.action_type = action_type
self.after = after
self.after = OLDEST_OBJECT
self._users = {}
self._state = guild._state
if reverse is None:
self.reverse = after is not None
else:
self.reverse = reverse
self._filter = None # entry dict -> bool
self.entries = asyncio.Queue(loop=self.loop)
if self.before and self.after:
if self.reverse:
self._strategy = self._after_strategy
self._filter = lambda m: int(m['id']) < self.before.id
else:
self._strategy = self._before_strategy
self._filter = lambda m: int(m['id']) > self.after.id
elif self.after:
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
async def _before_strategy(self, retrieve):
before = self.before.id if self.before else None

Loading…
Cancel
Save