Context

This class provides context information about the incoming interaction as a whole.

You normally would receive this as a ctx parameter to your Command function, similar to in Discord.py.

@discord.command()
def my_user_id(ctx):
    "Show your user ID"
    return f"Your ID is {ctx.author.id}"

This object is always passed in as the first positional argument to your command function. You shouldn’t ever need to create one yourself.

You can also use this object to send followup messages, edit messages, and delete messages:

@discord.command()
def delay(ctx, duration: int):
    def do_delay():
        time.sleep(duration)

        ctx.edit("Hi! I waited for you :)")

    thread = threading.Thread(target=do_delay)
    thread.start()

    return Message(deferred=True)

Pass in either a Message object or a string (which will be converted into a Message object. See Message Models for more details.

Full API

class flask_discord_interactions.Context(author: Union[Member, User] = None, id: str = None, type: int = None, command_type: int = None, token: str = None, channel_id: str = None, guild_id: str = None, options: list = None, values: list = None, components: list = None, resolved: dict = None, command_name: str = None, command_id: str = None, members: List[Member] = None, channels: List[Channel] = None, roles: List[Role] = None, message: Message = None, locale: Optional[str] = None, guild_locale: Optional[str] = None, app_permissions: Optional[str] = None, app: Flask = None, discord: DiscordInteractions = None, custom_id: str = None, primary_id: str = None, handler_state: list = None, target_id: str = None, target: Union[User, Message] = None)

Represents the context in which a Command or custom ID handler is invoked.

author

A User or Member object representing the invoking user.

Type

Union[flask_discord_interactions.models.user.Member, flask_discord_interactions.models.user.User]

id

The unique ID (snowflake) of this interaction.

Type

str

type

The InteractionType of this interaction.

Type

int

command_type

The ApplicationCommandType of this interaction.

Type

int

token

The token to use when sending followup messages.

Type

str

channel_id

The unique ID (snowflake) of the channel this command was invoked in.

Type

str

guild_id

The unique ID (snowflake) of the guild this command was invoked in.

Type

str

options

A list of the options passed to the command.

Type

list

values

A list of the values selected, if this is a Select Menu handler.

Type

list

components

The Modal’s components with their submitted values, if this is a Modal handler.

Type

list

resolved

Additional data ()

Type

dict

command_name

The name of the command that was invoked.

Type

str

command_id

The unique ID (snowflake) of the command that was invoked.

Type

str

users

User objects for each user specified as an option.

members

Member objects for each user specified as an option, if this command was invoked in a guild.

Type

List[flask_discord_interactions.models.user.Member]

channels

Channel objects for each channel specified as an option.

Type

List[flask_discord_interactions.models.channel.Channel]

roles

Role object for each role specified as an option.

Type

List[flask_discord_interactions.models.role.Role]

target

The targeted User or message.

Type

Union[flask_discord_interactions.models.user.User, flask_discord_interactions.models.message.Message]

message

The message that the invoked components are attached to. Only available on component interactions.

Type

flask_discord_interactions.models.message.Message

locale

The selected language of the invoking user.

Type

Optional[str]

guild_locale

The guild’s preferred locale, if invoked in a guild.

Type

Optional[str]

app_permissions

Bitwise set of permissions the app or bot has within the channel the interaction was sent from.

Type

Optional[str]

create_args()

Create the arguments which will be passed to the function when the Command is invoked.

create_args_chat_input()

Create the arguments for this command, assuming it is a CHAT_INPUT command.

create_handler_args(handler: Callable)

Create the arguments which will be passed to the function when a custom ID handler is invoked.

Parameters

handler (Callable) – The custom ID handler to create arguments for.

delete(message: str = '@original')

Delete an existing message.

Parameters

message (str) – The ID of the message to delete. If omitted, deletes the original message.

edit(updated: Union[Message, str], message: str = '@original')

Edit an existing message.

Parameters
  • updated (Union[Message, str]) – The updated Message to edit the message to.

  • message (str) – The ID of the message to edit. If omitted, edits the original message.

followup_url(message: Optional[str] = None)

Return the followup URL for this interaction. This URL can be used to send a new message, or to edit or delete an existing message.

Parameters

message (str) – The ID of the message to edit or delete. If None, sends a new message. If “@original”, refers to the original message.

freeze()

Return a copy of this Context that can be pickled for RQ and Celery.

get_command(command_name: Optional[str] = None)

Get the ID of a command by name.

Parameters

command_name (str) – The name of the command to get the ID of.

get_component(component_id: str)

Get a Component, only available for Modal Contexts. If the component was not found, raises a LookupError.

Parameters

component_id (str) – The ID of the component to look up.

parse_author(data: dict)

Parse the author (invoking user) of this interaction.

This will set author to a User object if this interaction occurred in a direct message, or a Member object if interaction occurred in a guild.

Parameters

data – The incoming interaction data.

parse_custom_id()

Parse the custom ID of the incoming interaction data.

This includes the primary ID as well as any state stored in the handler.

parse_message(data: dict)

Parse the message out of in interaction.

Parameters

data – The incoming interaction data.

parse_resolved()

Parse the "resolved" section of the incoming interaction data.

This section includes objects representing each user, member, channel, and role passed as an argument to the command.

parse_target()

Parse the target of the incoming interaction.

For User and Message commands, the target is the relevant user or message. This method sets the ctx.target field.

send(message: Union[Message, str])

Send a new followup message.

Parameters

message (Union[Message, str]) – The Message to send as a followup message.