AlterselfError, so you can catch all library-level errors with a single except AlterselfError clause or target specific failure modes precisely. Understanding which exception maps to which failure condition lets you write resilient bots that handle rate limits, closed sessions, failed commands, and captcha challenges without crashing.
Exception Hierarchy
HTTPError
HTTPError is raised whenever an HTTP request to the Discord REST API returns a 4xx or 5xx response. It carries three pieces of information: the HTTP status code, Discord’s own error code, and a human-readable message from Discord’s response body.
Attributes
Common Discord Error Codes
These are the Discord JSON error codes you are most likely to encounter during normal operation. The full list is available in the Discord developer documentation.SessionClosed
SessionClosed is a subclass of GatewayError and is raised when the Discord gateway WebSocket connection closes with a close code. Some close codes are recoverable. alterself will automatically attempt to reconnect, but others are fatal and require you to address the underlying problem (such as an invalid token) before restarting.
Attributes
Fatal Close Codes
When alterself receives one of the following codes, it does not attempt to reconnect. TheSessionClosed exception propagates out of bot.start() and your process should treat it as a terminal error.
Non-fatal close codes (such as
4000 Unknown Error, 4001 Unknown Opcode, and 4009 Session Timed Out) trigger an automatic reconnect internally. You do not need to handle these yourself unless you want to log them.CaptchaChallenge
CaptchaChallenge is raised when Discord responds to an HTTP request by requiring you to solve an hCaptcha challenge before the action can proceed. This occurs most often on account actions such as joining a guild, sending a friend request, or logging in from an unrecognised location.
Attributes
CommandError
CommandError is the base class for all failures that occur within the command processing pipeline. You typically handle these in a global error handler registered with @bot.on_command_error rather than wrapping every individual command invocation in a try/except block.
CheckFailed
CheckFailed is raised when a check predicate, a function decorated with @commands.check, returns a falsy value. It signals that the invoking user, channel, or context did not meet the conditions you defined.
By default, alterself silently ignores CheckFailed and does not send any message to the user. You only need to handle it explicitly if you want to send feedback or log the failed check.
ConversionFailed
ConversionFailed is raised when an argument type converter encounters an error. For example, when a command expects an int but the user passes a word that cannot be parsed as one.
CommandNotFound
CommandNotFound is raised when the prefix and command name were parsed successfully, but no registered command matches the name. You can use this to silently ignore unknown commands or to suggest alternatives.
MissingArgument
MissingArgument is raised when a command handler declares a required parameter but the user’s invocation did not supply a value for it.