Repository: hikari-py/hikari
Pull request #395 (2020-12-16 14:13:25)

Add source_guild and source_channel to Webhook
### Summary
Add source_guild and source_channel to Webhook

### Checklist
<!-- Make sure to tick all the following boxes by putting an `x` in between (like this `[x]`) -->
- [x] I have run `nox` and all the pipelines have passed.
- [x] 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)
-->


davfsa: https://github.com/discord/discord-api-docs/pull/1837

Patch:
diff --git a/hikari/impl/entity_factory.py b/hikari/impl/entity_factory.py
index 10535829..e7d86856 100644
--- a/hikari/impl/entity_factory.py
+++ b/hikari/impl/entity_factory.py
@@ -2041,6 +2041,24 @@ def deserialize_webhook(self, payload: data_binding.JSONObject) -> webhook_model
         if (raw_application_id := payload.get("application_id")) is not None:
             application_id = snowflakes.Snowflake(raw_application_id)
 
+        source_channel: typing.Optional[channel_models.PartialChannel] = None
+        if "source_channel" in payload:
+            raw_source_channel = payload["source_channel"]
+            # In this case the channel type isn't provided as we can safely
+            # assume it's a news channel.
+            raw_source_channel["type"] = channel_models.ChannelType.GUILD_NEWS
+            source_channel = self.deserialize_partial_channel(raw_source_channel)
+
+        source_guild: typing.Optional[guild_models.PartialGuild] = None
+        if "source_guild" in payload:
+            source_guild_payload = payload["source_guild"]
+            source_guild = guild_models.PartialGuild(
+                app=self._app,
+                id=snowflakes.Snowflake(source_guild_payload["id"]),
+                name=source_guild_payload["name"],
+                icon_hash=source_guild_payload["icon"],
+            )
+
         return webhook_models.Webhook(
             app=self._app,
             id=snowflakes.Snowflake(payload["id"]),
@@ -2052,4 +2070,6 @@ def deserialize_webhook(self, payload: data_binding.JSONObject) -> webhook_model
             avatar_hash=payload["avatar"],
             token=payload.get("token"),
             application_id=application_id,
+            source_channel=source_channel,
+            source_guild=source_guild,
         )
diff --git a/hikari/webhooks.py b/hikari/webhooks.py
index d32e71f5..406e6e5a 100644
--- a/hikari/webhooks.py
+++ b/hikari/webhooks.py
@@ -107,6 +107,18 @@ class Webhook(snowflakes.Unique):
     application_id: typing.Optional[snowflakes.Snowflake] = attr.ib(eq=False, hash=False, repr=False)
     """The ID of the application that created this webhook."""
 
+    source_channel: typing.Optional[channels_.PartialChannel] = attr.ib(eq=False, hash=False, repr=True)
+    """The partial object of the channel a `CHANNEL_FOLLOWER` webhook is following.
+
+    Will be `builtins.None` for other webhook types.
+    """
+
+    source_guild: typing.Optional[guilds_.PartialGuild] = attr.ib(eq=False, hash=False, repr=True)
+    """The partial object of the guild a `CHANNEL_FOLLOWER` webhook is following.
+
+    Will be `builtins.None` for other webhook types.
+    """
+
     def __str__(self) -> str:
         return self.name if self.name is not None else f"Unnamed webhook ID {self.id}"
 


Test patch:
diff --git a/tests/hikari/impl/test_entity_factory.py b/tests/hikari/impl/test_entity_factory.py
index 6c25912d..66ae8ef5 100644
--- a/tests/hikari/impl/test_entity_factory.py
+++ b/tests/hikari/impl/test_entity_factory.py
@@ -434,6 +434,8 @@ def webhook_payload(self, user_payload):
             "avatar": "bb71f469c158984e265093a81b3397fb",
             "token": "ueoqrialsdfaKJLKfajslkdf",
             "application_id": "1234567890",
+            "source_channel": {"id": "381871767846780666", "name": "announcements"},
+            "source_guild": {"id": "574921006817476608", "name": "Hikari API", "icon": "a363a8443211231232fdfb2e50e6"},
         }
 
     @pytest.fixture()
@@ -3305,6 +3307,16 @@ def test_deserialize_webhook(self, entity_factory_impl, webhook_payload, user_pa
         assert webhook.avatar_hash == "bb71f469c158984e265093a81b3397fb"
         assert webhook.token == "ueoqrialsdfaKJLKfajslkdf"
         assert webhook.application_id == 1234567890
+
+        assert webhook.source_channel == entity_factory_impl.deserialize_partial_channel(
+            {"id": "381871767846780666", "name": "announcements", "type": 5}
+        )
+
+        assert webhook.source_guild.id == 574921006817476608
+        assert webhook.source_guild.name == "Hikari API"
+        assert webhook.source_guild.icon_hash == "a363a8443211231232fdfb2e50e6"
+        assert isinstance(webhook.source_guild, guild_models.PartialGuild)
+
         assert isinstance(webhook, webhook_models.Webhook)
 
     def test_deserialize_webhook_with_null_and_unset_fields(self, entity_factory_impl):
@@ -3317,3 +3329,5 @@ def test_deserialize_webhook_with_null_and_unset_fields(self, entity_factory_imp
         assert webhook.avatar_hash is None
         assert webhook.token is None
         assert webhook.application_id is None
+        assert webhook.source_channel is None
+        assert webhook.source_guild is None
diff --git a/tests/hikari/test_webhooks.py b/tests/hikari/test_webhooks.py
index c28399b2..7c127e68 100644
--- a/tests/hikari/test_webhooks.py
+++ b/tests/hikari/test_webhooks.py
@@ -43,6 +43,8 @@ def webhook(self):
             avatar_hash=None,
             token="abc123bca",
             application_id=None,
+            source_channel=object(),
+            source_guild=object(),
         )
 
     def test_str(self, webhook):