use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class WarnCommand method set.
@Command(value = "set", description = "Set the amount of warns a user has")
@CommandId(242)
@Redirects({ "set warns", "set warnings" })
@Examples({ "warn set @Shea#6653 2", "warn set Shea#6653 0", "warn set 402557516728369153 1" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void set(Sx4CommandEvent event, @Argument(value = "user") Member member, @Argument(value = "warnings") int warnings) {
if (member.canInteract(event.getMember())) {
event.replyFailure("You cannot change the amount of warnings of someone higher or equal than your top role").queue();
return;
}
Document warnData = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("warn")).get("warn", MongoDatabase.EMPTY_DOCUMENT);
boolean punishments = warnData.get("punishments", true);
int maxWarning = punishments ? warnData.getList("config", Document.class, Warn.DEFAULT_CONFIG).stream().map(d -> d.getInteger("number")).max(Integer::compareTo).get() : Integer.MAX_VALUE;
if (warnings > maxWarning) {
event.replyFailure("The max amount of warnings you can give is **" + maxWarning + "**").queue();
return;
}
Bson update = warnings == 0 ? Updates.unset("warnings") : Updates.combine(Updates.set("warnings", warnings), Updates.set("lastWarning", Clock.systemUTC().instant().getEpochSecond()));
Bson filter = Filters.and(Filters.eq("userId", member.getIdLong()), Filters.eq("guildId", event.getGuild().getIdLong()));
event.getMongo().updateWarnings(filter, update, new UpdateOptions().upsert(true)).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0 && result.getUpsertedId() == null) {
event.replyFailure("That user already had that amount of warnings").queue();
return;
}
event.replySuccess("That user now has **" + warnings + "** warning" + (warnings == 1 ? "" : "s")).queue();
});
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class AntiInviteCommand method resetAfter.
@Command(value = "reset after", description = "The time it should take for attempts to be taken away")
@CommandId(308)
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
@Examples({ "antiinvite reset after 1 1 day", "antiinvite reset after 3 5h 20s", "antiinvite reset after 3 5h 20s" })
public void resetAfter(Sx4CommandEvent event, @Argument(value = "amount") @Limit(min = 0) int amount, @Argument(value = "time", endless = true, nullDefault = true) Duration time) {
if (time != null && time.toMinutes() < 5) {
event.replyFailure("The duration has to be 5 minutes or above").queue();
return;
}
if (amount != 0 && time == null) {
event.reply("You need to provide a duration if attempts is more than 0").queue();
return;
}
Bson update = amount == 0 ? Updates.unset("attempts.reset") : Updates.set("attempts.reset", new Document("amount", amount).append("after", time.toSeconds()));
Bson filter = Filters.and(Filters.eq("regexId", AntiInviteCommand.REGEX_ID), Filters.eq("guildId", event.getGuild().getIdLong()));
event.getMongo().updateRegex(filter, update).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getMatchedCount() == 0) {
event.replyFailure("You do not have anti-invite setup").queue();
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("Your reset attempts configuration was already set to that").queue();
return;
}
event.reply(amount == 0 ? "Users attempts will no longer reset" + event.getConfig().getSuccessEmote() : String.format("Users attempts will now reset **%d** time%s after `%s` %s", amount, amount == 1 ? "" : "s", TimeUtility.LONG_TIME_FORMATTER.parse(time.toSeconds()), event.getConfig().getSuccessEmote())).queue();
});
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class AntiRegexCommand method add.
@Command(value = "add", description = "Add a custom regex to be checked on every message")
@CommandId(125)
@Examples({ "anti regex add [0-9]+", "anti regex add https://discord\\.com/channels/([0-9]+)/([0-9]+)/?" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void add(Sx4CommandEvent event, @Argument(value = "regex", endless = true) Pattern pattern) {
List<Bson> guildPipeline = List.of(Aggregates.project(Projections.fields(Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))), Projections.computed("guildId", "$_id"))), Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())));
List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("guildId", event.getGuild().getIdLong()), Filters.exists("enabled", false), Filters.eq("type", RegexType.REGEX.getId()))), Aggregates.group(null, Accumulators.sum("count", 1)), Aggregates.limit(10), Aggregates.unionWith("guilds", guildPipeline), Aggregates.group(null, Accumulators.max("count", "$count"), Accumulators.max("premium", "$premium")), Aggregates.project(Projections.fields(Projections.computed("premium", Operators.ifNull("$premium", false)), Projections.computed("count", Operators.ifNull("$count", 0)))));
event.getMongo().aggregateRegexes(pipeline).thenCompose(documents -> {
Document counter = documents.isEmpty() ? null : documents.get(0);
int count = counter == null ? 0 : counter.getInteger("count");
if (count >= 3 && !counter.getBoolean("premium")) {
throw new IllegalArgumentException("You need to have Sx4 premium to have more than 3 enabled anti regexes, you can get premium at <https://www.patreon.com/Sx4>");
}
if (count == 10) {
throw new IllegalArgumentException("You cannot have any more than 10 anti regexes");
}
Document patternData = new Document("guildId", event.getGuild().getIdLong()).append("type", RegexType.REGEX.getId()).append("pattern", pattern.pattern());
return event.getMongo().insertRegex(patternData);
}).whenComplete((result, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof MongoWriteException && ((MongoWriteException) cause).getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
event.replyFailure("You already have that anti regex setup in this server").queue();
return;
} else if (cause instanceof IllegalArgumentException) {
event.replyFailure(cause.getMessage()).queue();
return;
}
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.replySuccess("The regex `" + result.getInsertedId().asObjectId().getValue().toHexString() + "` is now active").queue();
});
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class AntiRegexCommand method resetAfter.
@Command(value = "reset after", description = "The time it should take for attempts to be taken away")
@CommandId(109)
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
@Examples({ "anti regex reset after 5f023782ef9eba03390a740c 1 1 day", "anti regex reset after 5f023782ef9eba03390a740c 3 5h 20s", "anti regex reset after 5f023782ef9eba03390a740c 3 5h 20s" })
public void resetAfter(Sx4CommandEvent event, @Argument(value = "id") ObjectId id, @Argument(value = "amount") @Limit(min = 0) int amount, @Argument(value = "time", endless = true, nullDefault = true) Duration time) {
if (time != null && time.toMinutes() < 5) {
event.replyFailure("The duration has to be 5 minutes or above").queue();
return;
}
if (amount != 0 && time == null) {
event.reply("You need to provide a duration if attempts is more than 0").queue();
return;
}
Bson update = amount == 0 ? Updates.unset("attempts.reset") : Updates.set("attempts.reset", new Document("amount", amount).append("after", time.toSeconds()));
event.getMongo().updateRegex(Filters.and(Filters.eq("_id", id), Filters.eq("guildId", event.getGuild().getIdLong())), update).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getMatchedCount() == 0) {
event.replyFailure("I could not find that anti regex").queue();
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("Your reset attempts configuration was already set to that").queue();
return;
}
event.reply(amount == 0 ? "Users attempts will no longer reset" + event.getConfig().getSuccessEmote() : String.format("Users attempts will now reset **%d** time%s after `%s` %s", amount, amount == 1 ? "" : "s", TimeUtility.LONG_TIME_FORMATTER.parse(time.toSeconds()), event.getConfig().getSuccessEmote())).queue();
});
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class AntiRegexCommand method list.
@Command(value = "list", description = "Lists the regexes which are active in this server")
@CommandId(110)
@Examples({ "anti regex list" })
public void list(Sx4CommandEvent event) {
List<Document> regexes = event.getMongo().getRegexes(Filters.eq("guildId", event.getGuild().getIdLong()), Projections.include("pattern")).into(new ArrayList<>());
if (regexes.isEmpty()) {
event.replyFailure("There are no regexes setup in this server").queue();
return;
}
PagedResult<Document> paged = new PagedResult<>(event.getBot(), regexes).setPerPage(6).setCustomFunction(page -> {
MessageBuilder builder = new MessageBuilder();
EmbedBuilder embed = new EmbedBuilder();
embed.setAuthor("Anti Regex", null, event.getGuild().getIconUrl());
embed.setTitle("Page " + page.getPage() + "/" + page.getMaxPage());
embed.setFooter(PagedResult.DEFAULT_FOOTER_TEXT, null);
page.forEach((data, index) -> embed.addField(data.getObjectId("_id").toHexString(), "`" + data.getString("pattern") + "`", true));
return builder.setEmbeds(embed.build());
});
paged.execute(event);
}
Aggregations