diff --git a/docs/components.rst b/docs/components.rst index eaa4e12..58e0ad2 100644 --- a/docs/components.rst +++ b/docs/components.rst @@ -229,6 +229,10 @@ in an incoming interaction, it is split on the newline character. The first line is used as the ID, and any subsequent lines are taken as arguments to the handler function. +Context Internals +----------------- + +Just like commands, custom_id handlers are passed a :class:`.Context` object when invoked. This object will also have a :attr:`.Context.message` field containing the :class:`.Message` that contained the component. Full API @@ -283,4 +287,4 @@ Full API .. autoclass:: flask_discord_interactions.SelectMenuOption(**kwargs) :show-inheritance: :members: - :undoc-members: \ No newline at end of file + :undoc-members: diff --git a/examples/components.py b/examples/components.py index a718894..ff2bc1b 100644 --- a/examples/components.py +++ b/examples/components.py @@ -216,6 +216,27 @@ def stateful_click_counter(ctx): ] ) +@discord.custom_handler() +def handle_parse_message(ctx): + return f"I told you, {ctx.message.content}!" + +@discord.command() +def message_parse_demo(ctx): + "Demonstrate the ability to parse the original message in a handler." + + return Message( + content="The answer is 42", + components=[ + ActionRow(components=[ + Button( + style=ButtonStyles.PRIMARY, + custom_id=handle_parse_message, + label="What is the answer?" + ) + ]) + ] + ) + discord.set_route("/interactions") discord.update_commands(guild_id=os.environ["TESTING_GUILD"]) diff --git a/flask_discord_interactions/context.py b/flask_discord_interactions/context.py index ad55325..2b8406d 100644 --- a/flask_discord_interactions/context.py +++ b/flask_discord_interactions/context.py @@ -54,6 +54,8 @@ class Context(LoadableDataclass): :class:`Role` object for each role specified as an option. target The targeted :class:`User` or message. + message + The message that the invoked components are attached to. Only available on component interactions """ author: Union[Member, User] = None id: str = None @@ -69,6 +71,7 @@ class Context(LoadableDataclass): members: List[Member] = None channels: List[Channel] = None roles: List[Role] = None + message: Message = None app: Any = None discord: Any = None @@ -110,6 +113,7 @@ def from_data(cls, discord=None, app=None, data={}): result.data = data result.parse_author(data) + result.parse_message(data) result.parse_custom_id() result.parse_resolved() result.parse_target() @@ -142,6 +146,20 @@ def parse_author(self, data): else: self.author = None + def parse_message(self, data): + """ + Parse the message out of in interaction. + + Parameters + ---------- + data + The incoming interaction data. + """ + if data.get("message"): + self.message = Message.from_dict(data["message"]) + else: + self.message = None + def parse_custom_id(self): """ Parse the custom ID of the incoming interaction data.