diff options
| author | bmintz <[email protected]> | 2018-07-30 18:31:28 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2018-07-31 13:36:54 -0400 |
| commit | 0c446398d18fb63cc4fbc2c634640cc278dc95c4 (patch) | |
| tree | 79c903274844d34e9e8dcde26610864f59e31cbe | |
| parent | Fix dumb typo in the warning. (diff) | |
| download | discord.py-0c446398d18fb63cc4fbc2c634640cc278dc95c4.tar.xz discord.py-0c446398d18fb63cc4fbc2c634640cc278dc95c4.zip | |
message: de-duplicate reaction type conversion
Removes some duplicate code in Message.{add,remove}_reaction.
The code in question converts the emoji object from Reaction, Emoji, str, or PartialEmoji
to a string form suitable for sending over the wire.
| -rw-r--r-- | discord/message.py | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/discord/message.py b/discord/message.py index c77e7378..9700fe37 100644 --- a/discord/message.py +++ b/discord/message.py @@ -675,18 +675,7 @@ class Message: The emoji parameter is invalid. """ - if isinstance(emoji, Reaction): - emoji = emoji.emoji - - if isinstance(emoji, Emoji): - emoji = '%s:%s' % (emoji.name, emoji.id) - elif isinstance(emoji, PartialEmoji): - emoji = emoji._as_reaction() - elif isinstance(emoji, str): - pass # this is okay - else: - raise InvalidArgument('emoji argument must be str, Emoji, or Reaction not {.__class__.__name__}.'.format(emoji)) - + emoji = self._emoji_reaction(emoji) await self._state.http.add_reaction(self.id, self.channel.id, emoji) async def remove_reaction(self, emoji, member): @@ -721,23 +710,26 @@ class Message: The emoji parameter is invalid. """ - if isinstance(emoji, Reaction): - emoji = emoji.emoji - - if isinstance(emoji, Emoji): - emoji = '%s:%s' % (emoji.name, emoji.id) - elif isinstance(emoji, PartialEmoji): - emoji = emoji._as_reaction() - elif isinstance(emoji, str): - pass # this is okay - else: - raise InvalidArgument('emoji argument must be str, Emoji, or Reaction not {.__class__.__name__}.'.format(emoji)) + emoji = self._emoji_reaction(emoji) if member.id == self._state.self_id: await self._state.http.remove_own_reaction(self.id, self.channel.id, emoji) else: await self._state.http.remove_reaction(self.id, self.channel.id, emoji, member.id) + @staticmethod + def _emoji_reaction(emoji): + if isinstance(emoji, Reaction): + return emoji.emoji + if isinstance(emoji, Emoji): + return '%s:%s' % (emoji.name, emoji.id) + if isinstance(emoji, PartialEmoji): + return emoji._as_reaction() + if isinstance(emoji, str): + return emoji # this is okay + + raise InvalidArgument('emoji argument must be str, Emoji, or Reaction not {.__class__.__name__}.'.format(emoji)) + async def clear_reactions(self): """|coro| |