Skip to content

context

Command and Interaction Context Utilities.

This module provides helper functions to abstract and normalize the process of extracting contextual information from different types of command invocations in discord.py.

The primary goal is to create a single, consistent dictionary format for context data, regardless of whether the command was triggered by a traditional prefix command (commands.Context) or a slash command (discord.Interaction). This standardized context is invaluable for logging, error reporting (e.g., to Sentry), and any other system that needs to operate on command data without worrying about the source type.

Functions:

Functions

_get_interaction_details

Python
_get_interaction_details(source: Interaction) -> dict[str, Any]

Extract context details specifically from a discord.Interaction.

Parameters:

  • source (Interaction) –

    The interaction object from a slash command.

Returns:

  • dict[str, Any]

    A dictionary containing interaction-specific context.

_get_context_details

Python
_get_context_details(source: Context[Any]) -> dict[str, Any]

Extract context details specifically from a commands.Context.

Parameters:

  • source (Context[Any]) –

    The context object from a prefix command.

Returns:

  • dict[str, Any]

    A dictionary containing context-specific data.

get_interaction_context

Python
get_interaction_context(source: ContextOrInteraction) -> dict[str, Any]

Build a standardized dictionary of context from a command or interaction.

This is the main public function of the module. It takes either a commands.Context or a discord.Interaction and returns a dictionary with a consistent set of keys, abstracting away the differences between the two source types.

Parameters:

  • source (Context[Tux] | Interaction) –

    The command Context or Interaction object.

Returns:

  • dict[str, Any]

    A dictionary with standardized context keys like user_id, command_name, guild_id, command_type, etc.