Repository: hikari-py/hikari
Pull request #1608 (2023-05-02 14:08:21)

Only return entries from the relevant guild from Guild.get methods
### Summary
<!-- Small summary of the merge request -->

### 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/changes/1608.bugfix.md b/changes/1608.bugfix.md
new file mode 100644
index 00000000..11faecb4
--- /dev/null
+++ b/changes/1608.bugfix.md
@@ -0,0 +1,1 @@
+`Guild.get_channel`, `Guild.get_emoji`, `Guild.get_sticker` and `Guild.get_role` now only return entries from the relevant guild.
diff --git a/hikari/guilds.py b/hikari/guilds.py
index 0ecd4e3e..09013e8e 100644
--- a/hikari/guilds.py
+++ b/hikari/guilds.py
@@ -2898,7 +2898,11 @@ def get_channel(
         if not isinstance(self.app, traits.CacheAware):
             return None
 
-        return self.app.cache.get_guild_channel(channel)
+        channel_ = self.app.cache.get_guild_channel(channel)
+        if channel_ and channel_.guild_id == self.id:
+            return channel_
+
+        return None
 
     def get_member(self, user: snowflakes.SnowflakeishOr[users.PartialUser]) -> typing.Optional[Member]:
         """Get a cached member that belongs to the guild by it's user ID or object.
@@ -2994,7 +2998,11 @@ def get_emoji(
         if not isinstance(self.app, traits.CacheAware):
             return None
 
-        return self.app.cache.get_emoji(emoji)
+        emoji_ = self.app.cache.get_emoji(emoji)
+        if emoji_ and emoji_.guild_id == self.id:
+            return emoji_
+
+        return None
 
     def get_sticker(
         self, sticker: snowflakes.SnowflakeishOr[stickers.GuildSticker]
@@ -3015,7 +3023,11 @@ def get_sticker(
         if not isinstance(self.app, traits.CacheAware):
             return None
 
-        return self.app.cache.get_sticker(sticker)
+        sticker_ = self.app.cache.get_sticker(sticker)
+        if sticker_ and sticker_.guild_id == self.id:
+            return sticker_
+
+        return None
 
     def get_role(self, role: snowflakes.SnowflakeishOr[PartialRole]) -> typing.Optional[Role]:
         """Get a cached role that belongs to the guild by it's ID or object.
@@ -3033,7 +3045,11 @@ def get_role(self, role: snowflakes.SnowflakeishOr[PartialRole]) -> typing.Optio
         if not isinstance(self.app, traits.CacheAware):
             return None
 
-        return self.app.cache.get_role(role)
+        role_ = self.app.cache.get_role(role)
+        if role_ and role_.guild_id == self.id:
+            return role_
+
+        return None
 
     async def fetch_owner(self) -> Member:
         """Fetch the owner of the guild.


Test patch:
diff --git a/tests/hikari/test_guilds.py b/tests/hikari/test_guilds.py
index 2c4d58f5..5142fa56 100644
--- a/tests/hikari/test_guilds.py
+++ b/tests/hikari/test_guilds.py
@@ -1021,9 +1021,17 @@ def test_emojis_when_no_cache_trait(self, model):
         assert model.get_emojis() == {}
 
     def test_get_sticker(self, model):
+        model.app.cache.get_sticker.return_value.guild_id = model.id
         assert model.get_sticker(456) is model.app.cache.get_sticker.return_value
         model.app.cache.get_sticker.assert_called_once_with(456)
 
+    def test_get_sticker_when_not_from_guild(self, model):
+        model.app.cache.get_sticker.return_value.guild_id = 546123123433
+
+        assert model.get_sticker(456) is None
+
+        model.app.cache.get_sticker.assert_called_once_with(456)
+
     def test_get_sticker_when_no_cache_trait(self, model):
         model.app = object()
         assert model.get_sticker(1234) is None
@@ -1045,17 +1053,33 @@ def test_get_roles_when_no_cache_trait(self, model):
         assert model.get_roles() == {}
 
     def test_get_emoji(self, model):
+        model.app.cache.get_emoji.return_value.guild_id = model.id
         assert model.get_emoji(456) is model.app.cache.get_emoji.return_value
         model.app.cache.get_emoji.assert_called_once_with(456)
 
+    def test_get_emoji_when_not_from_guild(self, model):
+        model.app.cache.get_emoji.return_value.guild_id = 1233212
+
+        assert model.get_emoji(456) is None
+
+        model.app.cache.get_emoji.assert_called_once_with(456)
+
     def test_get_emoji_when_no_cache_trait(self, model):
         model.app = object()
         assert model.get_emoji(456) is None
 
     def test_get_role(self, model):
+        model.app.cache.get_role.return_value.guild_id = model.id
         assert model.get_role(456) is model.app.cache.get_role.return_value
         model.app.cache.get_role.assert_called_once_with(456)
 
+    def test_get_role_when_not_from_guild(self, model):
+        model.app.cache.get_role.return_value.guild_id = 7623123321123
+
+        assert model.get_role(456) is None
+
+        model.app.cache.get_role.assert_called_once_with(456)
+
     def test_get_role_when_no_cache_trait(self, model):
         model.app = object()
         assert model.get_role(456) is None
@@ -1226,9 +1250,17 @@ async def test_fetch_afk_channel_when_None(self, model):
         assert await model.fetch_afk_channel() is None
 
     def test_get_channel(self, model):
+        model.app.cache.get_guild_channel.return_value.guild_id = model.id
         assert model.get_channel(456) is model.app.cache.get_guild_channel.return_value
         model.app.cache.get_guild_channel.assert_called_once_with(456)
 
+    def test_get_channel_when_not_from_guild(self, model):
+        model.app.cache.get_guild_channel.return_value.guild_id = 654523123
+
+        assert model.get_channel(456) is None
+
+        model.app.cache.get_guild_channel.assert_called_once_with(456)
+
     def test_get_channel_when_no_cache_trait(self, model):
         model.app = object()
         assert model.get_channel(456) is None