Skip to content

Commit

Permalink
Merge pull request #67 from therealr5/feature/context-message
Browse files Browse the repository at this point in the history
added message object to context
  • Loading branch information
breqdev authored Feb 3, 2022
2 parents e2dc9ec + ff46795 commit 0d39687
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/components.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -283,4 +287,4 @@ Full API
.. autoclass:: flask_discord_interactions.SelectMenuOption(**kwargs)
:show-inheritance:
:members:
:undoc-members:
:undoc-members:
21 changes: 21 additions & 0 deletions examples/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
18 changes: 18 additions & 0 deletions flask_discord_interactions/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 0d39687

Please sign in to comment.