Repository: Cog-Creators/Red-DiscordBot

Issue:
Check responding to autocomplete interaction: 400 Bad Request - Value must be one of {8}.
### What Red version are you using?

3.5.9

### What were you trying to do?

Presumably get autocomplete results for something

### What did you expect to happen?

The results to come through properly

### What actually happened?

The exception seems like it was handled and just logged to sentry, although i'm not certain as I cant seem to repro it on purpose.

### How can we reproduce this error?

I am not sure how to repro this error, it has occurred 19 times within the last 30 days during which i was unaware.

### Anything else?

## Raw Stack Trace
```
HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In type: Value must be one of {8}.
  File "discord/app_commands/tree.py", line 1089, in wrapper
    await self._call(interaction)
  File "discord/app_commands/tree.py", line 1210, in _call
    if not await self.interaction_check(interaction):
  File "redbot/core/tree.py", line 344, in interaction_check
    await interaction.response.send_message(
  File "discord/interactions.py", line 801, in send_message
    await adapter.create_interaction_response(
  File "discord/webhook/async_.py", line 221, in request
    raise HTTPException(response, data)
```
### Breadcrumbs
![image](https://github.com/Cog-Creators/Red-DiscordBot/assets/58824393/0da6de6c-2c9b-4353-849b-bec0a8a36bc9)


![image](https://github.com/Cog-Creators/Red-DiscordBot/assets/58824393/f84ff24f-f2f6-4fdb-af3d-1706ff5de30c)

Some insight from Zeph: check is responding to an autocomplete interaction, which only supports `APPLICATION_COMMAND_AUTOCOMPLETE_RESULT` (internally represented by the value 8 to Discord's API) responses


Discussion:
> ### How can we reproduce this error?
> I am not sure how to repro this error, it has occurred 19 times within the last 30 days during which i was unaware.

Presumably, based on this traceback, one could reproduce it by ignoring a channel or guild then attempting to use an app command that has autocomplete in that ignored channel or guild. I haven't tested this myself, yet, however.

Resolving patch:
diff --git a/redbot/core/tree.py b/redbot/core/tree.py
--- a/redbot/core/tree.py
+++ b/redbot/core/tree.py
@@ -334,6 +334,20 @@ async def on_error(
         else:
             log.exception(type(error).__name__, exc_info=error)
 
+    async def _send_interaction_check_failure(
+        self, interaction: discord.Interaction, message: str
+    ):
+        """Handles responding to interaction check failures.
+        Mainly used for when an interaction is an autocomplete and
+        providing the message in the autocomplete response.
+        """
+        if interaction.type is discord.InteractionType.autocomplete:
+            await interaction.response.autocomplete(
+                [discord.app_commands.Choice(name=message[:80], value="None")]
+            )
+            return
+        await interaction.response.send_message(message, ephemeral=True)
+
     async def interaction_check(self, interaction: discord.Interaction):
         """Global checks for app commands."""
         if interaction.user.bot:
@@ -341,15 +355,15 @@ async def interaction_check(self, interaction: discord.Interaction):
 
         if interaction.guild:
             if not (await self.client.ignored_channel_or_guild(interaction)):
-                await interaction.response.send_message(
-                    "This channel or server is ignored.", ephemeral=True
+                await self._send_interaction_check_failure(
+                    interaction, _("This channel or server is ignored.")
                 )
                 return False
 
         if not (await self.client.allowed_by_whitelist_blacklist(interaction.user)):
-            await interaction.response.send_message(
-                "You are not permitted to use commands because of an allowlist or blocklist.",
-                ephemeral=True,
+            await self._send_interaction_check_failure(
+                interaction,
+                _("You are not permitted to use commands because of an allowlist or blocklist."),
             )
             return False
 


Test patch: