Skip to content

Commit

Permalink
Merge pull request Pycord-Development#334 from ToxicKidz/fix/applicat…
Browse files Browse the repository at this point in the history
…ion-command-error

Fixes `discord.Bot.on_application_command_error`
  • Loading branch information
Dorukyum authored Oct 27, 2021
2 parents 6e2cb76 + 7f1d133 commit ac13ead
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 5 additions & 7 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion discord/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit ac13ead

Please sign in to comment.