added conventions for templated commands, to reduce the amount of necessary configuration

added an exception, in case the command was missing the correct types of parameters (channel is required, no channel was found)
added fix text to exception handler, in case the message of an exception is null
fixed off-by-one error when searching the missing parameter
changed supported chanel from guildChannel to textChannel for posttarget
added slowmode command
This commit is contained in:
Sheldan
2020-03-18 21:06:27 +01:00
parent 9119d57108
commit ec21305725
21 changed files with 212 additions and 42 deletions

View File

@@ -53,11 +53,11 @@ public class Ban implements Command {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("user").type(Member.class).optional(false).build());
parameters.add(Parameter.builder().name("reason").type(String.class).optional(true).remainder(true).build());
HelpInfo helpInfo = HelpInfo.builder().usageTemplate("ban_usage").longHelpTemplate("ban_long_help").build();
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("ban")
.module(Moderation.MODERATION)
.descriptionTemplate("ban_help_description")
.templated(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -50,11 +50,11 @@ public class Kick implements Command {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("user").type(Member.class).optional(false).build());
parameters.add(Parameter.builder().name("reason").type(String.class).optional(true).remainder(true).build());
HelpInfo helpInfo = HelpInfo.builder().usageTemplate("kick_usage").longHelpTemplate("kick_long_help").build();
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("kick")
.module(Moderation.MODERATION)
.descriptionTemplate("kick_help_description")
.templated(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -0,0 +1,56 @@
package dev.sheldan.abstracto.moderation.command;
import dev.sheldan.abstracto.command.Command;
import dev.sheldan.abstracto.command.HelpInfo;
import dev.sheldan.abstracto.command.execution.CommandConfiguration;
import dev.sheldan.abstracto.command.execution.CommandContext;
import dev.sheldan.abstracto.command.execution.Parameter;
import dev.sheldan.abstracto.command.execution.Result;
import dev.sheldan.abstracto.moderation.Moderation;
import dev.sheldan.abstracto.moderation.service.SlowModeService;
import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
@Component
public class SlowMode implements Command {
@Autowired
private SlowModeService slowModeService;
@Override
public Result execute(CommandContext commandContext) {
TextChannel channel;
long seconds = (Long) commandContext.getParameters().getParameters().get(0);
if(commandContext.getParameters().getParameters().size() == 2) {
channel = (TextChannel) commandContext.getParameters().getParameters().get(1);
if(commandContext.getGuild().getGuildChannelById(channel.getIdLong()) == null) {
throw new IllegalArgumentException("Given channel was not part of the current guild.");
}
} else {
channel = commandContext.getChannel();
}
slowModeService.setSlowMode(channel, Duration.ofSeconds(seconds));
return Result.fromSuccess();
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("seconds").type(Long.class).optional(false).build());
parameters.add(Parameter.builder().name("channel").type(TextChannel.class).optional(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("slowmode")
.module(Moderation.MODERATION)
.templated(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)
.build();
}
}

View File

@@ -56,11 +56,11 @@ public class Warn implements Command {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("user").type(Member.class).optional(false).build());
parameters.add(Parameter.builder().name("reason").type(String.class).optional(true).remainder(true).build());
HelpInfo helpInfo = HelpInfo.builder().usageTemplate("warn_usage").longHelpTemplate("long_help").build();
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("warn")
.module(Moderation.MODERATION)
.descriptionTemplate("warn_help_description")
.templated(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -0,0 +1,45 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.models.AChannel;
import dev.sheldan.abstracto.core.service.Bot;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
@Component
@Slf4j
public class SlowModeServiceBean implements SlowModeService {
@Autowired
private Bot bot;
@Override
public void setSlowMode(TextChannel channel, Duration duration) {
long seconds = duration.getSeconds();
if(seconds > TextChannel.MAX_SLOWMODE) {
throw new IllegalArgumentException("Slow mode duration must be < " + TextChannel.MAX_SLOWMODE + " seconds.");
}
channel.getManager().setSlowmode((int) seconds).queue();
}
@Override
public void setSlowMode(AChannel channel, Duration duration) {
JDA jpaInstance = bot.getInstance();
Guild guild = jpaInstance.getGuildById(channel.getServer().getId());
if(guild != null) {
TextChannel textChannelById = guild.getTextChannelById(channel.getId());
if(textChannelById != null) {
this.setSlowMode(textChannelById, duration);
} else {
log.warn("Channel {} was not found in guild {} when trying to set a slow mode.", channel.getId(), channel.getServer().getId());
}
} else {
log.warn("Guild {} was not found when trying to set slow mode.", channel.getServer().getId());
}
}
}

View File

@@ -0,0 +1 @@
Sets the slow mode of the the current (or given channel) to the given seconds.

View File

@@ -0,0 +1 @@
The slowmode in channel ${channel.name} has been set to ${channel.slowMode} seconds.

View File

@@ -0,0 +1 @@
Sets the slow mode of the the current (or given channel) to the given seconds.

View File

@@ -0,0 +1,11 @@
package dev.sheldan.abstracto.moderation.service;
import dev.sheldan.abstracto.core.models.AChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import java.time.Duration;
public interface SlowModeService {
void setSlowMode(TextChannel channel, Duration duration);
void setSlowMode(AChannel channel, Duration duration);
}