I Am Making A Bot In Java Discord API,
I want to send a Drop Down Menu In A Channel,
There is a MessageChannel Object I Have and I Know How To Send Message By
channel.sendMessage("MESSAGE").queue();
But How I Send A Drop Down Menu?
I Have Tried,
- Making A Class Implementing SelectMenu Interface,
i have overrided all the necessary methods likegetMinValue()
,getPlaceHolder()
I Have Expected, I My Bot Will Send A Drop Down Menu, in a text channel and when I select an option it will send me some result in that channel
CodePudding user response:
Assuming you're using the latest version of JDA (5.0.0-beta.2), the way to send a select menu is this:
Create a
StringSelectMenu
to allow selecting arbitrary strings:// The string here is the id later used to identify the menu on use. Make it unique for each menu. StringSelectMenu.Builder builder = StringSelectMenu.create("menu:id"); // Add one or more options // First parameter is the label that the user will see, the second is the value your bot will receive when this is selected. builder.addOption("Hello World", "hello_world"); // Then once you have configured all you want, build it StringSelectMenu menu = builder.build();
Send the menu using one of the send methods (reply/sendMessage)
// ActionRow is the layout in which your component is arranged (this is currently the only available layout) channel.sendMessageComponents(ActionRow.of(menu)).queue();
Setup an event listener for this menu
public void Listener extends ListenerAdapter { @Override public void onStringSelectInteraction(StringSelectInteractionEvent event) { // same id as before in create(...) if (event.getComponentId().equals("menu:id")) { List<String> selected = event.getValues(); // the values the user selected ... your code to handle the selection ... // Either reply with a message event.reply("Your selection was processed").queue(); // Or by editing the message (replace means it removes the menu) event.editMessage("It was selected").setReplace(true).queue(); } } }
There is also EntitySelectMenu
which works rather similarly, but can be used to select User
/Member
/Role
or GuildChannel
instead of string options.