Skip to content

Commit ac13ead

Browse files
authored
Merge pull request #334 from ToxicKidz/fix/application-command-error
Fixes `discord.Bot.on_application_command_error`
2 parents 6e2cb76 + 7f1d133 commit ac13ead

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

discord/bot.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -569,18 +569,16 @@ async def on_application_command_error(
569569
570570
This only fires if you do not specify any listeners for command error.
571571
"""
572-
# TODO
573-
# if self.extra_events.get('on_application_command_error', None):
574-
# return
572+
if self.extra_events.get('on_application_command_error', None):
573+
return
575574

576575
command = context.command
577576
if command and command.has_error_handler():
578577
return
579578

580-
# TODO
581-
# cog = context.cog
582-
# if cog and cog.has_error_handler():
583-
# return
579+
cog = context.cog
580+
if cog and cog.has_error_handler():
581+
return
584582

585583
print(f"Ignoring exception in command {context.command}:", file=sys.stderr)
586584
traceback.print_exception(

discord/commands/context.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25+
from __future__ import annotations
2526

2627
from typing import TYPE_CHECKING, Optional, Union
2728

@@ -31,6 +32,9 @@
3132
import discord
3233
from discord.state import ConnectionState
3334

35+
from .commands import ApplicationCommand
36+
from ..cog import Cog
37+
3438
from ..guild import Guild
3539
from ..interactions import Interaction, InteractionResponse
3640
from ..member import Member
@@ -63,7 +67,7 @@ class ApplicationContext(discord.abc.Messageable):
6367
def __init__(self, bot: "discord.Bot", interaction: Interaction):
6468
self.bot = bot
6569
self.interaction = interaction
66-
self.command = None
70+
self.command: ApplicationCommand = None # type: ignore
6771
self._state: ConnectionState = self.interaction._state
6872

6973
async def _get_channel(self) -> discord.abc.Messageable:
@@ -130,3 +134,11 @@ async def delete(self):
130134
@property
131135
def edit(self):
132136
return self.interaction.edit_original_message
137+
138+
@property
139+
def cog(self) -> Optional[Cog]:
140+
"""Optional[:class:`.Cog`]: Returns the cog associated with this context's command. None if it does not exist."""
141+
if self.command is None:
142+
return None
143+
144+
return self.command.cog

0 commit comments

Comments
 (0)