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¶
- components¶
The Modal’s components with their submitted values, if this is a Modal handler.
- Type:
- members¶
Member
objects for each user specified as an option, if this command was invoked in a guild.- Type:
- message¶
The message that the invoked components are attached to. Only available on component interactions.
- 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.
- 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 aUser
object if this interaction occurred in a direct message, or aMember
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.