Repository: hikari-py/hikari
Pull request #770 (2021-09-03 03:32:03)

Add is_for_emoji methods to the relevant reaction events
### Summary
Add is_for_emoji methods to relevant reaction events, as suggested by @tandemdude 

### Checklist
<!-- Make sure to tick all the following boxes by putting an `x` in between (like this `[x]`) -->
- [ ] I have run `nox` and all the pipelines have passed.
- [ ] I have made unittests according to the code I have added/modified/deleted.

### Related issues
<!--
To mention an issue use `#issue-id` and to mention a merge request use `!merge-request-id`
To close/fix an issue use `Close #issue-id` or `Fix #issue-id` (depending on the merge request)
-->



Patch:
diff --git a/hikari/events/reaction_events.py b/hikari/events/reaction_events.py
index faf24bcf..2d438adf 100644
--- a/hikari/events/reaction_events.py
+++ b/hikari/events/reaction_events.py
@@ -47,13 +47,13 @@
 
 import attr
 
+from hikari import emojis
 from hikari import intents
 from hikari.events import base_events
 from hikari.events import shard_events
 from hikari.internal import attr_extensions
 
 if typing.TYPE_CHECKING:
-    from hikari import emojis
     from hikari import guilds
     from hikari import snowflakes
     from hikari import traits
@@ -170,6 +170,23 @@ def is_animated(self) -> bool:
             Whether the emoji which was added is animated.
         """
 
+    def is_for_emoji(self, emoji: typing.Union[emojis.Emoji, str], /) -> bool:
+        """Get whether the reaction event is for a specific emoji.
+
+        Parameters
+        ----------
+        emoji : typing.Union[hikari.emojis.Emoji, builtins.str]
+            The emoji to check.
+
+            Passing `builtins.str` here indicates a unicode emoji.
+
+        Returns
+        -------
+        builtins.bool
+            Whether the emoji is the one which was added.
+        """
+        return emoji.id == self.emoji_id if isinstance(emoji, emojis.CustomEmoji) else emoji == self.emoji_name
+
 
 @base_events.requires_intents(intents.Intents.GUILD_MESSAGE_REACTIONS, intents.Intents.DM_MESSAGE_REACTIONS)
 class ReactionDeleteEvent(ReactionEvent, abc.ABC):
@@ -216,6 +233,23 @@ def emoji_id(self) -> typing.Optional[snowflakes.Snowflake]:
             `builtins.None`.
         """
 
+    def is_for_emoji(self, emoji: typing.Union[emojis.Emoji, str], /) -> bool:
+        """Get whether the reaction event is for a specific emoji.
+
+        Parameters
+        ----------
+        emoji : typing.Union[hikari.emojis.Emoji, builtins.str]
+            The emoji to check.
+
+            Passing `builtins.str` here indicates a unicode emoji.
+
+        Returns
+        -------
+        builtins.bool
+            Whether the emoji is the one which was removed.
+        """
+        return emoji.id == self.emoji_id if isinstance(emoji, emojis.CustomEmoji) else emoji == self.emoji_name
+
 
 @base_events.requires_intents(intents.Intents.GUILD_MESSAGE_REACTIONS, intents.Intents.DM_MESSAGE_REACTIONS)
 class ReactionDeleteAllEvent(ReactionEvent, abc.ABC):
