4
4
import de .presti .ree6 .commands .CommandEvent ;
5
5
import de .presti .ree6 .commands .interfaces .Command ;
6
6
import de .presti .ree6 .commands .interfaces .ICommand ;
7
+ import de .presti .ree6 .sql .SQLSession ;
7
8
import de .presti .ree6 .sql .entities .ScheduledMessage ;
8
9
import net .dv8tion .jda .api .Permission ;
9
10
import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
10
11
import net .dv8tion .jda .api .interactions .commands .OptionType ;
11
12
import net .dv8tion .jda .api .interactions .commands .build .CommandData ;
13
+ import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
12
14
import net .dv8tion .jda .api .interactions .commands .build .SubcommandData ;
13
15
import net .dv8tion .jda .internal .interactions .CommandDataImpl ;
14
16
import org .apache .commons .validator .GenericValidator ;
15
17
import org .apache .commons .validator .routines .DateValidator ;
16
18
17
19
import java .time .Duration ;
20
+ import java .time .Instant ;
18
21
import java .time .LocalDateTime ;
19
22
import java .time .ZoneId ;
20
23
import java .time .format .DateTimeFormatter ;
21
24
import java .time .format .DateTimeParseException ;
25
+ import java .util .Map ;
22
26
23
27
/**
24
28
* Schedule a message to a specific time or a repeating time.
@@ -46,29 +50,67 @@ public void onPerform(CommandEvent commandEvent) {
46
50
return ;
47
51
}
48
52
49
- OptionMapping repeat = commandEvent .getOption ( "repeat" );
53
+ String subCommand = commandEvent .getSlashCommandInteractionEvent (). getSubcommandName ( );
50
54
51
- OptionMapping month = commandEvent .getOption ("month" );
52
- OptionMapping day = commandEvent .getOption ("day" );
53
- OptionMapping hour = commandEvent .getOption ("hour" );
54
- OptionMapping minute = commandEvent .getOption ("minute" );
55
+ switch (subCommand ) {
55
56
56
- long fullTime = 0 ;
57
- if (month != null ) fullTime += Duration .ofDays (31 * month .getAsLong ()).toMillis ();
58
- if (day != null ) fullTime += Duration .ofDays (day .getAsLong ()).toMillis ();
59
- if (hour != null ) fullTime += Duration .ofHours (hour .getAsLong ()).toMillis ();
60
- if (minute != null ) fullTime += Duration .ofMinutes (minute .getAsLong ()).toMillis ();
57
+ case "list" -> {
58
+ StringBuilder stringBuilder = new StringBuilder ();
61
59
62
- if (fullTime < Duration .ofMinutes (1 ).toDays ()) {
63
- commandEvent .reply (commandEvent .getResource ("message.default.dateError.notEnough" ));
64
- return ;
65
- }
60
+ for (ScheduledMessage scheduledMessage : SQLSession .getSqlConnector ().getSqlWorker ()
61
+ .getEntityList (new ScheduledMessage (), "SELECT * FROM ScheduledMessage WHERE guild = :gid " ,
62
+ Map .of ("gid" , commandEvent .getGuild ().getIdLong ()))) {
63
+ stringBuilder .append (scheduledMessage .getId ()).append (" " ).append ("-" ).append (" " )
64
+ .append (scheduledMessage .getMessage ()).append (" " )
65
+ .append ("->" ).append (" " )
66
+ .append (Instant .ofEpochMilli (scheduledMessage .getDelayAmount ())
67
+ .atZone (ZoneId .systemDefault ()).format (DateTimeFormatter .ofPattern ("dd.MM.yyyy HH/mm" )));
68
+ }
69
+ // TODO:: list response message.
70
+ }
71
+
72
+ case "delete" -> {
73
+ OptionMapping id = commandEvent .getOption ("id" );
74
+
75
+ ScheduledMessage scheduledMessage = SQLSession .getSqlConnector ().getSqlWorker ()
76
+ .getEntity (new ScheduledMessage (), "SELECT * FROM ScheduledMessage WHERE guild = :gid AND Id = :id" ,
77
+ Map .of ("gid" , commandEvent .getGuild ().getIdLong (), "id" , id .getAsLong ()));
78
+
79
+ if (scheduledMessage != null ) {
80
+ // TODO:: add success message.
81
+ SQLSession .getSqlConnector ().getSqlWorker ().deleteEntity (scheduledMessage );
82
+ } else {
83
+ // TODO:: add failed message.
84
+ }
85
+ }
66
86
67
- // TODO:: check for webhook and use existing one.
68
- ScheduledMessage scheduledMessage = new ScheduledMessage ();
69
- scheduledMessage .setDelayAmount (fullTime );
70
- scheduledMessage .setRepeated (repeat .getAsBoolean ());
71
- // TODO:: add success message.
87
+ case "create" -> {
88
+ OptionMapping repeat = commandEvent .getOption ("repeat" );
89
+ OptionMapping month = commandEvent .getOption ("month" );
90
+ OptionMapping day = commandEvent .getOption ("day" );
91
+ OptionMapping hour = commandEvent .getOption ("hour" );
92
+ OptionMapping minute = commandEvent .getOption ("minute" );
93
+
94
+ long fullTime = 0 ;
95
+ if (month != null ) fullTime += Duration .ofDays (31 * month .getAsLong ()).toMillis ();
96
+ if (day != null ) fullTime += Duration .ofDays (day .getAsLong ()).toMillis ();
97
+ if (hour != null ) fullTime += Duration .ofHours (hour .getAsLong ()).toMillis ();
98
+ if (minute != null ) fullTime += Duration .ofMinutes (minute .getAsLong ()).toMillis ();
99
+
100
+ if (fullTime < Duration .ofMinutes (1 ).toDays ()) {
101
+ commandEvent .reply (commandEvent .getResource ("message.default.dateError.notEnough" ));
102
+ return ;
103
+ }
104
+
105
+ // TODO:: check for webhook and use existing one.
106
+ ScheduledMessage scheduledMessage = new ScheduledMessage ();
107
+ scheduledMessage .setDelayAmount (fullTime );
108
+ scheduledMessage .setRepeated (repeat .getAsBoolean ());
109
+ // TODO:: add success message.
110
+ }
111
+
112
+ default -> commandEvent .reply (commandEvent .getResource ("message.default.invalidOption" ));
113
+ }
72
114
}
73
115
74
116
/**
@@ -77,11 +119,15 @@ public void onPerform(CommandEvent commandEvent) {
77
119
@ Override
78
120
public CommandData getCommandData () {
79
121
return new CommandDataImpl ("schedule" , "command.description.schedule" )
80
- .addOption (OptionType .INTEGER , "month" , "The months of the delay." , false )
81
- .addOption (OptionType .INTEGER , "day" , "The days of the delay." , false )
82
- .addOption (OptionType .INTEGER , "hour" , "The hours of the delay." , false )
83
- .addOption (OptionType .INTEGER , "minute" , "The minutes of the delay." , false )
84
- .addOption (OptionType .BOOLEAN , "repeat" , "If the schedule should be repeated." , true );
122
+ .addSubcommands (new SubcommandData ("create" , "Create a new scheduled Message." )
123
+ .addOption (OptionType .INTEGER , "month" , "The months of the delay." , false )
124
+ .addOption (OptionType .INTEGER , "day" , "The days of the delay." , false )
125
+ .addOption (OptionType .INTEGER , "hour" , "The hours of the delay." , false )
126
+ .addOption (OptionType .INTEGER , "minute" , "The minutes of the delay." , false )
127
+ .addOption (OptionType .BOOLEAN , "repeat" , "If the schedule should be repeated." , true ),
128
+ new SubcommandData ("list" , "List all scheduled Messages." ),
129
+ new SubcommandData ("delete" , "Delete a scheduled Message." )
130
+ .addOptions (new OptionData (OptionType .INTEGER , "id" , "The ID of the scheduled Message" , true ).setMinValue (1 )));
85
131
}
86
132
87
133
/**
0 commit comments