discord-php / DiscordPHP

An API to interact with the popular messaging app Discord
MIT License
985 stars 236 forks source link

Select Menu / Message Replies #987

Closed RobertGrubb closed 1 year ago

RobertGrubb commented 1 year ago

Hey there,

I'm still kinda new to the whole Discord bot world, so bare with me here as I ask a general question.

Right now I'm working with message components. Specifically, I am working on the ability for a select menu option to be selected, and then have the user click a button, then handle the data afterwards. (Think - User selects an option, then clicks a button to perform the finalized action like you would on a form in a website).

So a couple of questions:

  1. Is there a way to NOT have to reply to a select menu interaction?
  2. If the answer to 1 is a no, how would I find the message replies and delete them all for the original message after the whole form is finalized?

Happy to provide even more context, but ideally I want to handle it like a form on a website, once the submit button is pressed I want to delete the original interaction message and any replies along with it.

SQKo commented 1 year ago
  1. $interaction->updateOriginalResponse() with the new message component. So you can replace the first response that contained select menu, with a new message builder that contains a button
  2. $interaction->getOriginalResponse() to get, and $interaction->deleteOriginalResponse() to delete, not sure what you mean with all, interactions are responded first, then followed by one or many follow ups (there are other methods for follow ups)

Check out the Discord API developer docs for more information: https://discord.com/developers/docs/interactions/receiving-and-responding

RobertGrubb commented 1 year ago

@SQKo Apologies for not being clear.

Basically, I want to create a message with components.

This message will have several components - for instance:

SelectMenu (Select a division) [Ability to select one]
SelectMenu (Select a squad) [Ability to select one]
Button (Acts as a submit button)

When I click the submit button, what is the best way to grab all information that was provided for the two selects at this point? Even some pseudo code would be amazing, just some kind of hint on how to successfully pull that off.

Basically just need it to act like a website form where they fill out select menus then grab that data once they click the button. Apologies if it's a newbish question.

SQKo commented 1 year ago

While interactions doesn't work that way, you can have 3 action rows to have your both select menus and button in one message. You can store each option selected on each interaction and continue the process once the submit button is clicked. gambar

The challenge here while you can defer each select, you will eventually need to respond/delete the interaction message in 15 minutes (an ephemeral message might be sufficient) otherwise it will be interaction failed, and wont be good UX for those who try to submit it later.

This is an incomplete code, you need to figure out by your own on how to deal with the behavior (with the ids or the listeners):

$builder = MessageBuilder::new();
$divsel = SelectMenu::new();
$divsel->addOption(Option::new('Division 1', '1'));
$divsel->addOption(Option::new('Division 2', '2'));
$sqdsel = SelectMenu::new();
$builder->addComponent($divsel);
$sqdsel->addOption(Option::new('Squad A', 'A'));
$sqdsel->addOption(Option::new('Squad B', 'B'));
$builder->addComponent($sqdsel);
$ar = ActionRow::new();
$submit = Button::new(Button::STYLE_PRIMARY, 'button_id')->setLabel('Submit');
$ar->addComponent($submit);
$builder->addComponent($ar);
$channel->sendMessage($builder);

$divsel->setListener(function (Interaction $interaction) {
    // store division
}, $discord);
$sqdsel->setListener(function (Interaction $interaction) {
    // store squad
}, $discord);
$submit->setListener(function (Interaction $interaction) {
    $interaction->sendFollowUpMessage(...);
    // proceed submit
}, $discord);