Skip to content

Commit 0a3bcb6

Browse files
committed
feat: add delimiter in dropdown menu option IDs
This way we are able to add more roles in the future without running into any character limits. Getting the last character of the menu option ID is not the most effective way to go about this. Adding the delimiter in the menu option IDs allows us to treat the ID of the role better and in a safer way.
1 parent 797340a commit 0a3bcb6

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

application/src/main/java/org/togetherjava/tjbot/features/basic/ApplicationCreateCommand.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class ApplicationCreateCommand extends SlashCommandAdapter {
6565
"What makes you a valuable addition to the team? 😎";
6666
private static final int OPTIONAL_ROLES_AMOUNT = 5;
6767
private static final String ROLE_COMPONENT_ID_HEADER = "application-create";
68+
private static final String VALUE_DELIMITER = "_";
6869

6970
private final Cache<Member, OffsetDateTime> applicationSubmitCooldown;
7071
private final Predicate<String> applicationChannelPattern;
@@ -101,13 +102,19 @@ private void generateRoleOptions(SlashCommandData data) {
101102
IntStream.range(0, OPTIONAL_ROLES_AMOUNT).forEach(index -> {
102103
int renderNumber = index + 1;
103104

104-
data.addOption(OptionType.STRING, "title" + renderNumber, "The title of the role");
105-
data.addOption(OptionType.STRING, "description" + renderNumber,
105+
data.addOption(OptionType.STRING, generateOptionId("title", renderNumber),
106+
"The title of the role");
107+
data.addOption(OptionType.STRING, generateOptionId("description", renderNumber),
106108
"The description of the role");
107-
data.addOption(OptionType.STRING, "emoji" + renderNumber, "The emoji of the role");
109+
data.addOption(OptionType.STRING, generateOptionId("emoji", renderNumber),
110+
"The emoji of the role");
108111
});
109112
}
110113

114+
private static String generateOptionId(String name, int id) {
115+
return "%s%s%d".formatted(name, VALUE_DELIMITER, id);
116+
}
117+
111118
@Override
112119
public void onSlashCommand(SlashCommandInteractionEvent event) {
113120
if (!handleHasPermissions(event)) {
@@ -197,11 +204,11 @@ public void onStringSelectSelection(StringSelectInteractionEvent event, List<Str
197204
* @return the amount of roles with missing data
198205
*/
199206
private static long getIncorrectRoleArgsCount(final List<OptionMapping> args) {
200-
final Map<Character, Integer> frequencyMap = new HashMap<>();
207+
final Map<String, Integer> frequencyMap = new HashMap<>();
201208

202209
args.stream()
203210
.map(OptionMapping::getName)
204-
.map(name -> name.charAt(name.length() - 1))
211+
.map(name -> name.split(VALUE_DELIMITER)[1])
205212
.forEach(number -> frequencyMap.merge(number, 1, Integer::sum));
206213

207214
return frequencyMap.values().stream().filter(value -> value != 3).count();
@@ -215,12 +222,12 @@ private static long getIncorrectRoleArgsCount(final List<OptionMapping> args) {
215222
*/
216223
private void addRolesToMenu(StringSelectMenu.Builder menuBuilder,
217224
final List<OptionMapping> args) {
218-
final Map<Character, MenuRole> roles = new HashMap<>();
225+
final Map<String, MenuRole> roles = new HashMap<>();
219226

220227
args.forEach(arg -> {
221228
final String name = arg.getName();
222229
final String argValue = arg.getAsString();
223-
final char roleId = name.charAt(name.length() - 1);
230+
final String roleId = name.split(VALUE_DELIMITER)[1];
224231
MenuRole role = roles.computeIfAbsent(roleId, k -> new MenuRole());
225232

226233
if (name.startsWith("title")) {

0 commit comments

Comments
 (0)