Skip to content

Commit 11a31ff

Browse files
committed
autocomplete of /config get
1 parent 40fc3b5 commit 11a31ff

File tree

4 files changed

+67
-44
lines changed

4 files changed

+67
-44
lines changed

src/main/java/net/javadiscord/javabot/systems/configuration/ConfigCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ConfigCommand extends SlashCommand implements CommandModerationPerm
1717
* @param getConfigSubcommand /config get
1818
* @param setConfigSubcommand /config set
1919
*/
20-
public ConfigCommand(BotConfig botConfig, ExportConfigSubcommand exportConfigSubcommand, GetConfigSubcommand getConfigSubcommand, SetConfigSubcommand setConfigSubcommand) {
20+
public ConfigCommand(BotConfig botConfig, ExportConfigSubcommand exportConfigSubcommand, GetConfigSubcommand getConfigSubcommand, ConfigSubcommand setConfigSubcommand) {
2121
setModerationSlashCommandData(Commands.slash("config", "Administrative Commands for managing the bot's configuration."));
2222
addSubcommands(exportConfigSubcommand, getConfigSubcommand, setConfigSubcommand);
2323
setRequiredUsers(botConfig.getSystems().getAdminConfig().getAdminUsers());

src/main/java/net/javadiscord/javabot/systems/configuration/ConfigSubcommand.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package net.javadiscord.javabot.systems.configuration;
22

33
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
4+
import xyz.dynxsty.dih4jda.util.AutoCompleteUtils;
5+
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
46
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
7+
import net.dv8tion.jda.api.interactions.commands.Command;
8+
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
59
import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction;
610
import net.javadiscord.javabot.data.config.BotConfig;
711
import net.javadiscord.javabot.data.config.GuildConfig;
@@ -10,6 +14,12 @@
1014
import net.javadiscord.javabot.util.Responses;
1115
import org.jetbrains.annotations.NotNull;
1216

17+
import java.lang.reflect.Field;
18+
import java.lang.reflect.Modifier;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
1323
import javax.annotation.Nonnull;
1424

1525
/**
@@ -45,4 +55,46 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
4555
}
4656

4757
protected abstract InteractionCallbackAction<?> handleConfigSubcommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull GuildConfig config) throws UnknownPropertyException;
58+
59+
/**
60+
* autocompletes a property.
61+
* @param event the {@link CommandAutoCompleteInteractionEvent} used for sending the autocomplete information
62+
* @param partialText the entered text
63+
*/
64+
protected void handlePropertyAutocomplete(CommandAutoCompleteInteractionEvent event, String partialText) {
65+
int lastDot = partialText.lastIndexOf('.');
66+
String parentPropertyName;
67+
String childPropertyName;
68+
if (lastDot == -1) {
69+
parentPropertyName = "";
70+
childPropertyName = partialText;
71+
} else {
72+
parentPropertyName = partialText.substring(0,lastDot);
73+
childPropertyName = partialText.substring(lastDot+1);
74+
}
75+
76+
GuildConfig guildConfig = botConfig.get(event.getGuild());
77+
try {
78+
Object resolved;
79+
if(parentPropertyName.isEmpty()) {
80+
resolved = guildConfig;
81+
} else {
82+
resolved = guildConfig.resolve(parentPropertyName);
83+
}
84+
if (resolved == null || !resolved.getClass().getPackageName().startsWith(GuildConfig.class.getPackageName())) {
85+
event.replyChoices().queue();
86+
return;
87+
}
88+
List<Choice> choices = Arrays.stream(resolved.getClass().getDeclaredFields())
89+
.filter(f -> !Modifier.isTransient(f.getModifiers()))
90+
.map(Field::getName)
91+
.map(name -> parentPropertyName.isEmpty() ? name : parentPropertyName+"."+name)
92+
.map(name -> new Command.Choice(name, name))
93+
.collect(Collectors.toList());
94+
95+
event.replyChoices(AutoCompleteUtils.filterChoices(childPropertyName, choices)).queue();
96+
} catch (UnknownPropertyException e) {
97+
event.replyChoices().queue();
98+
}
99+
}
48100
}

src/main/java/net/javadiscord/javabot/systems/configuration/GetConfigSubcommand.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.javadiscord.javabot.systems.configuration;
22

3+
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
34
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
5+
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
46
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
57
import net.dv8tion.jda.api.interactions.commands.OptionType;
68
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
@@ -9,21 +11,22 @@
911
import net.javadiscord.javabot.data.config.GuildConfig;
1012
import net.javadiscord.javabot.data.config.UnknownPropertyException;
1113
import net.javadiscord.javabot.util.Responses;
14+
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
1215

1316
import javax.annotation.Nonnull;
1417

1518
/**
1619
* Subcommand that allows staff-members to get a single property variable from the guild config.
1720
*/
18-
public class GetConfigSubcommand extends ConfigSubcommand {
21+
public class GetConfigSubcommand extends ConfigSubcommand implements AutoCompletable {
1922
/**
2023
* The constructor of this class, which sets the corresponding {@link SubcommandData}.
2124
* @param botConfig The main configuration of the bot
2225
*/
2326
public GetConfigSubcommand(BotConfig botConfig) {
2427
super(botConfig);
2528
setCommandData(new SubcommandData("get", "Get the current value of a configuration property.")
26-
.addOption(OptionType.STRING, "property", "The name of a property.", true)
29+
.addOption(OptionType.STRING, "property", "The name of a property.", true, true)
2730
);
2831
}
2932

@@ -37,4 +40,12 @@ public ReplyCallbackAction handleConfigSubcommand(@Nonnull SlashCommandInteracti
3740
Object value = config.resolve(property);
3841
return Responses.info(event, "Configuration Property", "The value of the property `%s` is:\n```\n%s\n```", property, value);
3942
}
43+
44+
@Override
45+
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
46+
if (target.getName().equals("property")) {
47+
String partialText = target.getValue();
48+
handlePropertyAutocomplete(event, partialText);
49+
}
50+
}
4051
}

src/main/java/net/javadiscord/javabot/systems/configuration/SetConfigSubcommand.java

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
55
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
66
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
7-
import net.dv8tion.jda.api.interactions.commands.Command;
8-
import net.dv8tion.jda.api.interactions.commands.Command.Choice;
97
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
108
import net.dv8tion.jda.api.interactions.commands.OptionType;
119
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
@@ -21,14 +19,9 @@
2119
import net.javadiscord.javabot.util.Responses;
2220
import xyz.dynxsty.dih4jda.interactions.AutoCompletable;
2321
import xyz.dynxsty.dih4jda.interactions.components.ModalHandler;
24-
import xyz.dynxsty.dih4jda.util.AutoCompleteUtils;
2522
import xyz.dynxsty.dih4jda.util.ComponentIdBuilder;
2623

27-
import java.lang.reflect.Field;
28-
import java.lang.reflect.Modifier;
29-
import java.util.Arrays;
3024
import java.util.List;
31-
import java.util.stream.Collectors;
3225

3326
import javax.annotation.Nonnull;
3427

@@ -93,40 +86,7 @@ public void handleModal(ModalInteractionEvent event, List<ModalMapping> values)
9386
public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) {
9487
if (target.getName().equals("property")) {
9588
String partialText = target.getValue();
96-
int lastDot = partialText.lastIndexOf('.');
97-
String parentPropertyName;
98-
String childPropertyName;
99-
if (lastDot == -1) {
100-
parentPropertyName = "";
101-
childPropertyName = partialText;
102-
} else {
103-
parentPropertyName = partialText.substring(0,lastDot);
104-
childPropertyName = partialText.substring(lastDot+1);
105-
}
106-
107-
GuildConfig guildConfig = botConfig.get(event.getGuild());
108-
try {
109-
Object resolved;
110-
if(parentPropertyName.isEmpty()) {
111-
resolved = guildConfig;
112-
} else {
113-
resolved = guildConfig.resolve(parentPropertyName);
114-
}
115-
if (resolved == null || !resolved.getClass().getPackageName().startsWith(GuildConfig.class.getPackageName())) {
116-
event.replyChoices().queue();
117-
return;
118-
}
119-
List<Choice> choices = Arrays.stream(resolved.getClass().getDeclaredFields())
120-
.filter(f -> !Modifier.isTransient(f.getModifiers()))
121-
.map(Field::getName)
122-
.map(name -> parentPropertyName.isEmpty() ? name : parentPropertyName+"."+name)
123-
.map(name -> new Command.Choice(name, name))
124-
.collect(Collectors.toList());
125-
126-
event.replyChoices(AutoCompleteUtils.filterChoices(childPropertyName, choices)).queue();
127-
} catch (UnknownPropertyException e) {
128-
event.replyChoices().queue();
129-
}
89+
handlePropertyAutocomplete(event, partialText);
13090
}
13191
}
13292
}

0 commit comments

Comments
 (0)