mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-03 15:27:07 +00:00
[AB-xxx] adding support to use @time inputs for duration and instant command parameters
This commit is contained in:
@@ -130,13 +130,14 @@ public class RemindServiceBean implements ReminderService {
|
|||||||
if(remindIn.getSeconds() < 60) {
|
if(remindIn.getSeconds() < 60) {
|
||||||
reminder.setJobTriggerKey(null);
|
reminder.setJobTriggerKey(null);
|
||||||
log.info("Directly scheduling unremind for reminder {}, because it was below the threshold.", reminder.getId());
|
log.info("Directly scheduling unremind for reminder {}, because it was below the threshold.", reminder.getId());
|
||||||
|
long nanos = Math.max(remindIn.toNanos(), Duration.ofSeconds(5).toNanos()); // should be good enough, if its too small, it doesnt find the reminder
|
||||||
instantReminderScheduler.schedule(() -> {
|
instantReminderScheduler.schedule(() -> {
|
||||||
try {
|
try {
|
||||||
self.executeReminder(reminder.getId());
|
self.executeReminder(reminder.getId());
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
log.error("Failed to remind immediately.", exception);
|
log.error("Failed to remind immediately.", exception);
|
||||||
}
|
}
|
||||||
}, remindIn.toNanos(), TimeUnit.NANOSECONDS);
|
}, nanos, TimeUnit.NANOSECONDS);
|
||||||
} else {
|
} else {
|
||||||
HashMap<Object, Object> parameters = new HashMap<>();
|
HashMap<Object, Object> parameters = new HashMap<>();
|
||||||
parameters.put("reminderId", reminder.getId().toString());
|
parameters.put("reminderId", reminder.getId().toString());
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class InstantSlashCommandParameterProvider implements SlashCommandParamet
|
|||||||
return SlashCommandOptionTypeMapping
|
return SlashCommandOptionTypeMapping
|
||||||
.builder()
|
.builder()
|
||||||
.type(Instant.class)
|
.type(Instant.class)
|
||||||
.optionTypes(Arrays.asList(OptionType.INTEGER))
|
.optionTypes(Arrays.asList(OptionType.STRING))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ public class DurationFormatException extends AbstractoRunTimeException implement
|
|||||||
private final DurationFormatExceptionModel model;
|
private final DurationFormatExceptionModel model;
|
||||||
|
|
||||||
|
|
||||||
public DurationFormatException(String wrongFormat, List<String> validFormats) {
|
public DurationFormatException(String invalidFormat, List<String> validFormats) {
|
||||||
super("Duration format exception ");
|
super("Duration format exception ");
|
||||||
this.model = DurationFormatExceptionModel
|
this.model = DurationFormatExceptionModel
|
||||||
.builder()
|
.builder()
|
||||||
.invalidFormat(wrongFormat)
|
.invalidFormat(invalidFormat)
|
||||||
.validFormats(validFormats)
|
.validFormats(validFormats)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package dev.sheldan.abstracto.core.exception;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.models.exception.InstantFormatExceptionModel;
|
||||||
|
import dev.sheldan.abstracto.core.templating.Templatable;
|
||||||
|
|
||||||
|
public class InstantFormatException extends AbstractoRunTimeException implements Templatable {
|
||||||
|
|
||||||
|
private final InstantFormatExceptionModel model;
|
||||||
|
|
||||||
|
|
||||||
|
public InstantFormatException(String invalidFormat) {
|
||||||
|
super("Instant format exception ");
|
||||||
|
this.model = InstantFormatExceptionModel
|
||||||
|
.builder()
|
||||||
|
.invalidFormat(invalidFormat)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTemplateName() {
|
||||||
|
return "instant_invalid_time_format_exception";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getTemplateModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package dev.sheldan.abstracto.core.models.exception;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public class InstantFormatExceptionModel implements Serializable {
|
||||||
|
private final String invalidFormat;
|
||||||
|
}
|
||||||
@@ -3,12 +3,18 @@ package dev.sheldan.abstracto.core.utils;
|
|||||||
|
|
||||||
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
|
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
|
||||||
import dev.sheldan.abstracto.core.exception.DurationFormatException;
|
import dev.sheldan.abstracto.core.exception.DurationFormatException;
|
||||||
|
import dev.sheldan.abstracto.core.exception.InstantFormatException;
|
||||||
|
import java.time.Instant;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.ISnowflake;
|
import net.dv8tion.jda.api.entities.ISnowflake;
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
import net.dv8tion.jda.api.entities.Role;
|
import net.dv8tion.jda.api.entities.Role;
|
||||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
|
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
|
||||||
|
import net.dv8tion.jda.api.utils.TimeFormat;
|
||||||
|
import net.dv8tion.jda.api.utils.Timestamp;
|
||||||
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
|
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.math.NumberUtils;
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
@@ -18,40 +24,65 @@ import java.util.List;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class ParseUtils {
|
public class ParseUtils {
|
||||||
|
|
||||||
private ParseUtils() {
|
private ParseUtils() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Pattern messageRegex = Pattern.compile("(?<number>\\d+)(?<unit>[ywdhms]+)");
|
private static final Pattern MESSAGE_REGEX = Pattern.compile("(?<number>\\d+)(?<unit>[ywdhms]+)");
|
||||||
private static List<String> validDuration = Arrays.asList("w", "d", "h", "m", "s");
|
private static final List<String> VALID_DURATION = Arrays.asList("w", "d", "h", "m", "s");
|
||||||
|
|
||||||
public static Duration parseDuration(String textToParseFrom) {
|
public static Duration parseDuration(String textToParseFrom) {
|
||||||
if(textToParseFrom == null || textToParseFrom.isEmpty()) {
|
if(textToParseFrom == null || textToParseFrom.isEmpty()) {
|
||||||
throw new DurationFormatException("", validDuration);
|
throw new DurationFormatException("", VALID_DURATION);
|
||||||
}
|
}
|
||||||
Matcher matcher = ParseUtils.messageRegex.matcher(textToParseFrom);
|
Duration targetDuration;
|
||||||
Duration start = Duration.ZERO;
|
try {
|
||||||
|
Timestamp discordTimeStamp = TimeFormat.parse(textToParseFrom);
|
||||||
|
targetDuration = Duration.between(Instant.now(), discordTimeStamp.toInstant());
|
||||||
|
return targetDuration;
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
Matcher matcher = ParseUtils.MESSAGE_REGEX.matcher(textToParseFrom);
|
||||||
|
targetDuration = Duration.ZERO;
|
||||||
String rest = textToParseFrom;
|
String rest = textToParseFrom;
|
||||||
while(matcher.find()) {
|
while(matcher.find()) {
|
||||||
String unit = matcher.group("unit");
|
String unit = matcher.group("unit");
|
||||||
String number = matcher.group("number");
|
String number = matcher.group("number");
|
||||||
rest = rest.replace(matcher.group(0), "");
|
rest = rest.replace(matcher.group(0), "");
|
||||||
long parsed = Long.parseLong(number);
|
long parsed = Long.parseLong(number);
|
||||||
switch (unit) {
|
targetDuration = switch (unit) {
|
||||||
case "w": start = start.plus(Duration.ofDays(parsed * 7)); break;
|
case "w" -> targetDuration.plus(Duration.ofDays(parsed * 7));
|
||||||
case "d": start = start.plus(Duration.ofDays(parsed)); break;
|
case "d" -> targetDuration.plus(Duration.ofDays(parsed));
|
||||||
case "h": start = start.plus(Duration.ofHours(parsed)); break;
|
case "h" -> targetDuration.plus(Duration.ofHours(parsed));
|
||||||
case "m": start = start.plus(Duration.ofMinutes(parsed)); break;
|
case "m" -> targetDuration.plus(Duration.ofMinutes(parsed));
|
||||||
case "s": start = start.plus(Duration.ofSeconds(parsed)); break;
|
case "s" -> targetDuration.plus(Duration.ofSeconds(parsed));
|
||||||
default: throw new DurationFormatException(unit, validDuration);
|
default -> throw new DurationFormatException(unit, VALID_DURATION);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
if(!rest.equals("")) {
|
if(!rest.equals("")) {
|
||||||
throw new DurationFormatException(rest, validDuration);
|
throw new DurationFormatException(rest, VALID_DURATION);
|
||||||
}
|
}
|
||||||
return start;
|
return targetDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Instant parseInstant(String textToParseFrom) {
|
||||||
|
if(textToParseFrom == null || textToParseFrom.isEmpty()) {
|
||||||
|
throw new DurationFormatException("", VALID_DURATION);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Timestamp discordTimeStamp = TimeFormat.parse(textToParseFrom);
|
||||||
|
return discordTimeStamp.toInstant();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
if(StringUtils.isNumeric(textToParseFrom)) {
|
||||||
|
return Instant.ofEpochSecond(Integer.parseInt(textToParseFrom));
|
||||||
|
}
|
||||||
|
throw new InstantFormatException(textToParseFrom);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Role parseRoleFromText(String text, Guild guild) {
|
public static Role parseRoleFromText(String text, Guild guild) {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.build.timestamp.format>yyyy/MM/dd HH:mm</maven.build.timestamp.format>
|
<maven.build.timestamp.format>yyyy/MM/dd HH:mm</maven.build.timestamp.format>
|
||||||
<jda.version>6.2.0</jda.version>
|
<jda.version>6.3.1</jda.version>
|
||||||
<asciidoctor.maven.plugin.version>2.2.6</asciidoctor.maven.plugin.version>
|
<asciidoctor.maven.plugin.version>2.2.6</asciidoctor.maven.plugin.version>
|
||||||
<asciidoctorj.pdf.version>1.5.3</asciidoctorj.pdf.version>
|
<asciidoctorj.pdf.version>1.5.3</asciidoctorj.pdf.version>
|
||||||
<asciidoctorj.version>2.3.0</asciidoctorj.version>
|
<asciidoctorj.version>2.3.0</asciidoctorj.version>
|
||||||
|
|||||||
Reference in New Issue
Block a user