Repository: hikari-py/hikari
Pull request #2058 (2024-09-24 17:23:03)

Add audit log reason to channel deletion
### Summary
This PR adds the optional `reason` argument for the audit log to `hikari.api.rest.RESTClient.delete_channel` and `hikari.impl.rest.RESTClientImpl.delete_channel`, the same way it already exists for e.g. `edit_channel`

### Checklist
- [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
n/a, I brought this up on Discord earlier today and proceeded to fix it, as I was told it was likely an oversight

lukasthaler: Added the changelog fragment, but unsure what to do about the failing tests on unix and macOS - can't reproduce these on my Windows machine, and from what I can tell based on the reported error, the failure has nothing to do with my code changes
davfsa: Hey!

Thanks for the pull request!

The test failures are completely unrelated to this PR. They seem to arise from a change downstream in aiohttp. I will fix them shortly and then get this merged!

Patch:
diff --git a/changes/2058.feature.md b/changes/2058.feature.md
new file mode 100644
index 00000000..c51bf399
--- /dev/null
+++ b/changes/2058.feature.md
@@ -0,0 +1,3 @@
+Add the optional audit log `reason` argument to `hikari.api.rest.RESTClient.delete_channel` and
+`hikari.impl.rest.RESTClientImpl.delete_channel`, the same way it already exists for e.g.
+`edit_channel`
diff --git a/hikari/api/rest.py b/hikari/api/rest.py
index 537f5ab4..1c135a7e 100644
--- a/hikari/api/rest.py
+++ b/hikari/api/rest.py
@@ -372,7 +372,9 @@ async def follow_channel(
 
     @abc.abstractmethod
     async def delete_channel(
-        self, channel: snowflakes.SnowflakeishOr[channels_.PartialChannel]
+        self,
+        channel: snowflakes.SnowflakeishOr[channels_.PartialChannel],
+        reason: undefined.UndefinedOr[str] = undefined.UNDEFINED,
     ) -> channels_.PartialChannel:
         """Delete a channel in a guild, or close a DM.
 
@@ -385,6 +387,9 @@ async def delete_channel(
         channel
             The channel to delete. This may be the object or the ID of an
             existing channel.
+        reason
+            If provided, the reason that will be recorded in the audit logs.
+            Maximum of 512 characters.
 
         Returns
         -------
diff --git a/hikari/impl/rest.py b/hikari/impl/rest.py
index 154c9b8d..96a555f4 100644
--- a/hikari/impl/rest.py
+++ b/hikari/impl/rest.py
@@ -1142,10 +1142,12 @@ async def follow_channel(
         return self._entity_factory.deserialize_channel_follow(response)
 
     async def delete_channel(
-        self, channel: snowflakes.SnowflakeishOr[channels_.PartialChannel]
+        self,
+        channel: snowflakes.SnowflakeishOr[channels_.PartialChannel],
+        reason: undefined.UndefinedOr[str] = undefined.UNDEFINED,
     ) -> channels_.PartialChannel:
         route = routes.DELETE_CHANNEL.compile(channel=channel)
-        response = await self._request(route)
+        response = await self._request(route, reason=reason)
         assert isinstance(response, dict)
         return self._entity_factory.deserialize_channel(response)
 


Test patch:
diff --git a/tests/hikari/impl/test_rest.py b/tests/hikari/impl/test_rest.py
index d942c11e..684dd99e 100644
--- a/tests/hikari/impl/test_rest.py
+++ b/tests/hikari/impl/test_rest.py
@@ -2232,11 +2232,21 @@ async def test_delete_channel(self, rest_client):
         expected_route = routes.DELETE_CHANNEL.compile(channel=123)
         rest_client._request = mock.AsyncMock(return_value={"id": "NNNNN"})
 
+        result = await rest_client.delete_channel(StubModel(123), reason="some reason :)")
+
+        assert result is rest_client._entity_factory.deserialize_channel.return_value
+        rest_client._entity_factory.deserialize_channel.assert_called_once_with(rest_client._request.return_value)
+        rest_client._request.assert_awaited_once_with(expected_route, reason="some reason :)")
+
+    async def test_delete_channel_without_optionals(self, rest_client):
+        expected_route = routes.DELETE_CHANNEL.compile(channel=123)
+        rest_client._request = mock.AsyncMock(return_value={"id": "NNNNN"})
+
         result = await rest_client.delete_channel(StubModel(123))
 
         assert result is rest_client._entity_factory.deserialize_channel.return_value
         rest_client._entity_factory.deserialize_channel.assert_called_once_with(rest_client._request.return_value)
-        rest_client._request.assert_awaited_once_with(expected_route)
+        rest_client._request.assert_awaited_once_with(expected_route, reason=undefined.UNDEFINED)
 
     async def test_edit_my_voice_state_when_requesting_to_speak(self, rest_client):
         rest_client._request = mock.AsyncMock()