mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-14 03:45:57 +00:00
changed parameter for slowmode command to be a duration string instead of just seconds
added exception to duration parsing, in case there is a unknown duration indicator
This commit is contained in:
@@ -6,6 +6,7 @@ import dev.sheldan.abstracto.core.command.config.HelpInfo;
|
|||||||
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
import dev.sheldan.abstracto.core.command.execution.CommandContext;
|
||||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||||
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||||
|
import dev.sheldan.abstracto.core.utils.ParseUtils;
|
||||||
import dev.sheldan.abstracto.moderation.Moderation;
|
import dev.sheldan.abstracto.moderation.Moderation;
|
||||||
import dev.sheldan.abstracto.moderation.config.ModerationFeatures;
|
import dev.sheldan.abstracto.moderation.config.ModerationFeatures;
|
||||||
import dev.sheldan.abstracto.moderation.service.SlowModeService;
|
import dev.sheldan.abstracto.moderation.service.SlowModeService;
|
||||||
@@ -26,20 +27,26 @@ public class SlowMode extends AbstractConditionableCommand {
|
|||||||
@Override
|
@Override
|
||||||
public CommandResult execute(CommandContext commandContext) {
|
public CommandResult execute(CommandContext commandContext) {
|
||||||
TextChannel channel;
|
TextChannel channel;
|
||||||
long seconds = (Long) commandContext.getParameters().getParameters().get(0);
|
String durationString = (String) commandContext.getParameters().getParameters().get(0);
|
||||||
|
Duration duration;
|
||||||
|
if(durationString.equalsIgnoreCase("off")) {
|
||||||
|
duration = Duration.ZERO;
|
||||||
|
} else {
|
||||||
|
duration = ParseUtils.parseDuration(durationString);
|
||||||
|
}
|
||||||
if(commandContext.getParameters().getParameters().size() == 2) {
|
if(commandContext.getParameters().getParameters().size() == 2) {
|
||||||
channel = (TextChannel) commandContext.getParameters().getParameters().get(1);
|
channel = (TextChannel) commandContext.getParameters().getParameters().get(1);
|
||||||
} else {
|
} else {
|
||||||
channel = commandContext.getChannel();
|
channel = commandContext.getChannel();
|
||||||
}
|
}
|
||||||
slowModeService.setSlowMode(channel, Duration.ofSeconds(seconds));
|
slowModeService.setSlowMode(channel, duration);
|
||||||
return CommandResult.fromSuccess();
|
return CommandResult.fromSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommandConfiguration getConfiguration() {
|
public CommandConfiguration getConfiguration() {
|
||||||
List<Parameter> parameters = new ArrayList<>();
|
List<Parameter> parameters = new ArrayList<>();
|
||||||
parameters.add(Parameter.builder().name("seconds").type(Long.class).optional(false).build());
|
parameters.add(Parameter.builder().name("duration").type(String.class).optional(false).build());
|
||||||
parameters.add(Parameter.builder().name("channel").type(TextChannel.class).optional(true).build());
|
parameters.add(Parameter.builder().name("channel").type(TextChannel.class).optional(true).build());
|
||||||
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
||||||
return CommandConfiguration.builder()
|
return CommandConfiguration.builder()
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ public class SlowModeServiceBean implements SlowModeService {
|
|||||||
channel.getManager().setSlowmode((int) seconds).queue();
|
channel.getManager().setSlowmode((int) seconds).queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void disableSlowMOde(TextChannel channel) {
|
||||||
|
setSlowMode(channel, Duration.ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setSlowMode(AChannel channel, Duration duration) {
|
public void setSlowMode(AChannel channel, Duration duration) {
|
||||||
Optional<TextChannel> textChannelOptional = botService.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
|
Optional<TextChannel> textChannelOptional = botService.getTextChannelFromServer(channel.getServer().getId(), channel.getId());
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
Sets the slow mode of the the current (or given channel) to the given seconds.
|
Sets the slow mode of the the current (or given channel) to the given duration.
|
||||||
@@ -1 +1 @@
|
|||||||
Sets the slow mode of the the current (or given channel) to the given seconds.
|
Sets the slow mode of the the current (or given channel) to the given duration.
|
||||||
@@ -7,5 +7,6 @@ import java.time.Duration;
|
|||||||
|
|
||||||
public interface SlowModeService {
|
public interface SlowModeService {
|
||||||
void setSlowMode(TextChannel channel, Duration duration);
|
void setSlowMode(TextChannel channel, Duration duration);
|
||||||
|
void disableSlowMOde(TextChannel channel);
|
||||||
void setSlowMode(AChannel channel, Duration duration);
|
void setSlowMode(AChannel channel, Duration duration);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package dev.sheldan.abstracto.core.utils;
|
package dev.sheldan.abstracto.core.utils;
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@@ -11,9 +13,11 @@ public class ParseUtils {
|
|||||||
public static Duration parseDuration(String textToParseFrom) {
|
public static Duration parseDuration(String textToParseFrom) {
|
||||||
Matcher matcher = ParseUtils.messageRegex.matcher(textToParseFrom);
|
Matcher matcher = ParseUtils.messageRegex.matcher(textToParseFrom);
|
||||||
Duration start = Duration.ZERO;
|
Duration start = Duration.ZERO;
|
||||||
|
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), "");
|
||||||
long parsed = Long.parseLong(number);
|
long parsed = Long.parseLong(number);
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case "w": start = start.plus(Duration.ofDays(parsed * 7)); break;
|
case "w": start = start.plus(Duration.ofDays(parsed * 7)); break;
|
||||||
@@ -21,8 +25,12 @@ public class ParseUtils {
|
|||||||
case "h": start = start.plus(Duration.ofHours(parsed)); break;
|
case "h": start = start.plus(Duration.ofHours(parsed)); break;
|
||||||
case "m": start = start.plus(Duration.ofMinutes(parsed)); break;
|
case "m": start = start.plus(Duration.ofMinutes(parsed)); break;
|
||||||
case "s": start = start.plus(Duration.ofSeconds(parsed)); break;
|
case "s": start = start.plus(Duration.ofSeconds(parsed)); break;
|
||||||
|
default: throw new AbstractoRunTimeException(String.format("Invalid time format %s", unit));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(!rest.equals("")) {
|
||||||
|
throw new AbstractoRunTimeException(String.format("Invalid time format found: %s", rest));
|
||||||
|
}
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.sheldan.abstracto.core.utils;
|
package dev.sheldan.abstracto.core.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
@@ -41,4 +42,9 @@ public class ParseUtilsTest {
|
|||||||
Duration duration = ParseUtils.parseDuration("70s");
|
Duration duration = ParseUtils.parseDuration("70s");
|
||||||
assertEquals(Duration.ofMinutes(1).plus(Duration.ofSeconds(10)), duration);
|
assertEquals(Duration.ofMinutes(1).plus(Duration.ofSeconds(10)), duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = AbstractoRunTimeException.class)
|
||||||
|
public void invalidTimeFormat() {
|
||||||
|
ParseUtils.parseDuration("1k");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user