Skip to main content
Every exception raised by alterself inherits from the base class 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.
HTTP status 429 Too Many Requests is handled automatically by alterself’s HTTP client. It reads the Retry-After header and retries the request after the cooldown expires. You will only see an HTTPError with status=429 if the global rate limit is sustained for longer than alterself’s configured retry budget.

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. The SessionClosed 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

alterself does not bundle a captcha solver. Solving the challenge is your responsibility. Frequent CaptchaChallenge errors on common actions are a signal that Discord has flagged the account. Reducing request frequency and ensuring realistic behaviour patterns will help.

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.
Register a single @bot.on_command_error handler at the top level of your bot file to centralise error reporting. This keeps individual command functions clean and ensures you never miss an unhandled CommandError.