use of com.sx4.bot.events.mod.TemporaryBanEvent in project Sx4 by sx4-discord-bot.
the class TemporaryBanCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user") String userArgument, @Argument(value = "time", nullDefault = true) Duration time, @Argument(value = "reason", endless = true, nullDefault = true) Reason reason, @Option(value = "days", description = "Set how many days of messages should be deleted from the user") @DefaultNumber(1) @Limit(min = 0, max = 7) int days) {
SearchUtility.getUser(event.getShardManager(), userArgument).thenAccept(user -> {
if (user == null) {
event.replyFailure("I could not find that user").queue();
return;
}
if (user.getIdLong() == event.getSelfUser().getIdLong()) {
event.replyFailure("You cannot ban me, that is illegal").queue();
return;
}
Guild guild = event.getGuild();
Member member = guild.getMember(user);
if (member != null) {
if (!event.getMember().canInteract(member)) {
event.replyFailure("You cannot ban someone higher or equal than your top role").queue();
return;
}
if (!event.getSelfMember().canInteract(member)) {
event.replyFailure("I cannot ban someone higher or equal than my top role").queue();
return;
}
}
event.getGuild().retrieveBan(user).submit().whenComplete((ban, exception) -> {
if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception).getErrorResponse() == ErrorResponse.UNKNOWN_BAN) {
Document data = event.getMongo().getGuildById(guild.getIdLong(), Projections.include("temporaryBan.defaultTime")).get("temporaryBan", MongoDatabase.EMPTY_DOCUMENT);
long duration = time == null ? data.get("defaultTime", ModUtility.DEFAULT_TEMPORARY_BAN_DURATION) : time.toSeconds();
List<Bson> update = List.of(Operators.set("unbanAt", Operators.add(Operators.nowEpochSecond(), duration)));
Bson filter = Filters.and(Filters.eq("userId", event.getMember().getIdLong()), Filters.eq("guildId", event.getGuild().getIdLong()));
event.getMongo().updateTemporaryBan(filter, update, new UpdateOptions().upsert(true)).whenComplete((result, resultException) -> {
if (ExceptionUtility.sendExceptionally(event, resultException)) {
return;
}
event.getGuild().ban(user, days).reason(ModUtility.getAuditReason(reason, event.getAuthor())).queue($ -> {
event.replySuccess("**" + user.getAsTag() + "** has been temporarily banned for " + TimeUtility.LONG_TIME_FORMATTER.parse(duration)).queue();
event.getBot().getModActionManager().onModAction(new TemporaryBanEvent(event.getMember(), user, reason, member != null, duration));
event.getBot().getTemporaryBanManager().putBan(event.getGuild().getIdLong(), user.getIdLong(), duration);
});
});
} else {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.replyFailure("That user is already banned").queue();
}
});
});
}
use of com.sx4.bot.events.mod.TemporaryBanEvent in project Sx4 by sx4-discord-bot.
the class ModUtility method performAction.
public static CompletableFuture<? extends Action> performAction(Sx4 bot, Action action, Member target, Member moderator, Reason reason) {
Guild guild = target.getGuild();
switch(action.getModAction()) {
case WARN:
return ModUtility.warn(bot, target, moderator, reason);
case MUTE:
case MUTE_EXTEND:
if (!guild.getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
return CompletableFuture.failedFuture(new BotPermissionException(Permission.MANAGE_ROLES));
}
return ModUtility.mute(bot, target, moderator, Duration.ofSeconds(((TimeAction) action).getDuration()), action.getModAction().isExtend(), reason);
case KICK:
if (!guild.getSelfMember().hasPermission(Permission.KICK_MEMBERS)) {
return CompletableFuture.failedFuture(new BotPermissionException(Permission.KICK_MEMBERS));
}
if (!guild.getSelfMember().canInteract(target)) {
return CompletableFuture.failedFuture(new BotHierarchyException("kick"));
}
if (!moderator.hasPermission(Permission.KICK_MEMBERS)) {
return CompletableFuture.failedFuture(new AuthorPermissionException(Permission.KICK_MEMBERS));
}
return target.kick(ModUtility.getAuditReason(reason, moderator.getUser())).submit().thenApply($ -> {
bot.getModActionManager().onModAction(new KickEvent(moderator, target.getUser(), reason));
return action;
});
case TEMPORARY_BAN:
if (!guild.getSelfMember().hasPermission(Permission.BAN_MEMBERS)) {
return CompletableFuture.failedFuture(new BotPermissionException(Permission.BAN_MEMBERS));
}
if (!guild.getSelfMember().canInteract(target)) {
return CompletableFuture.failedFuture(new BotHierarchyException("ban"));
}
if (!moderator.hasPermission(Permission.BAN_MEMBERS)) {
return CompletableFuture.failedFuture(new AuthorPermissionException(Permission.BAN_MEMBERS));
}
long temporaryBanDuration = ((TimeAction) action).getDuration();
List<Bson> temporaryBanUpdate = List.of(Operators.set("unbanAt", Operators.add(Operators.nowEpochSecond(), temporaryBanDuration)));
Bson filter = Filters.and(Filters.eq("userId", target.getIdLong()), Filters.eq("guildId", guild.getIdLong()));
return bot.getMongo().updateTemporaryBan(filter, temporaryBanUpdate, new UpdateOptions().upsert(true)).thenCompose(temporaryBanResult -> target.ban(1).reason(ModUtility.getAuditReason(reason, moderator.getUser())).submit()).thenApply($ -> {
bot.getModActionManager().onModAction(new TemporaryBanEvent(moderator, target.getUser(), reason, true, temporaryBanDuration));
bot.getTemporaryBanManager().putBan(guild.getIdLong(), target.getIdLong(), temporaryBanDuration);
return action;
});
case BAN:
if (!guild.getSelfMember().hasPermission(Permission.BAN_MEMBERS)) {
return CompletableFuture.failedFuture(new BotPermissionException(Permission.BAN_MEMBERS));
}
if (!guild.getSelfMember().canInteract(target)) {
return CompletableFuture.failedFuture(new BotHierarchyException("ban"));
}
if (!moderator.hasPermission(Permission.BAN_MEMBERS)) {
return CompletableFuture.failedFuture(new AuthorPermissionException(Permission.BAN_MEMBERS));
}
return target.ban(1).reason(ModUtility.getAuditReason(reason, moderator.getUser())).submit().thenApply($ -> {
bot.getModActionManager().onModAction(new BanEvent(moderator, target.getUser(), reason, true));
return action;
});
default:
return CompletableFuture.completedFuture(null);
}
}
Aggregations