Skip to content

Commit 15c0115

Browse files
Tais993Zabuzard
andauthored
Too many roles bugfix role-select (#404)
* Added Jetbrains Annotations dependency * Added vararg utility methods in SlashCommandAdapter * Full rework of RoleSelectCommand The new RoleSelectCommand takes vararg of Roles, and generates the embed based on that. * Fixed issues after rebase. * Fixed some issues from CR * Bugfixes with jda/discord issues on the options * Some polishing * Polish, improvements, bugfixes Co-authored-by: Zabuzard <zabuza.dev@gmail.com>
1 parent 969849b commit 15c0115

File tree

3 files changed

+233
-197
lines changed

3 files changed

+233
-197
lines changed

application/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ shadowJar {
3939
}
4040

4141
dependencies {
42+
implementation 'org.jetbrains:annotations:23.0.0'
43+
4244
implementation project(':database')
4345

4446
implementation 'net.dv8tion:JDA:5.0.0-alpha.9'

application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
44
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
55
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
6+
import net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload;
7+
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
68
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
79
import net.dv8tion.jda.api.interactions.commands.build.Commands;
10+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
811
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
912
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Range;
14+
import org.jetbrains.annotations.Unmodifiable;
1015
import org.togetherjava.tjbot.commands.componentids.ComponentId;
1116
import org.togetherjava.tjbot.commands.componentids.ComponentIdGenerator;
1217
import org.togetherjava.tjbot.commands.componentids.Lifespan;
1318

1419
import java.util.Arrays;
1520
import java.util.List;
1621
import java.util.Objects;
22+
import java.util.function.Function;
23+
import java.util.stream.IntStream;
1724

1825
/**
1926
* Adapter implementation of a {@link SlashCommand}. The minimal setup only requires implementation
@@ -68,9 +75,9 @@ public abstract class SlashCommandAdapter implements SlashCommand {
6875
* Creates a new adapter with the given data.
6976
*
7077
* @param name the name of this command, requirements for this are documented in
71-
* {@link CommandData#CommandData(String, String)}
78+
* {@link SlashCommandData#setName(String)}
7279
* @param description the description of this command, requirements for this are documented in
73-
* {@link CommandData#CommandData(String, String)}
80+
* {@link SlashCommandData#setDescription(String)}
7481
* @param visibility the visibility of the command
7582
*/
7683
protected SlashCommandAdapter(@NotNull String name, @NotNull String description,
@@ -157,4 +164,58 @@ public void onSelectionMenu(@NotNull SelectMenuInteractionEvent event,
157164
return Objects.requireNonNull(componentIdGenerator)
158165
.generate(new ComponentId(getName(), Arrays.asList(args)), lifespan);
159166
}
167+
168+
/**
169+
* Copies the given option multiple times.
170+
* <p>
171+
* The generated options are all not required (optional) and have ascending number suffixes on
172+
* their name. For example, if the name of the given option is {@code "foo"}, calling this with
173+
* an amount of {@code 5} would result in a list of options like:
174+
* <ul>
175+
* <li>{@code "foo1"}</li>
176+
* <li>{@code "foo2"}</li>
177+
* <li>{@code "foo3"}</li>
178+
* <li>{@code "foo4"}</li>
179+
* <li>{@code "foo5"}</li>
180+
* </ul>
181+
* <p>
182+
* This can be useful to offer a variable amount of input options for a user, similar to
183+
* <i>varargs</i>.
184+
* <p>
185+
* After generation, the user input can conveniently be parsed back using
186+
* {@link #getMultipleOptionsByNamePrefix(CommandInteractionPayload, String)}.
187+
*
188+
* @param optionData the original option to copy
189+
* @param amount how often to copy the option
190+
* @return the generated list of options
191+
*/
192+
@Unmodifiable
193+
protected static @NotNull List<OptionData> generateMultipleOptions(
194+
@NotNull OptionData optionData, @Range(from = 1, to = 25) int amount) {
195+
String baseName = optionData.getName();
196+
197+
Function<String, OptionData> nameToOption =
198+
name -> new OptionData(optionData.getType(), name, optionData.getDescription());
199+
200+
return IntStream.rangeClosed(1, amount)
201+
.mapToObj(i -> baseName + i)
202+
.map(nameToOption)
203+
.toList();
204+
}
205+
206+
/**
207+
* Gets all options from the given event whose name start with the given prefix.
208+
*
209+
* @param event the event to extract options from
210+
* @param namePrefix the name prefix to search for
211+
* @return all options with the given prefix
212+
*/
213+
@Unmodifiable
214+
protected static @NotNull List<OptionMapping> getMultipleOptionsByNamePrefix(
215+
@NotNull CommandInteractionPayload event, @NotNull String namePrefix) {
216+
return event.getOptions()
217+
.stream()
218+
.filter(option -> option.getName().startsWith(namePrefix))
219+
.toList();
220+
}
160221
}

0 commit comments

Comments
 (0)