diff options
| author | Rapptz <[email protected]> | 2016-01-14 15:42:56 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2016-01-14 15:42:56 -0500 |
| commit | d33d0bed69a1bb29e5c9bba434cb5e32536da91a (patch) | |
| tree | a00b146de8b5f412cd9729b3f163babea1fbba41 /discord/client.py | |
| parent | [commands] Add on_command and on_command_completion events. (diff) | |
| download | discord.py-d33d0bed69a1bb29e5c9bba434cb5e32536da91a.tar.xz discord.py-d33d0bed69a1bb29e5c9bba434cb5e32536da91a.zip | |
Add support for `async for` in Client.logs_from.
This is a breaking change. Code will still work in Python 3.4 as-is but
if you use Python 3.5 you will have to change your code to the new
`async for` syntax as the older version is not supported in 3.5.
On the other hand, this comes with performance improvements if you use
Python 3.5 as it will lazily load 100 message chunks on an as needed
basis rather than loading all messages in one go.
Diffstat (limited to 'discord/client.py')
| -rw-r--r-- | discord/client.py | 80 |
1 files changed, 47 insertions, 33 deletions
diff --git a/discord/client.py b/discord/client.py index 7e49aa89..98f62a44 100644 --- a/discord/client.py +++ b/discord/client.py @@ -41,6 +41,7 @@ from .permissions import Permissions from . import utils from .enums import ChannelType, ServerRegion from .voice_client import VoiceClient +from .iterators import LogsFromIterator import asyncio import aiohttp @@ -53,6 +54,7 @@ import itertools import zlib from random import randint as random_integer +PY35 = sys.version_info >= (3, 5) log = logging.getLogger(__name__) request_logging_format = '{method} {response.url} has returned {response.status}' request_success_log = '{response.url} with {json} received {data}' @@ -1115,24 +1117,6 @@ class Client: @asyncio.coroutine def _logs_from(self, channel, limit=100, before=None, after=None): - url = '{}/{}/messages'.format(endpoints.CHANNELS, channel.id) - params = { - 'limit': limit - } - - if before: - params['before'] = before.id - if after: - params['after'] = after.id - - response = yield from aiohttp.get(url, params=params, headers=self.headers, loop=self.loop) - log.debug(request_logging_format.format(method='GET', response=response)) - yield from utils._verify_successful_response(response) - messages = yield from response.json() - return messages - - @asyncio.coroutine - def logs_from(self, channel, limit=100, *, before=None, after=None): """|coro| This coroutine returns a generator that obtains logs from a specified channel. @@ -1172,24 +1156,54 @@ class Client: if message.content.startswith('!hello'): if message.author == client.user: yield from client.edit_message(message, 'goodbye') + + Python 3.5 Usage :: + + counter = 0 + async for message in client.logs_from(channel, limit=500): + if message.author == client.user: + counter += 1 """ + url = '{}/{}/messages'.format(endpoints.CHANNELS, channel.id) + params = { + 'limit': limit + } - def generator(data): - for message in data: - yield Message(channel=channel, **message) - - result = [] - while limit > 0: - retrieve = limit if limit <= 100 else 100 - data = yield from self._logs_from(channel, retrieve, before, after) - if len(data): - limit -= retrieve - result.extend(data) - before = Object(id=data[-1]['id']) - else: - break + if before: + params['before'] = before.id + if after: + params['after'] = after.id + + response = yield from aiohttp.get(url, params=params, headers=self.headers, loop=self.loop) + log.debug(request_logging_format.format(method='GET', response=response)) + yield from utils._verify_successful_response(response) + messages = yield from response.json() + return messages + + if PY35: + def logs_from(self, channel, limit=100, *, before=None, after=None): + return LogsFromIterator(self, channel, limit, before, after) + else: + @asyncio.coroutine + def logs_from(self, channel, limit=100, *, before=None, after=None): + def generator(data): + for message in data: + yield Message(channel=channel, **message) + + result = [] + while limit > 0: + retrieve = limit if limit <= 100 else 100 + data = yield from self._logs_from(channel, retrieve, before, after) + if len(data): + limit -= retrieve + result.extend(data) + before = Object(id=data[-1]['id']) + else: + break + + return generator(result) - return generator(result) + logs_from.__doc__ = _logs_from.__doc__ # Member management |