Unlimited option of Role in a Slash-Command

Viewed 434

With JDA, I am trying to develop a slash-command on Discord like this:

/command @role1 @role2 @role3...

I want to have an unlimited option of roles, but I can't find any solution to have an array of Role. Have you an idea ?

My actual code:

commands.addCommands(
            new CommandData("command", "list of possible roles")
                    .addOptions(new OptionData(ROLE, "role", "the role"))
    );
1 Answers

Discord does not offer any kind of variadic option types at this time. Your only hope is to use an option of type STRING and getting the mentions from it:

commands.addCommands(
    Commands.slash("command", "list of possible roles")
         .addOption(STRING, "role", "the role")
);

This only works in 5.0.0-alpha.12 which added getMentions().getRoles():

public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
  OptionMapping opt = event.getOption("role");
  List<Role> roles = opt.getMentions().getRoles();
}

Note: The string option still accepts arbitrary strings and other mention types. Be aware of this when handling commands this way.

Related