Skip to content

Commit dab1c31

Browse files
Added /preferences list & moved /preferences to /preferences set
1 parent d156b1c commit dab1c31

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.javadiscord.javabot.systems.user_preferences.commands;
2+
3+
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
4+
import net.dv8tion.jda.api.interactions.commands.build.Commands;
5+
6+
/**
7+
* <h3>This class represents the /preferences command.</h3>
8+
*/
9+
public class PreferencesCommand extends SlashCommand {
10+
/**
11+
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
12+
*/
13+
public PreferencesCommand() {
14+
setSlashCommandData(Commands.slash("preferences", "Contains commands for managing user preferences.")
15+
.setGuildOnly(true)
16+
);
17+
addSubcommands(new PreferencesListSubcommand(), new PreferencesSetSubcommand());
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
package net.javadiscord.javabot.systems.user_preferences.commands;
3+
4+
import com.dynxsty.dih4jda.interactions.commands.SlashCommand;
5+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
6+
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
7+
import net.javadiscord.javabot.Bot;
8+
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceManager;
9+
import net.javadiscord.javabot.systems.user_preferences.model.Preference;
10+
import net.javadiscord.javabot.util.Responses;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import java.util.Arrays;
14+
import java.util.stream.Collectors;
15+
16+
/**
17+
* <h3>This class represents the /preferences list command.</h3>
18+
*/
19+
public class PreferencesListSubcommand extends SlashCommand.Subcommand {
20+
/**
21+
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
22+
*/
23+
public PreferencesListSubcommand() {
24+
setSubcommandData(new SubcommandData("list", "Shows all your preferences."));
25+
}
26+
27+
@Override
28+
public void execute(@NotNull SlashCommandInteractionEvent event) {
29+
UserPreferenceManager manager = new UserPreferenceManager(Bot.getDataSource());
30+
String preferences = Arrays.stream(Preference.values())
31+
.map(p -> String.format("%s (%s)", p, manager.getOrCreate(event.getUser().getIdLong(), p).isEnabled() ? "Enabled" : "Disabled"))
32+
.collect(Collectors.joining("\n"));
33+
Responses.info(event, String.format("%s's Preferences", event.getUser().getName()), preferences).queue();
34+
}
35+
}

src/main/java/net/javadiscord/javabot/systems/user_preferences/commands/UserPreferenceCommand.java renamed to src/main/java/net/javadiscord/javabot/systems/user_preferences/commands/PreferencesSetSubcommand.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package net.javadiscord.javabot.systems.user_preferences.commands;
23

34
import com.dynxsty.dih4jda.interactions.commands.AutoCompletable;
@@ -9,7 +10,7 @@
910
import net.dv8tion.jda.api.interactions.commands.Command;
1011
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
1112
import net.dv8tion.jda.api.interactions.commands.OptionType;
12-
import net.dv8tion.jda.api.interactions.commands.build.Commands;
13+
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
1314
import net.javadiscord.javabot.Bot;
1415
import net.javadiscord.javabot.systems.user_preferences.UserPreferenceManager;
1516
import net.javadiscord.javabot.systems.user_preferences.model.Preference;
@@ -20,14 +21,14 @@
2021
import java.util.List;
2122

2223
/**
23-
* <h3>This class represents the /preferences command.</h3>
24+
* <h3>This class represents the /preferences set command.</h3>
2425
*/
25-
public class UserPreferenceCommand extends SlashCommand implements AutoCompletable {
26+
public class PreferencesSetSubcommand extends SlashCommand.Subcommand implements AutoCompletable {
2627
/**
2728
* The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
2829
*/
29-
public UserPreferenceCommand() {
30-
setSlashCommandData(Commands.slash("preferences", "Allows you to set some preferences!")
30+
public PreferencesSetSubcommand() {
31+
setSubcommandData(new SubcommandData("set", "Allows you to set your preferences!")
3132
.addOption(OptionType.INTEGER, "preference", "The preference to set.", true, true)
3233
.addOption(OptionType.BOOLEAN, "state", "The state of the specified preference.", true)
3334
);
@@ -45,9 +46,9 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
4546
boolean state = stateMapping.getAsBoolean();
4647
UserPreferenceManager manager = new UserPreferenceManager(Bot.getDataSource());
4748
if (manager.setOrCreate(event.getUser().getIdLong(), preference, state)) {
48-
Responses.info(event, "Preference Updated", "Successfully set `%s` to `%s`!", preference, state).queue();
49+
Responses.info(event, "Preference Updated", "Successfully %s `%s`!", state ? "enabled" : "disabled", preference).queue();
4950
} else {
50-
Responses.error(event, "Could not update `%s` to `%s`.", preference, state).queue();
51+
Responses.error(event, "Could not %s `%s`.", state ? "enable" : "disable", preference).queue();
5152
}
5253
}
5354

0 commit comments

Comments
 (0)