22
33.. _Event Ref :
44
5- Events Reference
5+ Table Reference
66################
77
88.. warning ::
@@ -307,7 +307,7 @@ Events Reference
307307
308308
309309Client Events
310- ~~~~~~~~~~~~~
310+ #############
311311
312312.. py :function :: event_ready() -> None
313313 :async:
@@ -362,7 +362,7 @@ Client Events
362362
363363
364364Commands Events
365- ~~~~~~~~~~~~~~~
365+ ###############
366366
367367.. py :function :: event_command_invoked(ctx: twitchio.ext.commands.Context) -> None
368368 :async:
@@ -383,14 +383,71 @@ Commands Events
383383
384384 Event dispatched when a :class: `~twitchio.ext.commands.Command ` encounters an error during invocation.
385385
386- :param twitchio.ext.commands.CommandErrorPayload payload: The error payload containing context and the exception raised.
386+ By default, every :class: `~twitchio.ext.commands.Bot ` implements this event. You can override the default implementation
387+ of this event to fine-tune errors. Care should be taken to make sure unexpected exceptions are properly logged or made
388+ aware of.
389+
390+ Below is a small example of overriding the default implementation in a :class: `~twitchio.ext.commands.Component `. This,
391+ when used in an extension allows your error handler to be hot-reloaded without losing access to a proper exception handler.
392+
393+ Example
394+ -------
395+
396+ .. code :: python3
397+
398+ import logging
399+
400+ from twitchio.ext import commands
401+
402+
403+ logger = logging.getLogger(__name__)
404+
405+
406+ class ErrorComponent(commands.Component):
407+
408+ def __init__(self, bot: commands.Bot) -> None:
409+ # Store the original error handler; if we re/unload this component it will be reassigned to our bot...
410+ self.original = bot.event_command_error
411+
412+ # Override the default error handler...
413+ bot.event_command_error = self.event_command_error
387414
415+ self.bot = bot
416+
417+ async def component_teardown(self) -> None:
418+ # Reassign the original error handler...
419+ self.bot.event_command_error = self.original
420+
421+ async def event_command_error(self, payload: commands.CommandErrorPayload) -> None:
422+ ctx = payload.context
423+ command = ctx.command
424+ error = payload.exception
425+
426+ # We don't want to dispatch errors that have already been handled before...
427+ if command and command.has_error and ctx.error_dispatched:
428+ return
429+
430+ # Example: A common error to suppress is the CommandNotFound error...
431+ if isinstance(error, commands.CommandNotFound):
432+ return
433+
434+ # Example: As an example if a guard fails we can send a default message back...
435+ if isinstance(error, commands.GuardFailure):
436+ await ctx.send(f"{ctx.chatter} you do not have permission to use this command.")
437+
438+ # For all unhandled errors, we should log them so we know what went wrong...
439+ msg = f'Ignoring exception in command "{ctx.command}":\n'
440+ logger.error(msg, exc_info=error)
441+
442+
443+ async def setup(bot: commands.Bot) -> None:
444+ await bot.add_component(ErrorComponent(bot))
445+
446+ :param twitchio.ext.commands.CommandErrorPayload payload: The error payload containing context and the exception raised.
388447
389- EventSub Events
390- ~~~~~~~~~~~~~~~
391448
392449Automod
393- -------
450+ #######
394451
395452.. py :function :: event_automod_message_hold(payload: twitchio.AutomodMessageHold) -> None
396453 :async:
@@ -444,7 +501,7 @@ Automod
444501
445502
446503Bans
447- ----
504+ ####
448505
449506.. py :function :: event_ban(payload: twitchio.ChannelBan) -> None
450507 :async:
496553
497554
498555Channel / Broadcaster
499- ---------------------
556+ #####################
500557
501558.. py :function :: event_channel_update(payload: twitchio.ChannelUpdate) -> None
502559 :async:
@@ -560,7 +617,7 @@ Channel / Broadcaster
560617
561618
562619Channel Points
563- --------------
620+ ##############
564621
565622.. py :function :: event_automatic_redemption_add(payload: twitchio.ChannelPointsAutoRedeemAdd) -> None
566623 :async:
@@ -635,7 +692,7 @@ Channel Points
635692 :param twitchio.ChannelPointsRedemptionUpdate payload: The EventSub payload received for this event.
636693
637694Charity Campaigns
638- -----------------
695+ #################
639696
640697.. py :function :: event_charity_campaign_donate(payload: twitchio.CharityCampaignDonation) -> None
641698 :async:
@@ -686,7 +743,7 @@ Charity Campaigns
686743 :param twitchio.CharityCampaignStop payload: The EventSub payload for this event.
687744
688745Chat / Messages
689- ---------------
746+ ###############
690747
691748.. py :function :: event_message(payload: twitchio.ChatMessage) -> None
692749 :async:
@@ -811,7 +868,7 @@ Chat / Messages
811868 :param twitchio.ChannelBitsUse payload: The EventSub payload for this event.
812869
813870Goals
814- -----
871+ #####
815872
816873.. py :function :: event_goal_begin(payload: twitchio.GoalBegin) -> None
817874 :async:
@@ -850,7 +907,7 @@ Goals
850907 :param twitchio.GoalEnd payload: The EventSub payload for this event.
851908
852909Hype Train
853- ----------
910+ ##########
854911
855912.. py :function :: event_hype_train(payload: twitchio.HypeTrainBegin) -> None
856913 :async:
@@ -890,7 +947,7 @@ Hype Train
890947
891948
892949Moderation
893- ----------
950+ ##########
894951
895952.. py :function :: event_mod_action(payload: twitchio.ChannelModerate) -> None
896953 :async:
@@ -957,7 +1014,7 @@ Moderation
9571014
9581015
9591016Polls
960- -----
1017+ #####
9611018
9621019.. py :function :: event_poll_begin(payload: twitchio.ChannelPollBegin) -> None
9631020 :async:
@@ -996,7 +1053,7 @@ Polls
9961053 :param twitchio.ChannelPollEnd payload: The EventSub payload for this event.
9971054
9981055Predictions
999- -----------
1056+ ###########
10001057
10011058.. py :function :: event_prediction_begin(payload: twitchio.ChannelPredictionBegin) -> None
10021059 :async:
@@ -1048,7 +1105,7 @@ Predictions
10481105
10491106
10501107Shared Chat
1051- -----------
1108+ ###########
10521109
10531110.. py :function :: event_shared_chat_begin(payload: twitchio.SharedChatSessionBegin) -> None
10541111 :async:
@@ -1088,7 +1145,7 @@ Shared Chat
10881145
10891146
10901147Shield Mode
1091- -----------
1148+ ###########
10921149
10931150.. py :function :: event_shield_mode_begin(payload: twitchio.ShieldModeBegin) -> None
10941151 :async:
@@ -1116,7 +1173,7 @@ Shield Mode
11161173
11171174
11181175Shoutouts
1119- ---------
1176+ #########
11201177
11211178.. py :function :: event_shoutout_create(payload: twitchio.ShoutoutCreate) -> None
11221179 :async:
@@ -1143,7 +1200,7 @@ Shoutouts
11431200 :param twitchio.ShoutoutReceive payload: The EventSub payload for this event.
11441201
11451202Subscriptions
1146- -------------
1203+ #############
11471204
11481205.. py :function :: event_subscription(payload: twitchio.ChannelSubscribe) -> None
11491206 :async:
@@ -1194,7 +1251,7 @@ Subscriptions
11941251 :param twitchio.ChannelSubscriptionMessage payload: The EventSub payload for this event.
11951252
11961253Streams
1197- -------
1254+ #######
11981255
11991256.. py :function :: event_stream_online(payload: twitchio.StreamOnline) -> None
12001257 :async:
@@ -1221,7 +1278,7 @@ Streams
12211278 :param twitchio.StreamOffline payload: The EventSub payload for this event.
12221279
12231280Suspicious Users
1224- ----------------
1281+ ################
12251282
12261283.. py :function :: event_suspicious_user_message(payload: twitchio.SuspiciousUserMessage) -> None
12271284 :async:
@@ -1248,7 +1305,7 @@ Suspicious Users
12481305 :param twitchio.SuspiciousUserUpdate payload: The EventSub payload for this event.
12491306
12501307OAuth
1251- -----
1308+ #####
12521309
12531310.. py :function :: event_user_authorization_grant(payload: twitchio.UserAuthorizationGrant) -> None
12541311 :async:
@@ -1274,7 +1331,7 @@ OAuth
12741331
12751332
12761333User
1277- -----
1334+ ####
12781335
12791336.. py :function :: event_user_update(payload: twitchio.UserUpdate) -> None
12801337 :async:
@@ -1289,7 +1346,7 @@ User
12891346 :param twitchio.UserUpdate payload: The EventSub payload for this event.
12901347
12911348Warnings
1292- --------
1349+ ########
12931350
12941351.. py :function :: event_warning_acknowledge(payload: twitchio.ChannelWarningAcknowledge) -> None
12951352 :async:
0 commit comments