mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-16 04:21:49 +00:00
[AB-339] repurposing the softban command for a ban command with deletion days
This commit is contained in:
@@ -42,7 +42,7 @@ public class Ban extends AbstractConditionableCommand {
|
|||||||
User user = (User) parameters.get(0);
|
User user = (User) parameters.get(0);
|
||||||
String reason = (String) parameters.get(1);
|
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());
|
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import net.dv8tion.jda.api.entities.User;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -27,7 +26,7 @@ import static dev.sheldan.abstracto.moderation.service.BanService.BAN_EFFECT_KEY
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SoftBan extends AbstractConditionableCommand {
|
public class BanDelete extends AbstractConditionableCommand {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BanService banService;
|
private BanService banService;
|
||||||
@@ -36,11 +35,9 @@ public class SoftBan extends AbstractConditionableCommand {
|
|||||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||||
User user = (User) parameters.get(0);
|
User user = (User) parameters.get(0);
|
||||||
Duration delDays = Duration.ofDays(7);
|
Integer delDays = (Integer) parameters.get(1);
|
||||||
if(parameters.size() > 1) {
|
String reason = (String) parameters.get(2);
|
||||||
delDays = (Duration) parameters.get(1);
|
return banService.banUserWithNotification(user, reason, commandContext.getAuthor(), delDays, commandContext.getMessage())
|
||||||
}
|
|
||||||
return banService.softBanUser(commandContext.getGuild(), user, delDays)
|
|
||||||
.thenApply(unused -> CommandResult.fromSuccess());
|
.thenApply(unused -> CommandResult.fromSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,11 +45,12 @@ public class SoftBan extends AbstractConditionableCommand {
|
|||||||
public CommandConfiguration getConfiguration() {
|
public CommandConfiguration getConfiguration() {
|
||||||
List<Parameter> parameters = new ArrayList<>();
|
List<Parameter> parameters = new ArrayList<>();
|
||||||
parameters.add(Parameter.builder().name("user").templated(true).type(User.class).build());
|
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();
|
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
|
||||||
List<EffectConfig> effectConfig = Arrays.asList(EffectConfig.builder().position(0).effectKey(BAN_EFFECT_KEY).build());
|
List<EffectConfig> effectConfig = Arrays.asList(EffectConfig.builder().position(0).effectKey(BAN_EFFECT_KEY).build());
|
||||||
return CommandConfiguration.builder()
|
return CommandConfiguration.builder()
|
||||||
.name("softBan")
|
.name("banDelete")
|
||||||
.module(ModerationModuleDefinition.MODERATION)
|
.module(ModerationModuleDefinition.MODERATION)
|
||||||
.templated(true)
|
.templated(true)
|
||||||
.async(true)
|
.async(true)
|
||||||
@@ -34,7 +34,7 @@ public class UnBan extends AbstractConditionableCommand {
|
|||||||
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
|
||||||
List<Object> parameters = commandContext.getParameters().getParameters();
|
List<Object> parameters = commandContext.getParameters().getParameters();
|
||||||
User user = (User) parameters.get(0);
|
User user = (User) parameters.get(0);
|
||||||
return banService.unBanUser(user, commandContext.getAuthor())
|
return banService.unBanUserWithNotification(user, commandContext.getAuthor())
|
||||||
.thenApply(aVoid -> CommandResult.fromSuccess());
|
.thenApply(aVoid -> CommandResult.fromSuccess());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ public class BanServiceBean implements BanService {
|
|||||||
private ChannelService channelService;
|
private ChannelService channelService;
|
||||||
|
|
||||||
@Override
|
@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
|
BanLog banLog = BanLog
|
||||||
.builder()
|
.builder()
|
||||||
.bannedUser(member.getUser())
|
.bannedUser(member.getUser())
|
||||||
@@ -58,18 +58,19 @@ public class BanServiceBean implements BanService {
|
|||||||
.commandMessage(message)
|
.commandMessage(message)
|
||||||
.reason(reason)
|
.reason(reason)
|
||||||
.build();
|
.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);
|
CompletableFuture<Void> messageFuture = sendBanLogMessage(banLog, member.getGuild().getIdLong(), BAN_LOG_TEMPLATE);
|
||||||
return CompletableFuture.allOf(banFuture, messageFuture);
|
return CompletableFuture.allOf(banFuture, messageFuture);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
BanLog banLog = BanLog
|
||||||
.builder()
|
.builder()
|
||||||
.bannedUser(user)
|
.bannedUser(user)
|
||||||
.banningMember(banningMember)
|
.banningMember(banningMember)
|
||||||
.commandMessage(message)
|
.commandMessage(message)
|
||||||
|
.deletionDays(deletionDays)
|
||||||
.reason(reason)
|
.reason(reason)
|
||||||
.build();
|
.build();
|
||||||
Guild guild = banningMember.getGuild();
|
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 {}."
|
.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()));
|
, 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<Void> messageFuture = sendBanLogMessage(banLog, guild.getIdLong(), BAN_LOG_TEMPLATE);
|
||||||
CompletableFuture.allOf(banFuture, messageFuture)
|
CompletableFuture.allOf(banFuture, messageFuture)
|
||||||
.thenAccept(unused1 -> returningFuture.complete(null))
|
.thenAccept(unused1 -> returningFuture.complete(null))
|
||||||
@@ -107,7 +108,7 @@ public class BanServiceBean implements BanService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Void> unBanUser(User user, Member unBanningMember) {
|
public CompletableFuture<Void> unBanUserWithNotification(User user, Member unBanningMember) {
|
||||||
Guild guild = unBanningMember.getGuild();
|
Guild guild = unBanningMember.getGuild();
|
||||||
UnBanLog banLog = UnBanLog
|
UnBanLog banLog = UnBanLog
|
||||||
.builder()
|
.builder()
|
||||||
|
|||||||
@@ -8,4 +8,5 @@
|
|||||||
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
|
http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
|
||||||
<include file="seedData/data.xml" relativeToChangelogFile="true"/>
|
<include file="seedData/data.xml" relativeToChangelogFile="true"/>
|
||||||
<include file="tables/tables.xml" relativeToChangelogFile="true"/>
|
<include file="tables/tables.xml" relativeToChangelogFile="true"/>
|
||||||
|
<include file="update/update.xml" relativeToChangelogFile="true"/>
|
||||||
</databaseChangeLog>
|
</databaseChangeLog>
|
||||||
@@ -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>
|
||||||
@@ -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>
|
||||||
@@ -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.CommandConfigValidator;
|
||||||
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
|
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
|
||||||
import dev.sheldan.abstracto.moderation.service.BanService;
|
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.Member;
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
import net.dv8tion.jda.api.entities.User;
|
import net.dv8tion.jda.api.entities.User;
|
||||||
@@ -41,7 +40,7 @@ public class BanTest {
|
|||||||
public void testBanWithReason() {
|
public void testBanWithReason() {
|
||||||
String customReason = "reason2";
|
String customReason = "reason2";
|
||||||
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(bannedMember, customReason));
|
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);
|
CompletableFuture<CommandResult> result = testUnit.executeAsync(parameters);
|
||||||
Member banningMember = banLogModelCaptor.getValue();
|
Member banningMember = banLogModelCaptor.getValue();
|
||||||
Assert.assertEquals(parameters.getAuthor(), banningMember);
|
Assert.assertEquals(parameters.getAuthor(), banningMember);
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public class BanServiceBeanTest {
|
|||||||
when(mockedGuild.ban(user, 0, REASON)).thenReturn(mockedAction);
|
when(mockedGuild.ban(user, 0, REASON)).thenReturn(mockedAction);
|
||||||
MessageToSend mockedMessage = Mockito.mock(MessageToSend.class);
|
MessageToSend mockedMessage = Mockito.mock(MessageToSend.class);
|
||||||
when(templateService.renderEmbedTemplate(eq(BanServiceBean.BAN_LOG_TEMPLATE), any(), eq(SERVER_ID))).thenReturn(mockedMessage);
|
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(mockedGuild, times(1)).ban(user, 0, REASON);
|
||||||
verify(postTargetService, times(1)).sendEmbedInPostTarget(mockedMessage, ModerationPostTarget.BAN_LOG, SERVER_ID);
|
verify(postTargetService, times(1)).sendEmbedInPostTarget(mockedMessage, ModerationPostTarget.BAN_LOG, SERVER_ID);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ public class BanLog {
|
|||||||
*/
|
*/
|
||||||
private User bannedUser;
|
private User bannedUser;
|
||||||
private Message commandMessage;
|
private Message commandMessage;
|
||||||
|
private Integer deletionDays;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
|
|
||||||
public interface BanService {
|
public interface BanService {
|
||||||
String BAN_EFFECT_KEY = "ban";
|
String BAN_EFFECT_KEY = "ban";
|
||||||
CompletableFuture<Void> banMember(Member member, String reason, Member banningMember, Message message);
|
CompletableFuture<Void> banMemberWithNotification(Member member, String reason, Member banningMember, Integer deletionDays, Message message);
|
||||||
CompletableFuture<Void> banUser(User user, String reason, Member banningMember, Message message);
|
CompletableFuture<Void> banUserWithNotification(User user, String reason, Member banningMember, Integer deletionDays, Message message);
|
||||||
CompletableFuture<Void> unBanUser(User user, Member unBanningUser);
|
CompletableFuture<Void> unBanUserWithNotification(User user, Member unBanningUser);
|
||||||
CompletableFuture<Void> banUser(Guild guild, User user, Integer deletionDays, String reason);
|
CompletableFuture<Void> banUser(Guild guild, User user, Integer deletionDays, String reason);
|
||||||
CompletableFuture<Void> unbanUser(Guild guild, User user);
|
CompletableFuture<Void> unbanUser(Guild guild, User user);
|
||||||
CompletableFuture<Void> softBanUser(Guild guild, User user, Duration delDays);
|
CompletableFuture<Void> softBanUser(Guild guild, User user, Duration delDays);
|
||||||
|
|||||||
Reference in New Issue
Block a user