Browse Source

Make Client.wait_for_reaction return a namedtuple instead.

Also fix a bug in the case that emoji parameter is `None`.
pull/384/head
Rapptz 9 years ago
parent
commit
edcd1750c9
  1. 35
      discord/client.py

35
discord/client.py

@ -61,6 +61,8 @@ PY35 = sys.version_info >= (3, 5)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
AppInfo = namedtuple('AppInfo', 'id name description icon owner') AppInfo = namedtuple('AppInfo', 'id name description icon owner')
WaitedReaction = namedtuple('WaitedReaction', 'reaction user')
def app_info_icon_url(self): def app_info_icon_url(self):
"""Retrieves the application's icon_url if it exists. Empty string otherwise.""" """Retrieves the application's icon_url if it exists. Empty string otherwise."""
if not self.icon: if not self.icon:
@ -215,7 +217,7 @@ class Client:
removed.append(i) removed.append(i)
else: else:
if result: if result:
future.set_result((reaction, user)) future.set_result(WaitedReaction(reaction, user))
removed.append(i) removed.append(i)
@ -770,10 +772,24 @@ class Client:
async def on_message(message): async def on_message(message):
if message.content.startswith('$react'): if message.content.startswith('$react'):
msg = await client.send_message(message.channel, 'React with thumbs up or thumbs down.') msg = await client.send_message(message.channel, 'React with thumbs up or thumbs down.')
(reaction, user) = await client.wait_for_reaction(['\N{THUMBS UP SIGN}', res = await client.wait_for_reaction(['\N{THUMBS UP SIGN}', '\N{THUMBS DOWN SIGN}'], message=msg)
'\N{THUMBS DOWN SIGN}'], await client.send_message(message.channel, '{.user} reacted with {.reaction.emoji}!'.format(res))
message=msg)
await client.send_message(message.channel, '{} reacted with {.emoji}!'.format(user, reaction)) Checking for reaction emoji regardless of skin tone:
.. code-block:: python
@client.event
async def on_message(message):
if message.content.startswith('$react'):
msg = await client.send_message(message.channel, 'React with thumbs up or thumbs down.')
def check(reaction, user):
e = str(reaction.emoji)
return e.startswith(('\N{THUMBS UP SIGN}', '\N{THUMBS DOWN SIGN}'))
res = await client.wait_for_reaction(message=msg, check=check)
await client.send_message(message.channel, '{.user} reacted with {.reaction.emoji}!'.format(res))
Parameters Parameters
----------- -----------
@ -793,12 +809,13 @@ class Client:
Returns Returns
-------- --------
tuple namedtuple
A tuple of ``(reaction, user)`` similar to :func:`on_reaction_add`. A namedtuple with attributes ``reaction`` and ``user`` similar to :func:`on_reaction_add`.
""" """
emoji_check = lambda r: True if emoji is None:
if isinstance(emoji, (str, Emoji)): emoji_check = lambda r: True
elif isinstance(emoji, (str, Emoji)):
emoji_check = lambda r: r.emoji == emoji emoji_check = lambda r: r.emoji == emoji
else: else:
emoji_check = lambda r: r.emoji in emoji emoji_check = lambda r: r.emoji in emoji

Loading…
Cancel
Save