diff options
| author | Rapptz <[email protected]> | 2017-01-11 21:20:28 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2017-01-11 21:20:28 -0500 |
| commit | ae6fb54b1b926463166cb248d6b399dd38ea1e6e (patch) | |
| tree | 9c7cbd09d710ef73c0f4c55714971170b628e0ea /discord/reaction.py | |
| parent | Fix NameError inside Messageable.get_message (diff) | |
| download | discord.py-ae6fb54b1b926463166cb248d6b399dd38ea1e6e.tar.xz discord.py-ae6fb54b1b926463166cb248d6b399dd38ea1e6e.zip | |
Make Reaction.users return an async iterator.
Diffstat (limited to 'discord/reaction.py')
| -rw-r--r-- | discord/reaction.py | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/discord/reaction.py b/discord/reaction.py index d604e26c..4ec2fd75 100644 --- a/discord/reaction.py +++ b/discord/reaction.py @@ -26,7 +26,7 @@ DEALINGS IN THE SOFTWARE. import asyncio -from .user import User +from .iterators import ReactionIterator class Reaction: """Represents a reaction to a message. @@ -86,11 +86,11 @@ class Reaction: def __repr__(self): return '<Reaction emoji={0.emoji!r} me={0.me} count={0.count}>'.format(self) - @asyncio.coroutine - def users(self, limit=100, after=None): + def users(self, limit=None, after=None): """|coro| - Get the users that added this reaction. + Returns an asynchronous iterator representing the + users that have reacted to the message. The ``after`` parameter must represent a member and meet the :class:`abc.Snowflake` abc. @@ -99,6 +99,8 @@ class Reaction: ------------ limit: int The maximum number of results to return. + If not provided, returns all the users who + reacted to the message. after: :class:`abc.Snowflake` For pagination, reactions are sorted by member. @@ -107,23 +109,48 @@ class Reaction: HTTPException Getting the users for the reaction failed. - Returns + Examples + --------- + + Usage :: + + # I do not actually recommend doing this. + async for user in reaction.users(): + await channel.send('{0} has reacted with {1.emoji}!'.format(user, reaction)) + + Flattening into a list: :: + + users = await reaction.users().flatten() + # users is now a list... + winner = random.choice(users) + await channel.send('{} has won the raffle.'.format(winner)) + + Python 3.4 Usage :: + + iterator = reaction.users() + while True: + try: + user = yield from iterator.get() + except discord.NoMoreItems: + break + else: + await channel.send('{0} has reacted with {1.emoji}!'.format(user, reaction)) + + Yields -------- - List[:class:`User`] - A list of users who reacted to the message. + Union[:class:`User`, :class:`Member`] + The member (if retrievable) or the user that has reacted + to this message. The case where it can be a :class:`Member` is + in a guild message context. Sometimes it can be a :class:`User` + if the member has left the guild. """ - # TODO: Return an iterator a la `Messageable.history`? - if self.custom_emoji: emoji = '{0.name}:{0.id}'.format(self.emoji) else: emoji = self.emoji - if after: - after = after.id + if limit is None: + limit = self.count - msg = self.message - state = msg._state - data = yield from state.http.get_reaction_users(msg.id, msg.channel.id, emoji, limit, after=after) - return [User(state=state, data=user) for user in data] + return ReactionIterator(self.message, emoji, limit, after) |