Skip to content

Commit 9f065aa

Browse files
authored
Add shortcut property to send modals via ApplicationContext (#1062)
* add shortcut methods to send modals via ApplicationContext * update docstring * add in changed return value for underlying send_modal method
1 parent f4b0d07 commit 9f065aa

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

discord/commands/context.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,15 @@ def unselected_options(self) -> Optional[List[Option]]:
215215
return self.command.options # type: ignore
216216
return None
217217

218+
@property
219+
def send_modal(self) -> Interaction:
220+
"""Sends a modal dialog to the user who invoked the interaction."""
221+
return self.interaction.response.send_modal
222+
218223
@property
219224
def respond(self) -> Callable[..., Awaitable[Union[Interaction, WebhookMessage]]]:
220225
"""Callable[..., Union[:class:`~.Interaction`, :class:`~.Webhook`]]: Sends either a response
221-
or a followup response depending if the interaction has been responded to yet or not."""
226+
or a followup response depending on if the interaction has been responded to yet or not."""
222227
if not self.interaction.response.is_done():
223228
return self.interaction.response.send_message # self.response
224229
else:

discord/interactions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ async def send_autocomplete_result(
792792

793793
self._responded = True
794794

795-
async def send_modal(self, modal: Modal):
795+
async def send_modal(self, modal: Modal) -> Interaction:
796796
"""|coro|
797797
Responds to this interaction by sending a modal dialog.
798798
This cannot be used to respond to another modal dialog submission.
@@ -823,6 +823,7 @@ async def send_modal(self, modal: Modal):
823823
)
824824
self._responded = True
825825
self._parent._state.store_modal(modal, self._parent.user.id)
826+
return self._parent
826827

827828

828829
class _InteractionMessageState:

examples/modal_dialogs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ async def callback(self, interaction: discord.Interaction):
3535
async def modal_slash(ctx):
3636
"""Shows an example of a modal dialog being invoked from a slash command."""
3737
modal = MyModal(title="Slash Command Modal")
38-
await ctx.interaction.response.send_modal(modal)
38+
await ctx.send_modal(modal)
3939

4040

4141
@bot.message_command(name="messagemodal", guild_ids=[...])
4242
async def modal_message(ctx, message):
4343
"""Shows an example of a modal dialog being invoked from a message command."""
4444
modal = MyModal(title="Message Command Modal")
4545
modal.title = f"Modal for Message ID: {message.id}"
46-
await ctx.interaction.response.send_modal(modal)
46+
await ctx.send_modal(modal)
4747

4848

4949
@bot.user_command(name="usermodal", guild_ids=[...])
5050
async def modal_user(ctx, member):
5151
"""Shows an example of a modal dialog being invoked from a user command."""
5252
modal = MyModal(title="User Command Modal")
5353
modal.title = f"Modal for User: {member.display_name}"
54-
await ctx.interaction.response.send_modal(modal)
54+
await ctx.send_modal(modal)
5555

5656

5757
@bot.command()

0 commit comments

Comments
 (0)