-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRemindmeCommand.java
More file actions
166 lines (141 loc) · 5.53 KB
/
RemindmeCommand.java
File metadata and controls
166 lines (141 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Skybot, a multipurpose discord bot
* Copyright (C) 2017 Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan & Maurice R S "Sanduhr32"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ml.duncte123.skybot.commands.essentials;
import me.duncte123.durationparser.DurationParser;
import me.duncte123.durationparser.ParsedDuration;
import ml.duncte123.skybot.objects.BaseCommand;
import ml.duncte123.skybot.objects.command.CommandCategory;
import ml.duncte123.skybot.objects.command.CommandContext;
import ml.duncte123.skybot.objects.command.Flag;
import ml.duncte123.skybot.utils.AirUtils;
import javax.annotation.Nonnull;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static me.duncte123.botcommons.messaging.MessageUtils.sendMsg;
public class RemindmeCommand extends BaseCommand {
public RemindmeCommand() {
super(
"remind",
"Creates a reminder for you, add `--channel` to remind you in the current channel",
CommandCategory.UTILS,
"Example: `{prefix}remind Clean your room :/ -t 1d5m",
new String[]{
"remindme",
},
false,
"<reminder> -t <number><w/d/h/m/s>",
true,
2,
List.of(),
List.of(),
new Flag[]{
new Flag(
't',
"time",
"Sets the time for the reminder"
),
new Flag(
'c',
"channel",
"When this flag is set you will be reminded in the channel where you executed the command (reminder is in your DMs by default)"
),
}
);
}
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<String> args = ctx.getArgs();
if (args.size() < 2) {
this.sendUsageInstructions(ctx);
return;
}
final var flags = ctx.getParsedFlags(this);
if (flags.get("undefined").isEmpty()) {
sendMsg(ctx, "What do you want me to remind you of?\nUsage: " + this.getUsageInstructions(ctx));
return;
}
if (!flags.containsKey("t")) {
sendMsg(ctx, "It looks like you forgot to add the time, you can do this with the `--time` flag");
return;
}
final Optional<ParsedDuration> optionalDuration = getDuration(flags);
if (optionalDuration.isEmpty()) {
sendMsg(ctx, "Incorrect duration format");
return;
}
final ParsedDuration duration = optionalDuration.get();
if (duration.getMilis() == 0) {
sendMsg(ctx, "Your specified time is too short or the time syntax is invalid.");
return;
}
if (duration.getSeconds() < 30) {
sendMsg(ctx, "Minimum duration is 30 seconds");
return;
}
if (duration.getDays() > 1460) { // 4 years in days
sendMsg(ctx, "Just keep it below 4 years ok ;)");
return;
}
final String reminder = String.join(" ", flags.get("undefined"));
if (reminder.length() > 255) {
sendMsg(ctx, "The maximum message length is 255 characters");
return;
}
final OffsetDateTime expireDate = AirUtils.getDatabaseDate(duration);
createReminder(ctx, expireDate, reminder, flags, duration);
}
private Optional<ParsedDuration> getDuration(Map<String, List<String>> flags) {
try {
return DurationParser.parse(String.join("", flags.get("t")));
}
catch (IllegalArgumentException ignored) {
return Optional.empty();
}
}
private void createReminder(CommandContext ctx, OffsetDateTime expireDate, String reminder, Map<String, List<String>> flags, ParsedDuration duration) {
final boolean inChannel = flags.containsKey("c");
final String where = inChannel ? " here" : "";
ctx.getDatabaseAdapter().createReminder(
ctx.getAuthor().getIdLong(),
reminder,
expireDate,
ctx.getChannel().getIdLong(),
ctx.getMessage().getIdLong(),
ctx.getJDAGuild().getIdLong(),
inChannel,
(success, id) -> {
if (success) {
sendMsg(
ctx,
String.format(
"Got it, I'll remind you%s in _%s_ about \"%s\" (Reminder id %d)",
where,
duration,
reminder,
id
)
);
} else {
sendMsg(ctx, "Something went wrong while creating the reminder, try again later");
}
return null;
});
}
}