1
1
package dev .nincodedo .ninbot .components .users ;
2
2
3
3
import dev .nincodedo .ninbot .components .users .UserCommandName .Subcommand ;
4
- import dev .nincodedo .nincord .Emojis ;
5
4
import dev .nincodedo .nincord .command .slash .SlashSubCommand ;
5
+ import dev .nincodedo .nincord .config .db .component .ComponentConfiguration ;
6
+ import dev .nincodedo .nincord .config .db .component .ComponentService ;
6
7
import dev .nincodedo .nincord .message .MessageExecutor ;
7
8
import dev .nincodedo .nincord .message .SlashCommandEventMessageExecutor ;
9
+ import lombok .RequiredArgsConstructor ;
10
+ import net .dv8tion .jda .api .EmbedBuilder ;
8
11
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
9
- import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
10
- import net .dv8tion .jda .api .interactions .commands .OptionType ;
11
- import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
12
12
import net .dv8tion .jda .api .interactions .commands .build .SubcommandData ;
13
+ import net .dv8tion .jda .api .interactions .components .ActionRow ;
14
+ import net .dv8tion .jda .api .interactions .components .selections .SelectOption ;
15
+ import net .dv8tion .jda .api .interactions .components .selections .StringSelectMenu ;
16
+ import net .dv8tion .jda .api .utils .messages .MessageEditBuilder ;
17
+ import org .apache .commons .text .WordUtils ;
13
18
import org .jetbrains .annotations .NotNull ;
14
19
import org .springframework .stereotype .Component ;
15
20
16
21
import java .util .Arrays ;
17
22
import java .util .List ;
18
23
19
24
@ Component
25
+ @ RequiredArgsConstructor
20
26
public class UserCommand implements SlashSubCommand <Subcommand > {
21
27
22
- private UserService userService ;
23
-
24
- public UserCommand (UserService userService ) {
25
- this .userService = userService ;
26
- }
28
+ private final UserService userService ;
29
+ private final ComponentService componentService ;
27
30
28
31
@ Override
29
32
public MessageExecutor execute (@ NotNull SlashCommandInteractionEvent event ,
30
33
@ NotNull SlashCommandEventMessageExecutor messageExecutor , @ NotNull Subcommand subcommand ) {
31
- if (subcommand == Subcommand .BIRTHDAY ) {
32
- updateBirthday (event );
33
- messageExecutor .addEphemeralMessage (Emojis .THUMBS_UP );
34
- } else if (subcommand == Subcommand .ANNOUNCEMENT ) {
35
- toggleAnnouncement (event .getMember ().getId ());
36
- messageExecutor .addEphemeralMessage (Emojis .THUMBS_UP );
34
+ if (subcommand == Subcommand .FEATURES ) {
35
+ event .deferReply (true ).queue ();
36
+ var userToggleableComponents = componentService .findUserToggleableComponents ();
37
+
38
+ var usersDisabledComponents = userService .getUserById (event .getUser ().getId ()).getUserSettings ()
39
+ .stream ()
40
+ .filter (ComponentConfiguration ::getDisabled )
41
+ .map (ComponentConfiguration ::getComponent )
42
+ .map (component -> SelectOption .of (createSelectOptionLabel (component ),
43
+ createSelectOptionValue (component )))
44
+ .toList ();
45
+
46
+ var selectOptions = userToggleableComponents .stream ()
47
+ .map (component -> SelectOption .of (createSelectOptionLabel (component ),
48
+ createSelectOptionValue (component )))
49
+ .toList ();
50
+
51
+ var editedMessage = new MessageEditBuilder ().setReplace (true )
52
+ .setEmbeds (new EmbedBuilder ().setTitle ("Ninbot Feature User Settings" )
53
+ .appendDescription ("This is a list of all Ninbot features you currently have disabled. "
54
+ + "Add items to the list to disable those features for your user in any server "
55
+ + "with Ninbot." )
56
+ .build ())
57
+ .setComponents (ActionRow .of (StringSelectMenu .create ("user-disabled-id" )
58
+ .addOptions (selectOptions )
59
+ .setRequiredRange (0 , userToggleableComponents .size ())
60
+ .setDefaultOptions (usersDisabledComponents )
61
+ .build ()))
62
+ .build ();
63
+
64
+ event .getHook ().editOriginal (editedMessage ).queue ();
37
65
}
38
66
return messageExecutor ;
39
67
}
40
68
41
- private void toggleAnnouncement (String userId ) {
42
- userService .toggleBirthdayAnnouncement (userId );
69
+ private @ NotNull String createSelectOptionValue (dev .nincodedo .nincord .config .db .component .Component component ) {
70
+ return String .format ("component-%s-%s" , component .getName ().replace ('-' , '_' ),
71
+ component .getId ());
43
72
}
44
73
45
- private void updateBirthday (SlashCommandInteractionEvent event ) {
46
- var birthday = event .getOption (UserCommandName .Option .MONTH .get (), OptionMapping ::getAsString ) + "-"
47
- + event .getOption (UserCommandName .Option .DAY .get (), OptionMapping ::getAsString );
48
- var userId = event .getMember ().getId ();
49
- var guildId = event .getGuild ().getId ();
50
- userService .updateBirthday (userId , guildId , birthday );
74
+ private String createSelectOptionLabel (dev .nincodedo .nincord .config .db .component .Component component ) {
75
+ return WordUtils .capitalizeFully (component .getName ()
76
+ .replace ('-' , ' ' ));
51
77
}
52
78
53
79
@ Override
@@ -57,16 +83,8 @@ public String getName() {
57
83
58
84
@ Override
59
85
public List <SubcommandData > getSubcommandDatas () {
60
- return Arrays .asList (
61
- new SubcommandData (Subcommand .BIRTHDAY .get (), "Set your birthday for announcements." )
62
- .addOptions (new OptionData (OptionType .INTEGER , UserCommandName .Option .MONTH .get (), "Month of "
63
- + "your birthday." ,
64
- true , true ).setMinValue (1 ).setMaxValue (12 ))
65
- .addOptions (new OptionData (OptionType .INTEGER , UserCommandName .Option .DAY .get (), "Day of your"
66
- + " birthday." , true , true ).setMinValue (1 )
67
- .setMaxValue (31 )),
68
- new SubcommandData (Subcommand .ANNOUNCEMENT .get (), "Toggles your birthday announcement"
69
- + " on or off." ));
86
+ return Arrays .asList (new SubcommandData (Subcommand .FEATURES .get (), "Opt in or out of various Ninbot "
87
+ + "features." ));
70
88
}
71
89
72
90
@ Override
0 commit comments