Skip to content

Commit f537f17

Browse files
committed
Interaction::respondWithModal
1 parent c8bd5a2 commit f537f17

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/Discord/Parts/Interactions/Interaction.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Discord\Builders\Components\Component;
1717
use Discord\Builders\Components\ComponentObject;
1818
use Discord\Builders\MessageBuilder;
19+
use Discord\Builders\ModalBuilder;
1920
use Discord\Exceptions\AttachmentSizeException;
2021
use Discord\Helpers\Collection;
2122
use Discord\Helpers\Multipart;
@@ -69,7 +70,7 @@
6970
class Interaction extends Part
7071
{
7172
/**
72-
* {@inheritDoc}
73+
* @inheritDoc
7374
*/
7475
protected $fillable = [
7576
'id',
@@ -647,9 +648,9 @@ public function autoCompleteResult(array $choices): PromiseInterface
647648
*
648649
* @link https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
649650
*
650-
* @param string $title The title of the popup modal, max 45 characters
651-
* @param string $custom_id Developer-defined identifier for the component, max 100 characters
652-
* @param array|ComponentObject[] $components Between 1 and 5 (inclusive) components that make up the modal contained in Action Row
651+
* @param string $title The title of the popup modal, max 45 characters.
652+
* @param string $custom_id Developer-defined identifier for the component, max 100 characters.
653+
* @param array|ComponentObject[] $components Between 1 and 5 (inclusive) components that make up the modal.
653654
* @param callable|null $submit The function to call once modal is submitted.
654655
*
655656
* @throws \LogicException Interaction is Ping or Modal Submit.
@@ -684,6 +685,32 @@ public function showModal(string $title, string $custom_id, array $components, ?
684685
});
685686
}
686687

688+
/**
689+
* Responds to the interaction with a popup modal.
690+
*
691+
* @link https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
692+
*
693+
* @param ModalBuilder $modal The modal.
694+
* @param callable|null $submit The function to call once modal is submitted.
695+
*
696+
* @return PromiseInterface
697+
*/
698+
public function respondWithModal($modal, ?callable $submit = null): PromiseInterface
699+
{
700+
if ($submit) {
701+
$this->discord->on(Event::INTERACTION_CREATE, $this->createListener($modal->getCustomId(), $submit, 60 * 15));
702+
}
703+
704+
return $this->respond($modal->jsonSerialize())->then(function ($response) use ($modal, $submit) {
705+
if ($submit) {
706+
$listener = $this->createListener($modal->getCustomId(), $submit, 60 * 15);
707+
$this->discord->on(Event::INTERACTION_CREATE, $listener);
708+
}
709+
710+
return $response;
711+
});
712+
}
713+
687714
/**
688715
* Creates a listener callback for handling modal submit interactions with a specific custom ID.
689716
*

0 commit comments

Comments
 (0)