@@ -258,6 +292,23 @@ def emoji_id(self) -> typing.Optional[snowflakes.Snowflake]:
             `builtins.None`.
         """
 
+    def is_for_emoji(self, emoji: typing.Union[emojis.Emoji, str], /) -> bool:
+        """Get whether the reaction event is for a specific emoji.
+
+        Parameters
+        ----------
+        emoji : typing.Union[hikari.emojis.Emoji, builtins.str]
+            The emoji to check.
+
+            Passing `builtins.str` here indicates a unicode emoji.
+
+        Returns
+        -------
+        builtins.bool
+            Whether the emoji is the one which was removed.
+        """
+        return emoji.id == self.emoji_id if isinstance(emoji, emojis.CustomEmoji) else emoji == self.emoji_name
+
 
 @attr_extensions.with_copy
 @attr.define(kw_only=True, weakref_slot=False)
diff --git a/hikari/impl/rest.py b/hikari/impl/rest.py
index d83d00b4..6d4ae5f4 100644
--- a/hikari/impl/rest.py
+++ b/hikari/impl/rest.py
@@ -28,11 +28,7 @@
 
 from __future__ import annotations
 
-__all__: typing.List[str] = [
-    "ClientCredentialsStrategy",
-    "RESTApp",
-    "RESTClientImpl",
-]
+__all__: typing.List[str] = ["ClientCredentialsStrategy", "RESTApp", "RESTClientImpl"]
 
 import asyncio
 import base64
@@ -2777,10 +2773,8 @@ async def ban_user(
     ) -> None:
         body = data_binding.JSONObjectBuilder()
         body.put("delete_message_days", delete_message_days)
-        # This endpoint specifies a reason in the body, specifically.
-        body.put("reason", reason)
         route = routes.PUT_GUILD_BAN.compile(guild=guild, user=user)
-        await self._request(route, json=body)
+        await self._request(route, json=body, reason=reason)
 
     ban_member = ban_user
 


Test patch:
diff --git a/tests/hikari/events/test_reaction_events.py b/tests/hikari/events/test_reaction_events.py
index 121b198a..81b73046 100644
--- a/tests/hikari/events/test_reaction_events.py
+++ b/tests/hikari/events/test_reaction_events.py
@@ -23,8 +23,130 @@
 import mock
 import pytest
 
+from hikari import emojis
 from hikari import guilds
 from hikari.events import reaction_events
+from tests.hikari import hikari_test_helpers
+
+
+class TestReactionAddEvent:
+    def test_is_for_emoji_when_custom_emoji_matches(self):
+        event = hikari_test_helpers.mock_class_namespace(reaction_events.ReactionAddEvent, emoji_id=333333)()
+
+        assert event.is_for_emoji(emojis.CustomEmoji(id=333333, name=None, is_animated=True))
+
+    def test_is_for_emoji_when_unicode_emoji_matches(self):
+        event = hikari_test_helpers.mock_class_namespace(reaction_events.ReactionAddEvent, emoji_name="🌲")()
+
+        assert event.is_for_emoji(emojis.UnicodeEmoji("🌲"))
+
+    @pytest.mark.parametrize(
+        ("emoji_id", "emoji_name", "emoji"),
+        [
+            (None, "hi", emojis.CustomEmoji(name=None, id=54123, is_animated=False)),
+            (123321, None, emojis.UnicodeEmoji("no u")),
+        ],
+    )
+    def test_is_for_emoji_when_wrong_emoji_type(self, emoji_id, emoji_name, emoji):
+        event = hikari_test_helpers.mock_class_namespace(
+            reaction_events.ReactionAddEvent, emoji_id=emoji_id, emoji_name=emoji_name
+        )()
+
+        assert event.is_for_emoji(emoji) is False
+
+    @pytest.mark.parametrize(
+        ("emoji_id", "emoji_name", "emoji"),
+        [
+            (None, "hi", emojis.UnicodeEmoji("bye")),
+            (123321, None, emojis.CustomEmoji(id=123312123, name=None, is_animated=False)),
+        ],
+    )
+    def test_is_for_emoji_when_emoji_miss_match(self, emoji_id, emoji_name, emoji):
+        event = hikari_test_helpers.mock_class_namespace(
+            reaction_events.ReactionAddEvent, emoji_id=emoji_id, emoji_name=emoji_name
+        )()
+
+        assert event.is_for_emoji(emoji) is False
+
+
+class TestReactionDeleteEvent:
+    def test_is_for_emoji_when_custom_emoji_matches(self):
+        event = hikari_test_helpers.mock_class_namespace(reaction_events.ReactionAddEvent, emoji_id=333)()
+
+        assert event.is_for_emoji(emojis.CustomEmoji(id=333, name=None, is_animated=True))
+
+    def test_is_for_emoji_when_unicode_emoji_matches(self):
+        event = hikari_test_helpers.mock_class_namespace(reaction_events.ReactionAddEvent, emoji_name="e")()
+
+        assert event.is_for_emoji(emojis.UnicodeEmoji("e"))
+
+    @pytest.mark.parametrize(
+        ("emoji_id", "emoji_name", "emoji"),
+        [
+            (None, "hasdi", emojis.CustomEmoji(name=None, id=3123, is_animated=False)),
+            (534123, None, emojis.UnicodeEmoji("nodfgdu")),
+        ],
+    )
+    def test_is_for_emoji_when_wrong_emoji_type(self, emoji_id, emoji_name, emoji):
+        event = hikari_test_helpers.mock_class_namespace(
+            reaction_events.ReactionAddEvent, emoji_id=emoji_id, emoji_name=emoji_name
+        )()
+
+        assert event.is_for_emoji(emoji) is False
+
+    @pytest.mark.parametrize(
+        ("emoji_id", "emoji_name", "emoji"),
+        [
+            (None, "hfdasi", emojis.UnicodeEmoji("bgye")),
+            (54123, None, emojis.CustomEmoji(id=34123, name=None, is_animated=False)),
+        ],
+    )
+    def test_is_for_emoji_when_emoji_miss_match(self, emoji_id, emoji_name, emoji):
+        event = hikari_test_helpers.mock_class_namespace(
+            reaction_events.ReactionAddEvent, emoji_id=emoji_id, emoji_name=emoji_name
+        )()
+
+        assert event.is_for_emoji(emoji) is False
+
+
+class TestReactionDeleteEmojiEvent:
+    def test_is_for_emoji_when_custom_emoji_matches(self):
+        event = hikari_test_helpers.mock_class_namespace(reaction_events.ReactionAddEvent, emoji_id=332223333)()
+
+        assert event.is_for_emoji(emojis.CustomEmoji(id=332223333, name=None, is_animated=True))
+
+    def test_is_for_emoji_when_unicode_emoji_matches(self):
+        event = hikari_test_helpers.mock_class_namespace(reaction_events.ReactionAddEvent, emoji_name="🌲e")()
+
+        assert event.is_for_emoji(emojis.UnicodeEmoji("🌲e"))
+
+    @pytest.mark.parametrize(
+        ("emoji_id", "emoji_name", "emoji"),
+        [
+            (None, "heeei", emojis.CustomEmoji(name=None, id=541123, is_animated=False)),
+            (1233211, None, emojis.UnicodeEmoji("no eeeu")),
+        ],
+    )
+    def test_is_for_emoji_when_wrong_emoji_type(self, emoji_id, emoji_name, emoji):
+        event = hikari_test_helpers.mock_class_namespace(
+            reaction_events.ReactionAddEvent, emoji_id=emoji_id, emoji_name=emoji_name
+        )()
+
+        assert event.is_for_emoji(emoji) is False
+
+    @pytest.mark.parametrize(
+        ("emoji_id", "emoji_name", "emoji"),
+        [
+            (None, "dsahi", emojis.UnicodeEmoji("bye321")),
+            (12331231, None, emojis.CustomEmoji(id=121233312123, name=None, is_animated=False)),
+        ],
+    )
+    def test_is_for_emoji_when_emoji_miss_match(self, emoji_id, emoji_name, emoji):
+        event = hikari_test_helpers.mock_class_namespace(
+            reaction_events.ReactionAddEvent, emoji_id=emoji_id, emoji_name=emoji_name
+        )()
+
+        assert event.is_for_emoji(emoji) is False
 
 
 class TestGuildReactionAddEvent:
diff --git a/tests/hikari/impl/test_rest.py b/tests/hikari/impl/test_rest.py
index 43f915db..03cd56e1 100644
--- a/tests/hikari/impl/test_rest.py
+++ b/tests/hikari/impl/test_rest.py
@@ -2899,11 +2899,11 @@ async def test_kick_user(self, rest_client):
 
     async def test_ban_user(self, rest_client):
         expected_route = routes.PUT_GUILD_BAN.compile(guild=123, user=456)
-        expected_json = {"delete_message_days": 7, "reason": "because i can"}
+        expected_json = {"delete_message_days": 7}
         rest_client._request = mock.AsyncMock()
 
         await rest_client.ban_user(StubModel(123), StubModel(456), delete_message_days=7, reason="because i can")
-        rest_client._request.assert_awaited_once_with(expected_route, json=expected_json)
+        rest_client._request.assert_awaited_once_with(expected_route, json=expected_json, reason="because i can")
 
     async def test_unban_user(self, rest_client):
         expected_route = routes.DELETE_GUILD_BAN.compile(guild=123, user=456)