From 40d0cc23bee45a647689b5ba2c808a77297235f7 Mon Sep 17 00:00:00 2001 From: Rouven Date: Wed, 2 Feb 2022 22:33:58 +0100 Subject: [PATCH 1/3] added message object to context --- flask_discord_interactions/context.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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. From 458a037b6886f34d7d3c0eb196ed09d436d3ae08 Mon Sep 17 00:00:00 2001 From: Brooke Chalmers Date: Wed, 2 Feb 2022 22:30:42 -0500 Subject: [PATCH 2/3] Add paragraph to documentation --- docs/components.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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: From ff4679549b904f9771ecd70b56ee2d5a3e1c88b1 Mon Sep 17 00:00:00 2001 From: Brooke Chalmers Date: Wed, 2 Feb 2022 22:31:17 -0500 Subject: [PATCH 3/3] Add example of using message from context --- examples/components.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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"])