Repository: hikari-py/hikari
Pull request #1573 (2023-04-02 02:38:18)

Support Autocomplete response builder for interaction methods
### Summary
Just a small follow up to #1539

### 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/api/rest.py b/hikari/api/rest.py
index b2fb50ec..b6e4fc06 100644
--- a/hikari/api/rest.py
+++ b/hikari/api/rest.py
@@ -7292,6 +7292,7 @@ def interaction_autocomplete_builder(
         choices: typing.Union[
             typing.Sequence[commands.CommandChoice], typing.Sequence[special_endpoints.AutocompleteChoiceBuilder]
         ],
+        _stack_level: int = 0,
     ) -> special_endpoints.InteractionAutocompleteBuilder:
         """Create a builder for an autocomplete interaction response.
 
@@ -7674,6 +7675,7 @@ async def create_autocomplete_response(
         choices: typing.Union[
             typing.Sequence[commands.CommandChoice], typing.Sequence[special_endpoints.AutocompleteChoiceBuilder]
         ],
+        _stack_level: int = 0,
     ) -> None:
         """Create the initial response for an autocomplete interaction.
 
diff --git a/hikari/impl/rest.py b/hikari/impl/rest.py
index 9b819e50..a6c1818f 100644
--- a/hikari/impl/rest.py
+++ b/hikari/impl/rest.py
@@ -3948,8 +3948,9 @@ def interaction_autocomplete_builder(
         choices: typing.Union[
             typing.Sequence[commands.CommandChoice], typing.Sequence[special_endpoints.AutocompleteChoiceBuilder]
         ],
+        _stack_level: int = 0,
     ) -> special_endpoints.InteractionAutocompleteBuilder:
