or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/py-cord@2.6.x
tile.json

tessl/pypi-py-cord

tessl install tessl/pypi-py-cord@2.6.0

A modern, async-ready Python API wrapper for Discord with comprehensive bot development features

Agent Success

Agent success rate when using this tile

93%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.11x

Baseline

Agent success rate without this tile

84%

task.mdevals/scenario-3/

Discord Bot Command Error Handler

Build a robust error handling system for a Discord bot that gracefully handles common errors and implements retry logic with exponential backoff.

Requirements

Your task is to implement a command error handler that:

  1. Catches and handles common Discord API errors
  2. Implements retry logic with exponential backoff for transient errors
  3. Provides user-friendly error messages to the command context

Error Handling

Handle the following error types:

  • HTTP errors (403 Forbidden, 404 Not Found, etc.) - display appropriate messages
  • Rate limit errors - retry with exponential backoff
  • Command errors (BadArgument, MissingPermissions, etc.) - display helpful usage information

Retry Logic with Exponential Backoff

For transient errors (rate limits, connection issues):

  • Start with a base delay of 1 second
  • Maximum of 5 retry attempts
  • Double the delay after each failed attempt (1s, 2s, 4s, 8s, 16s)
  • After maximum retries, notify the user and stop

Error Messages

  • Send error messages to the channel where the command was invoked using the context
  • Provide clear explanations of what went wrong
  • For retries, optionally indicate the retry attempt

Test Cases

  • When a rate limit error occurs, the handler waits and retries with exponential backoff starting at 1 second @test
  • When an HTTP 403 Forbidden error occurs, the handler sends a message indicating insufficient permissions @test
  • When maximum retry attempts (5) are exceeded, the handler notifies the user and stops retrying @test
  • When a command argument error occurs, the handler sends a message explaining the correct usage @test

Implementation

@generates

API

import discord
from discord.ext import commands
from typing import Optional, Callable, Any
import asyncio

class CommandErrorHandler:
    """
    Handles errors for Discord bot commands with retry logic and user feedback.
    """

    def __init__(self, bot: commands.Bot, max_retries: int = 5, base_delay: float = 1.0):
        """
        Initialize the error handler.

        Args:
            bot: The Discord bot instance
            max_retries: Maximum number of retry attempts for transient errors
            base_delay: Base delay in seconds for exponential backoff
        """
        pass

    async def handle_command_error(self, ctx: commands.Context, error: Exception) -> None:
        """
        Handle errors that occur during command execution.

        Args:
            ctx: The command context
            error: The exception that occurred
        """
        pass

    async def retry_with_backoff(
        self,
        operation: Callable,
        *args,
        **kwargs
    ) -> Optional[Any]:
        """
        Retry an operation with exponential backoff.

        Args:
            operation: The async function to retry
            *args: Positional arguments for the operation
            **kwargs: Keyword arguments for the operation

        Returns:
            The result of the operation if successful, None if all retries failed
        """
        pass

    def setup_error_handler(self) -> None:
        """
        Register the error handler with the bot.
        """
        pass

Dependencies { .dependencies }

py-cord { .dependency }

A modern, async-ready Python API wrapper for Discord with comprehensive bot development features.

@satisfied-by