|
1 | 1 | package net.javadiscord.javabot.systems.configuration; |
2 | 2 |
|
| 3 | +import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; |
| 4 | +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; |
3 | 5 | import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 6 | +import net.dv8tion.jda.api.interactions.AutoCompleteQuery; |
4 | 7 | import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
5 | 8 | import net.dv8tion.jda.api.interactions.commands.OptionType; |
6 | 9 | import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
7 | | -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; |
| 10 | +import net.dv8tion.jda.api.interactions.components.text.TextInput; |
| 11 | +import net.dv8tion.jda.api.interactions.components.text.TextInputStyle; |
| 12 | +import net.dv8tion.jda.api.interactions.modals.Modal; |
| 13 | +import net.dv8tion.jda.api.interactions.modals.ModalMapping; |
| 14 | +import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction; |
| 15 | +import net.javadiscord.javabot.annotations.AutoDetectableComponentHandler; |
8 | 16 | import net.javadiscord.javabot.data.config.BotConfig; |
9 | 17 | import net.javadiscord.javabot.data.config.GuildConfig; |
10 | 18 | import net.javadiscord.javabot.data.config.UnknownPropertyException; |
11 | 19 | import net.javadiscord.javabot.util.Responses; |
| 20 | +import xyz.dynxsty.dih4jda.interactions.AutoCompletable; |
| 21 | +import xyz.dynxsty.dih4jda.interactions.components.ModalHandler; |
| 22 | +import xyz.dynxsty.dih4jda.util.ComponentIdBuilder; |
| 23 | + |
| 24 | +import java.util.List; |
12 | 25 |
|
13 | 26 | import javax.annotation.Nonnull; |
14 | 27 |
|
15 | 28 | /** |
16 | 29 | * Subcommand that allows staff-members to edit the bot's configuration. |
17 | 30 | */ |
18 | | -public class SetConfigSubcommand extends ConfigSubcommand { |
| 31 | +@AutoDetectableComponentHandler("config-set") |
| 32 | +public class SetConfigSubcommand extends ConfigSubcommand implements ModalHandler, AutoCompletable { |
19 | 33 | /** |
20 | 34 | * The constructor of this class, which sets the corresponding {@link SubcommandData}. |
21 | 35 | * @param botConfig The main configuration of the bot |
22 | 36 | */ |
23 | 37 | public SetConfigSubcommand(BotConfig botConfig) { |
24 | 38 | super(botConfig); |
25 | 39 | setCommandData(new SubcommandData("set", "Sets the value of a configuration property.") |
26 | | - .addOption(OptionType.STRING, "property", "The name of a property.", true) |
27 | | - .addOption(OptionType.STRING, "value", "The value to set for the property.", true) |
| 40 | + .addOption(OptionType.STRING, "property", "The name of a property.", true, true) |
| 41 | + .addOption(OptionType.STRING, "value", "The value to set for the property.", false) |
28 | 42 | ); |
29 | | - setRequiredUsers(botConfig.getSystems().getAdminConfig().getAdminUsers()); |
30 | 43 | } |
31 | 44 |
|
32 | 45 | @Override |
33 | | - public ReplyCallbackAction handleConfigSubcommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull GuildConfig config) throws UnknownPropertyException { |
| 46 | + public InteractionCallbackAction<?> handleConfigSubcommand(@Nonnull SlashCommandInteractionEvent event, @Nonnull GuildConfig config) throws UnknownPropertyException { |
34 | 47 | OptionMapping propertyOption = event.getOption("property"); |
35 | 48 | OptionMapping valueOption = event.getOption("value"); |
36 | | - if (propertyOption == null || valueOption == null) { |
| 49 | + if (propertyOption == null) { |
37 | 50 | return Responses.replyMissingArguments(event); |
38 | 51 | } |
39 | 52 | String property = propertyOption.getAsString().trim(); |
| 53 | + GuildConfig guildConfig = botConfig.get(event.getGuild()); |
| 54 | + if (valueOption == null) { |
| 55 | + Object resolved = guildConfig.resolve(property); |
| 56 | + if (resolved == null) { |
| 57 | + return Responses.error(event, "Config `%s` not found", property); |
| 58 | + } |
| 59 | + return event.replyModal( |
| 60 | + Modal.create(ComponentIdBuilder.build("config-set", property), "Change configuration value") |
| 61 | + .addActionRow(TextInput.create("value", "new value", TextInputStyle.PARAGRAPH) |
| 62 | + .setValue(String.valueOf(resolved)) |
| 63 | + .build()) |
| 64 | + .build()); |
| 65 | + } |
40 | 66 | String valueString = valueOption.getAsString().trim(); |
41 | | - botConfig.get(event.getGuild()).set(property, valueString); |
| 67 | + guildConfig.set(property, valueString); |
42 | 68 | return Responses.success(event, "Configuration Updated", "The property `%s` has been set to `%s`.", property, valueString); |
43 | 69 | } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void handleModal(ModalInteractionEvent event, List<ModalMapping> values) { |
| 73 | + String[] id = ComponentIdBuilder.split(event.getModalId()); |
| 74 | + String property = id[1]; |
| 75 | + String valueString = event.getValue("value").getAsString(); |
| 76 | + GuildConfig guildConfig = botConfig.get(event.getGuild()); |
| 77 | + try { |
| 78 | + guildConfig.set(property, valueString); |
| 79 | + Responses.success(event, "Configuration Updated", "The property `%s` has been set to `%s`.", property, valueString).queue(); |
| 80 | + } catch (UnknownPropertyException e) { |
| 81 | + Responses.error(event, "Property not found: %s", property).queue(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void handleAutoComplete(CommandAutoCompleteInteractionEvent event, AutoCompleteQuery target) { |
| 87 | + if (target.getName().equals("property")) { |
| 88 | + String partialText = target.getValue(); |
| 89 | + handlePropertyAutocomplete(event, partialText); |
| 90 | + } |
| 91 | + } |
44 | 92 | } |
0 commit comments