-        return special_endpoints_impl.InteractionAutocompleteBuilder(choices, _stack_level=1)
+        return special_endpoints_impl.InteractionAutocompleteBuilder(choices, _stack_level=_stack_level + 1)
 
     def interaction_message_builder(
         self, type_: typing.Union[base_interactions.ResponseType, int], /
@@ -4082,6 +4083,7 @@ async def create_autocomplete_response(
         choices: typing.Union[
             typing.Sequence[commands.CommandChoice], typing.Sequence[special_endpoints.AutocompleteChoiceBuilder]
         ],
+        _stack_level: int = 0,
     ) -> None:
         route = routes.POST_INTERACTION_RESPONSE.compile(interaction=interaction, token=token)
 
@@ -4099,6 +4101,7 @@ async def create_autocomplete_response(
                     removal_version="2.0.0.dev119",
                     additional_info="Use AutocompleteChoiceBuilder instead",
                     quote=False,
+                    stack_level=3 + _stack_level,
                 )
                 warned = True
 
diff --git a/hikari/interactions/command_interactions.py b/hikari/interactions/command_interactions.py
index eff6ce0f..5ceef418 100644
--- a/hikari/interactions/command_interactions.py
+++ b/hikari/interactions/command_interactions.py
@@ -383,7 +383,10 @@ class AutocompleteInteraction(BaseCommandInteraction):
     """Parameter values provided by the user invoking this command."""
 
     def build_response(
-        self, choices: typing.Sequence[commands.CommandChoice]
+        self,
+        choices: typing.Union[
+            typing.Sequence[commands.CommandChoice], typing.Sequence[special_endpoints.AutocompleteChoiceBuilder]
+        ],
     ) -> special_endpoints.InteractionAutocompleteBuilder:
         """Get a message response builder for use in the REST server flow.
 
@@ -392,6 +395,10 @@ def build_response(
             `AutocompleteInteraction.create_response` should be used to set
             the interaction response.
 
+        .. deprecated:: 2.0.0.dev118
+            Passing `hikari.commands.CommandChoice`s here instead of
+            `hikari.api.special_endpoints.AutocompleteChoiceBuilder`s.
+
         Examples
         --------
         .. code-block:: python
@@ -401,9 +408,9 @@ async def handle_autocomplete_interaction(interaction: AutocompleteInteraction)
                     interaction
                     .build_response(
                         [
-                            CommandChoice(name="foo", value="a"),
-                            CommandChoice(name="bar", value="b"),
-                            CommandChoice(name="baz", value="c"),
+                            AutocompleteChoiceBuilder(name="foo", value="a"),
+                            AutocompleteChoiceBuilder(name="bar", value="b"),
+                            AutocompleteChoiceBuilder(name="baz", value="c"),
                         ]
                     )
                 )
@@ -413,12 +420,18 @@ async def handle_autocomplete_interaction(interaction: AutocompleteInteraction)
         hikari.api.special_endpoints.InteractionAutocompleteBuilder
             Interaction autocomplete response builder object.
         """
-        return self.app.rest.interaction_autocomplete_builder(choices)
-
-    async def create_response(self, choices: typing.Sequence[commands.CommandChoice]) -> None:
-        """Create a response for this autocomplete interaction."""
-        await self.app.rest.create_autocomplete_response(
-            self.id,
-            self.token,
-            choices,
-        )
+        return self.app.rest.interaction_autocomplete_builder(choices, _stack_level=1)
+
+    async def create_response(
+        self,
+        choices: typing.Union[
+            typing.Sequence[commands.CommandChoice], typing.Sequence[special_endpoints.AutocompleteChoiceBuilder]
+        ],
+    ) -> None:
+        """Create a response for this autocomplete interaction.
+
+        .. deprecated:: 2.0.0.dev118
+            Passing `hikari.commands.CommandChoice`s here instead of
+            `hikari.api.special_endpoints.AutocompleteChoiceBuilder`s.
+        """
+        await self.app.rest.create_autocomplete_response(self.id, self.token, choices, _stack_level=1)


Test patch:
diff --git a/tests/hikari/interactions/test_command_interactions.py b/tests/hikari/interactions/test_command_interactions.py
index fa7d975d..523c218a 100644
--- a/tests/hikari/interactions/test_command_interactions.py
+++ b/tests/hikari/interactions/test_command_interactions.py
@@ -23,9 +23,9 @@
 import pytest
 
 from hikari import channels
-from hikari import commands
 from hikari import snowflakes
 from hikari import traits
+from hikari.impl import special_endpoints
 from hikari.interactions import base_interactions
 from hikari.interactions import command_interactions
 
@@ -124,14 +124,17 @@ def mock_autocomplete_interaction(self, mock_app):
 
     @pytest.fixture()
     def mock_command_choices(self):
-        return [commands.CommandChoice(name="a", value="b"), commands.CommandChoice(name="foo", value="bar")]
+        return [
+            special_endpoints.AutocompleteChoiceBuilder(name="a", value="b"),
+            special_endpoints.AutocompleteChoiceBuilder(name="foo", value="bar"),
+        ]
 
     def test_build_response(self, mock_autocomplete_interaction, mock_app, mock_command_choices):
         mock_app.rest.interaction_autocomplete_builder = mock.Mock()
         builder = mock_autocomplete_interaction.build_response(mock_command_choices)
 
         assert builder is mock_app.rest.interaction_autocomplete_builder.return_value
-        mock_app.rest.interaction_autocomplete_builder.assert_called_once_with(mock_command_choices)
+        mock_app.rest.interaction_autocomplete_builder.assert_called_once_with(mock_command_choices, _stack_level=1)
 
     @pytest.mark.asyncio()
     async def test_create_response(
@@ -143,7 +146,5 @@ async def test_create_response(
         await mock_autocomplete_interaction.create_response(mock_command_choices)
 
         mock_app.rest.create_autocomplete_response.assert_awaited_once_with(
-            2312312,
-            "httptptptptptptptp",
-            mock_command_choices,
+            2312312, "httptptptptptptptp", mock_command_choices, _stack_level=1
         )