use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class RoleCommand method add.
@Command(value = "add", description = "Add a role to a member")
@CommandId(250)
@Redirects({ "addrole", "add role", "ar" })
@Examples({ "role add @Shea#6653 Role", "role add Shea 345718366373150720", "role add @Role" })
@AuthorPermissions(permissions = { Permission.MANAGE_ROLES })
@BotPermissions(permissions = { Permission.MANAGE_ROLES })
public void add(Sx4CommandEvent event, @Argument(value = "user", nullDefault = true) @AlternativeOptions("all") Alternative<Member> option, @Argument(value = "role", endless = true) Role role) {
if (role.isManaged()) {
event.replyFailure("I cannot give managed roles").queue();
return;
}
if (role.isPublicRole()) {
event.replyFailure("I cannot give the @everyone role").queue();
return;
}
if (!event.getMember().canInteract(role)) {
event.replyFailure("You cannot give a role higher or equal than your top role").queue();
return;
}
if (!event.getSelfMember().canInteract(role)) {
event.replyFailure("I cannot give a role higher or equal than my top role").queue();
return;
}
if (option != null && option.isAlternative()) {
List<Member> members = event.getGuild().getMemberCache().applyStream(stream -> stream.filter(member -> !member.getRoles().contains(role)).collect(Collectors.toList()));
if (members.size() == 0) {
event.replyFailure("All users already have that role").queue();
return;
}
if (!this.pending.add(event.getGuild().getIdLong())) {
event.replyFailure("You can only have 1 concurrent role being added to all users").queue();
return;
}
event.replyFormat("Adding %s to **%,d** user%s, another message will be sent once this is done %s", role.getAsMention(), members.size(), members.size() == 1 ? "" : "s", event.getConfig().getSuccessEmote()).queue();
List<CompletableFuture<Integer>> futures = new ArrayList<>();
for (Member member : members) {
futures.add(event.getGuild().addRoleToMember(member, role).submit().handle((result, exception) -> exception == null ? 1 : 0));
}
FutureUtility.allOf(futures).whenComplete((completed, exception) -> {
this.pending.remove(event.getGuild().getIdLong());
int count = completed.stream().reduce(0, Integer::sum);
event.replyFormat("Successfully added the role %s to **%,d/%,d** user%s %s", role.getAsMention(), count, members.size(), count == 1 ? "" : "s", event.getConfig().getSuccessEmote()).queue();
});
} else {
Member effectiveMember = option == null ? event.getMember() : option.getValue();
event.getGuild().addRoleToMember(effectiveMember, role).flatMap($ -> event.replySuccess(role.getAsMention() + " has been added to **" + effectiveMember.getUser().getAsTag() + "**")).queue();
}
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class InvitesCommand method leaderboard.
@Command(value = "leaderboard", aliases = { "lb" }, description = "View a leaderboard of all invites in the server sorted by user")
@CommandId(266)
@Redirects({ "lb invites", "leaderboard invites" })
@Examples({ "invites leaderboard" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void leaderboard(Sx4CommandEvent event) {
event.getGuild().retrieveInvites().queue(invites -> {
if (invites.isEmpty()) {
event.replyFailure("There are no invites in this server").queue();
return;
}
Map<Long, Integer> count = new HashMap<>();
AtomicInteger total = new AtomicInteger(0);
for (Invite invite : invites) {
User inviter = invite.getInviter();
if (inviter != null) {
count.compute(inviter.getIdLong(), (key, value) -> value == null ? invite.getUses() : value + invite.getUses());
}
total.addAndGet(invite.getUses());
}
List<Map.Entry<Long, Integer>> sortedCount = new ArrayList<>(count.entrySet());
sortedCount.sort(Collections.reverseOrder(Comparator.comparingInt(Map.Entry::getValue)));
PagedResult<Map.Entry<Long, Integer>> paged = new PagedResult<>(event.getBot(), sortedCount).setIncreasedIndex(true).setAuthor("Invites Leaderboard", null, event.getGuild().getIconUrl()).setDisplayFunction(data -> {
int percentInvited = Math.round(((float) data.getValue() / total.get()) * 100);
String percent = percentInvited >= 1 ? String.valueOf(percentInvited) : "<1";
Member member = event.getGuild().getMemberById(data.getKey());
String memberString = member == null ? String.valueOf(data.getKey()) : member.getUser().getAsTag();
return String.format("`%s` - %,d %s (%s%%)", memberString, data.getValue(), data.getValue() == 1 ? "invite" : "invites", percent);
});
paged.execute(event);
});
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class TemporaryBanCommand method defaultTime.
@Command(value = "default time", aliases = { "default duration" }, description = "Sets the default time to be used when a duration argument isn't given")
@CommandId(344)
@Examples({ "temporary ban default time 10m", "temporary ban default time 5d", "temporary ban default time 1h 30m" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void defaultTime(Sx4CommandEvent event, @Argument(value = "duration", endless = true) Duration duration) {
long seconds = duration.toSeconds();
Bson update = seconds == ModUtility.DEFAULT_TEMPORARY_BAN_DURATION ? Updates.unset("temporaryBan.defaultTime") : Updates.set("temporaryBan.defaultTime", seconds);
event.getMongo().updateGuildById(event.getGuild().getIdLong(), update).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0 && result.getUpsertedId() == null) {
event.replyFailure("Your temporary ban default time was already set to that").queue();
return;
}
event.replySuccess("Your temporary ban default time has been set to **" + TimeUtility.LONG_TIME_FORMATTER.parse(seconds) + "**").queue();
});
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class TemporaryBanCommand method list.
@Command(value = "list", description = "Lists all the users who have temporary bans")
@CommandId(345)
@Examples({ "temporary ban list" })
public void list(Sx4CommandEvent event) {
List<Document> bans = event.getMongo().getTemporaryBans(Filters.eq("guildId", event.getGuild().getIdLong()), Projections.include("unbanAt", "userId")).into(new ArrayList<>());
if (bans.isEmpty()) {
event.replyFailure("There is no one with a temporary ban in this server").queue();
return;
}
bans.sort(Comparator.comparingLong(d -> d.getLong("unbanAt")));
PagedResult<Document> paged = new PagedResult<>(event.getBot(), bans).setAuthor("Muted Users", null, event.getGuild().getIconUrl()).setIndexed(false).setSelect().setDisplayFunction(data -> {
User user = event.getShardManager().getUserById(data.getLong("userId"));
return (user == null ? "Anonymous#0000" : user.getAsTag()) + " - " + TimeUtility.LONG_TIME_FORMATTER.parse(data.getLong("unbanAt") - Clock.systemUTC().instant().getEpochSecond());
});
paged.execute(event);
}
use of com.jockie.bot.core.command.Command in project Sx4 by sx4-discord-bot.
the class WarnCommand method view.
@Command(value = "view", description = "View the amount of warnings a specific user is on")
@CommandId(259)
@Examples({ "warn view @Shea#6653", "warn view Shea", "warn view 402557516728369153" })
@Redirects({ "warnings", "warns" })
public void view(Sx4CommandEvent event, @Argument(value = "user", endless = true) Member member) {
List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("userId", member.getIdLong()), Filters.eq("guildId", event.getGuild().getIdLong()))), Aggregates.project(Projections.computed("warnings", Operators.cond(Operators.or(Operators.isNull("$reset"), Operators.isNull("$warnings")), Operators.ifNull("$warnings", 0), Operators.max(0, Operators.subtract("$warnings", Operators.multiply(Operators.toInt(Operators.floor(Operators.divide(Operators.subtract(Operators.nowEpochSecond(), "$lastWarning"), "$reset.after"))), "$reset.amount")))))));
event.getMongo().aggregateWarnings(pipeline).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
Document data = documents.isEmpty() ? MongoDatabase.EMPTY_DOCUMENT : documents.get(0);
int warnings = data.getInteger("warnings", 0);
event.reply("**" + member.getUser().getAsTag() + "** is currently on **" + warnings + "** warning" + (warnings == 1 ? "" : "s")).queue();
});
}
Aggregations