Repository: Pycord-Development/pycord

Issue:
cog_command_error is not working
### Summary

cog_command_error didn't work in my cog

### Reproduction Steps

Cog -> run connect command( no channel found ) -> catch error (cog_command_error )

### Minimal Reproducible Code

```python
async def connect_channel(self, ctx, channel: discord.VoiceChannel = None):
        try:
            channel = channel or ctx.author.voice.channel
        except:
            await ctx.respond("No voice channel.", ephemeral=True)
            raise NoChannelProvided

@slash_command(guild_ids=testguild, description="Connect to a voice channel.")
    async def connect(self, ctx, channel: discord.VoiceChannel = None):
        try:
            vc = await self.connect_channel(ctx, channel)
        except NoChannelProvided:
            return
```


### Expected Results

cog_command_error catch the error in my cog

### Actual Results

Application Command raised an exception: AttributeError: 'NoneType' object has no attribute 'connect'
Ignoring exception in command <discord.app.commands.SlashCommand name=connect>:
Traceback (most recent call last):
  File "C:\Users\Lam'Family\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app\commands.py", line 85, in wrapped
    ret = await coro(arg)
  File "C:\Users\Lam'Family\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\app\commands.py", line 445, in _invoke
    await self.callback(self.cog, ctx, **kwargs)
  File "C:\Users\Lam'Family\Desktop\Bots\cogs\basic.py", line 130, in connect
    vc = await self.connect_channel(ctx, channel)
  File "C:\Users\Lam'Family\Desktop\Bots\cogs\basic.py", line 123, in connect_channel
    vc: Player = await channel.connect(cls=player)
AttributeError: 'NoneType' object has no attribute 'connect'

### Intents

default, members

### System Information

Discord Version: 2.0.0a
Python Version: 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]

### Checklist

- [X] I have searched the open issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have removed my token from display, if visible.

### Additional Context

_No response_


Discussion:

Resolving patch:
diff --git a/discord/bot.py b/discord/bot.py
--- a/discord/bot.py
+++ b/discord/bot.py
@@ -569,18 +569,16 @@ async def on_application_command_error(
 
         This only fires if you do not specify any listeners for command error.
         """
-        # TODO
-        # if self.extra_events.get('on_application_command_error', None):
-        #     return
+        if self.extra_events.get('on_application_command_error', None):
+            return
 
         command = context.command
         if command and command.has_error_handler():
             return
 
-        # TODO
-        # cog = context.cog
-        # if cog and cog.has_error_handler():
-        #     return
+        cog = context.cog
+        if cog and cog.has_error_handler():
+            return
 
         print(f"Ignoring exception in command {context.command}:", file=sys.stderr)
         traceback.print_exception(
diff --git a/discord/commands/context.py b/discord/commands/context.py
--- a/discord/commands/context.py
+++ b/discord/commands/context.py
@@ -22,6 +22,7 @@
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.
 """
+from __future__ import annotations
 
 from typing import TYPE_CHECKING, Optional, Union
 
@@ -31,6 +32,9 @@
     import discord
     from discord.state import ConnectionState
 
+    from .commands import ApplicationCommand
+    from ..cog import Cog
+
 from ..guild import Guild
 from ..interactions import Interaction, InteractionResponse
 from ..member import Member
@@ -63,7 +67,7 @@ class ApplicationContext(discord.abc.Messageable):
     def __init__(self, bot: "discord.Bot", interaction: Interaction):
         self.bot = bot
         self.interaction = interaction
-        self.command = None
+        self.command: ApplicationCommand = None  # type: ignore
         self._state: ConnectionState = self.interaction._state
 
     async def _get_channel(self) -> discord.abc.Messageable:
@@ -130,3 +134,11 @@ async def delete(self):
     @property
     def edit(self):
         return self.interaction.edit_original_message
+
+    @property
+    def cog(self) -> Optional[Cog]:
+        """Optional[:class:`.Cog`]: Returns the cog associated with this context's command. None if it does not exist."""
+        if self.command is None:
+            return None
+       
+        return self.command.cog


Test patch: