[AB-339] repurposing the softban command for a ban command with deletion days

This commit is contained in:
Sheldan
2021-10-25 01:11:32 +02:00
parent 0514d355c7
commit 362d87778e
11 changed files with 47 additions and 22 deletions

View File

@@ -42,7 +42,7 @@ public class Ban extends AbstractConditionableCommand {
User user = (User) parameters.get(0);
String reason = (String) parameters.get(1);
return banService.banUser(user, reason, commandContext.getAuthor(), commandContext.getMessage())
return banService.banUserWithNotification(user, reason, commandContext.getAuthor(), 0, commandContext.getMessage())
.thenApply(aVoid -> CommandResult.fromSuccess());
}

View File

@@ -17,7 +17,6 @@ import net.dv8tion.jda.api.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -27,7 +26,7 @@ import static dev.sheldan.abstracto.moderation.service.BanService.BAN_EFFECT_KEY
@Component
@Slf4j
public class SoftBan extends AbstractConditionableCommand {
public class BanDelete extends AbstractConditionableCommand {
@Autowired
private BanService banService;
@@ -36,11 +35,9 @@ public class SoftBan extends AbstractConditionableCommand {
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
User user = (User) parameters.get(0);
Duration delDays = Duration.ofDays(7);
if(parameters.size() > 1) {
delDays = (Duration) parameters.get(1);
}
return banService.softBanUser(commandContext.getGuild(), user, delDays)
Integer delDays = (Integer) parameters.get(1);
String reason = (String) parameters.get(2);
return banService.banUserWithNotification(user, reason, commandContext.getAuthor(), delDays, commandContext.getMessage())
.thenApply(unused -> CommandResult.fromSuccess());
}
@@ -48,11 +45,12 @@ public class SoftBan extends AbstractConditionableCommand {
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("user").templated(true).type(User.class).build());
parameters.add(Parameter.builder().name("delDays").templated(true).type(Duration.class).optional(true).build());
parameters.add(Parameter.builder().name("delDays").templated(true).type(Integer.class).build());
parameters.add(Parameter.builder().name("reason").templated(true).type(String.class).remainder(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
List<EffectConfig> effectConfig = Arrays.asList(EffectConfig.builder().position(0).effectKey(BAN_EFFECT_KEY).build());
return CommandConfiguration.builder()
.name("softBan")
.name("banDelete")
.module(ModerationModuleDefinition.MODERATION)
.templated(true)
.async(true)

View File

@@ -34,7 +34,7 @@ public class UnBan extends AbstractConditionableCommand {
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
User user = (User) parameters.get(0);
return banService.unBanUser(user, commandContext.getAuthor())
return banService.unBanUserWithNotification(user, commandContext.getAuthor())
.thenApply(aVoid -> CommandResult.fromSuccess());
}

View File

@@ -50,7 +50,7 @@ public class BanServiceBean implements BanService {
private ChannelService channelService;
@Override
public CompletableFuture<Void> banMember(Member member, String reason, Member banningMember, Message message) {
public CompletableFuture<Void> banMemberWithNotification(Member member, String reason, Member banningMember, Integer deletionDays, Message message) {
BanLog banLog = BanLog
.builder()
.bannedUser(member.getUser())
@@ -58,18 +58,19 @@ public class BanServiceBean implements BanService {
.commandMessage(message)
.reason(reason)
.build();
CompletableFuture<Void> banFuture = banUser(member.getGuild(), member.getUser(), 0, reason);
CompletableFuture<Void> banFuture = banUser(member.getGuild(), member.getUser(), deletionDays, reason);
CompletableFuture<Void> messageFuture = sendBanLogMessage(banLog, member.getGuild().getIdLong(), BAN_LOG_TEMPLATE);
return CompletableFuture.allOf(banFuture, messageFuture);
}
@Override
public CompletableFuture<Void> banUser(User user, String reason, Member banningMember, Message message) {
public CompletableFuture<Void> banUserWithNotification(User user, String reason, Member banningMember, Integer deletionDays, Message message) {
BanLog banLog = BanLog
.builder()
.bannedUser(user)
.banningMember(banningMember)
.commandMessage(message)
.deletionDays(deletionDays)
.reason(reason)
.build();
Guild guild = banningMember.getGuild();
@@ -81,7 +82,7 @@ public class BanServiceBean implements BanService {
.thenAccept(message1 -> log.info("Notified about not being able to send ban notification in server {} and channel {} based on message {} from user {}."
, message.getGuild().getIdLong(), message.getChannel().getIdLong(), message.getIdLong(), message.getAuthor().getIdLong()));
}
CompletableFuture<Void> banFuture = banUser(guild, user, 0, reason);
CompletableFuture<Void> banFuture = banUser(guild, user, deletionDays, reason);
CompletableFuture<Void> messageFuture = sendBanLogMessage(banLog, guild.getIdLong(), BAN_LOG_TEMPLATE);
CompletableFuture.allOf(banFuture, messageFuture)
.thenAccept(unused1 -> returningFuture.complete(null))
@@ -107,7 +108,7 @@ public class BanServiceBean implements BanService {
}
@Override
public CompletableFuture<Void> unBanUser(User user, Member unBanningMember) {
public CompletableFuture<Void> unBanUserWithNotification(User user, Member unBanningMember) {
Guild guild = unBanningMember.getGuild();
UnBanLog banLog = UnBanLog
.builder()

View File

@@ -8,4 +8,5 @@
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="seedData/data.xml" relativeToChangelogFile="true"/>
<include file="tables/tables.xml" relativeToChangelogFile="true"/>
<include file="update/update.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -0,0 +1,15 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<changeSet author="Sheldan" id="command-rename_soft_ban">
<update tableName="command">
<column name="name" value="banDelete"/>
<where>name='softBan'</where>
</update>
</changeSet>
</databaseChangeLog>

View File

@@ -0,0 +1,10 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
<include file="command.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@@ -5,7 +5,6 @@ import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.moderation.service.BanService;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
@@ -41,7 +40,7 @@ public class BanTest {
public void testBanWithReason() {
String customReason = "reason2";
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(bannedMember, customReason));
when(banService.banUser(eq(bannedMember), eq(customReason), banLogModelCaptor.capture(), any(Message.class))).thenReturn(CompletableFuture.completedFuture(null));
when(banService.banUserWithNotification(eq(bannedMember), eq(customReason), banLogModelCaptor.capture(), eq(0), any(Message.class))).thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<CommandResult> result = testUnit.executeAsync(parameters);
Member banningMember = banLogModelCaptor.getValue();
Assert.assertEquals(parameters.getAuthor(), banningMember);

View File

@@ -52,7 +52,7 @@ public class BanServiceBeanTest {
when(mockedGuild.ban(user, 0, REASON)).thenReturn(mockedAction);
MessageToSend mockedMessage = Mockito.mock(MessageToSend.class);
when(templateService.renderEmbedTemplate(eq(BanServiceBean.BAN_LOG_TEMPLATE), any(), eq(SERVER_ID))).thenReturn(mockedMessage);
testUnit.banMember(memberToBan, REASON, banningMember, message);
testUnit.banMemberWithNotification(memberToBan, REASON, banningMember, 0, message);
verify(mockedGuild, times(1)).ban(user, 0, REASON);
verify(postTargetService, times(1)).sendEmbedInPostTarget(mockedMessage, ModerationPostTarget.BAN_LOG, SERVER_ID);
}