I hate regex
This commit is contained in:
parent
e1d452d5f8
commit
cae774a82f
3 changed files with 50 additions and 16 deletions
|
@ -27,6 +27,10 @@ public class Timeout extends ApplicationCommand {
|
||||||
@AppOption(name = "member") Member member,
|
@AppOption(name = "member") Member member,
|
||||||
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1m and 7d") String duration) {
|
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1m and 7d") String duration) {
|
||||||
Duration timeoutDuration = DurationUtils.parseDuration(duration);
|
Duration timeoutDuration = DurationUtils.parseDuration(duration);
|
||||||
|
if (!DurationUtils.isDurationBetween(timeoutDuration, Duration.ofMinutes(1), Duration.ofDays(7))) {
|
||||||
|
event.replyFormat("Invalid duration %s!, must be between 1m and 7d", duration).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
member.timeoutFor(timeoutDuration).queue();
|
member.timeoutFor(timeoutDuration).queue();
|
||||||
event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue();
|
event.replyFormat("Timed out %s for %s", member.getAsMention(), duration).queue();
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,11 +26,15 @@ public class Poll extends ApplicationCommand {
|
||||||
)
|
)
|
||||||
public void poll(GuildSlashEvent event,
|
public void poll(GuildSlashEvent event,
|
||||||
@AppOption(name = "title") String pollTitle,
|
@AppOption(name = "title") String pollTitle,
|
||||||
@AppOption(name = "duration", description = "ex: 2h5m, must be between 1h and 7d") String duration,
|
@AppOption(name = "duration", description = "ex: 2h, must be between 1h and 7d") String duration,
|
||||||
@AppOption(name = "options", description = "Comma-seperated poll options") String options,
|
@AppOption(name = "options", description = "Comma-seperated poll options") String options,
|
||||||
@Optional @AppOption(name = "multiple-choice", description = "Allow multiple choices? (defaults to false)") Boolean multiChoice) {
|
@Optional @AppOption(name = "multiple-choice", description = "Allow multiple choices? (defaults to false)") Boolean multiChoice) {
|
||||||
Boolean pollMultiChoice = Objects.requireNonNullElse(multiChoice, false);
|
Boolean pollMultiChoice = Objects.requireNonNullElse(multiChoice, false);
|
||||||
Duration pollDuration = DurationUtils.parseDuration(duration);
|
Duration pollDuration = DurationUtils.parseDuration(duration);
|
||||||
|
if (!DurationUtils.isDurationBetween(pollDuration, Duration.ofHours(1), Duration.ofDays(7))) {
|
||||||
|
event.replyFormat("Invalid duration %s!, must be between 1h and 7d", duration).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
String[] pollOptions = options.split(",");
|
String[] pollOptions = options.split(",");
|
||||||
MessagePollBuilder poll = MessagePollData.builder(pollTitle)
|
MessagePollBuilder poll = MessagePollData.builder(pollTitle)
|
||||||
.setDuration(pollDuration);
|
.setDuration(pollDuration);
|
||||||
|
|
|
@ -7,26 +7,52 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class DurationUtils {
|
public class DurationUtils {
|
||||||
private static final Pattern timePattern = Pattern.compile("(\\d+)([dhms])?");
|
private static final Pattern DURATION_PATTERN = Pattern.compile(
|
||||||
public static Duration parseDuration(String input) throws IllegalArgumentException {
|
"(\\d+d\\s*)?(\\d+h\\s*)?(\\d+m\\s*)?(\\d+s\\s*)?");
|
||||||
Matcher matcher = timePattern.matcher(input);
|
|
||||||
|
|
||||||
if (!matcher.matches()) {
|
public static Duration parseDuration(String durationString) {
|
||||||
throw new IllegalArgumentException("Invalid duration format");
|
durationString = durationString.trim();
|
||||||
|
|
||||||
|
Matcher matcher = DURATION_PATTERN.matcher(durationString);
|
||||||
|
long days = 0;
|
||||||
|
long hours = 0;
|
||||||
|
long minutes = 0;
|
||||||
|
long seconds = 0;
|
||||||
|
|
||||||
|
if (matcher.matches()) {
|
||||||
|
// Extract days if present
|
||||||
|
if (matcher.group(1) != null) {
|
||||||
|
days = Long.parseLong(matcher.group(1).replace("d", "").trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract hours if present
|
||||||
|
if (matcher.group(2) != null) {
|
||||||
|
hours = Long.parseLong(matcher.group(2).replace("h", "").trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract minutes if present
|
||||||
|
if (matcher.group(3) != null) {
|
||||||
|
minutes = Long.parseLong(matcher.group(3).replace("m", "").trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract seconds if present
|
||||||
|
if (matcher.group(4) != null) {
|
||||||
|
seconds = Long.parseLong(matcher.group(4).replace("s", "").trim());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int value = Integer.parseInt(matcher.group(1));
|
return Duration.ofDays(days)
|
||||||
String unit = matcher.group(2);
|
.plusHours(hours)
|
||||||
|
.plusMinutes(minutes)
|
||||||
return switch (unit == null || unit.isEmpty() ? "m" : unit.toLowerCase()) {
|
.plusSeconds(seconds);
|
||||||
case "d" -> Duration.ofDays(value);
|
|
||||||
case "h" -> Duration.ofHours(value);
|
|
||||||
case "m" -> Duration.ofMinutes(value);
|
|
||||||
case "s" -> Duration.ofSeconds(value);
|
|
||||||
default -> throw new IllegalArgumentException("Invalid unit");
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isDurationBetween(Duration target, Duration lowerThreshold, Duration upperThreshold) {
|
||||||
|
if (target == lowerThreshold || target == upperThreshold) return true;
|
||||||
|
return !target.isNegative() &&
|
||||||
|
(target.compareTo(lowerThreshold) >= 0) &&
|
||||||
|
(target.compareTo(upperThreshold) <= 0);
|
||||||
|
}
|
||||||
public static String getTimeDifference(OffsetDateTime offsetDateTime) {
|
public static String getTimeDifference(OffsetDateTime offsetDateTime) {
|
||||||
if (offsetDateTime == null) return "0 seconds";
|
if (offsetDateTime == null) return "0 seconds";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue