use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.
the class FakePermissionsCommand method delete.
@Command(value = "delete", aliases = { "remove" }, description = "Deletes fake permissions for a user or role")
@CommandId(172)
@Examples({ "fake permissions delete @Shea#6653", "fake permissions delete @Mods", "fake permissions delete all" })
@AuthorPermissions(permissions = { Permission.ADMINISTRATOR })
public void delete(Sx4CommandEvent event, @Argument(value = "user | role | all", endless = true, nullDefault = true) @AlternativeOptions("all") Alternative<IPermissionHolder> option) {
if (option == null) {
List<Document> fakePermissions = event.getProperty("fakePermissions");
if (fakePermissions.isEmpty()) {
event.replyFailure("Nothing has fake permissions in this server").queue();
return;
}
PagedResult<Document> paged = new PagedResult<>(event.getBot(), fakePermissions).setAuthor("User & Roles", null, event.getGuild().getIconUrl()).setTimeout(60).setDisplayFunction(data -> {
int type = data.getInteger("type");
long id = data.getLong("id");
if (type == HolderType.USER.getType()) {
User user = event.getShardManager().getUserById(id);
return user == null ? "Anonymous#0000 (" + id + ")" : user.getAsTag();
} else {
Role role = event.getGuild().getRoleById(id);
return role == null ? "Deleted Role (" + id + ")" : role.getAsMention();
}
});
paged.onTimeout(() -> event.reply("Timed out :stopwatch:").queue());
paged.onSelect(select -> {
Document data = select.getSelected();
boolean isRole = data.getInteger("type") == HolderType.ROLE.getType();
long id = data.getLong("id");
User user = isRole ? null : event.getShardManager().getUserById(id);
Role role = isRole ? event.getGuild().getRoleById(id) : null;
event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.pull("fakePermissions.holders", Filters.eq("id", id))).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("That " + (isRole ? "role" : "user") + " doesn't have any fake permissions").queue();
return;
}
event.replySuccess((isRole ? (role == null ? "Deleted Role (" + id + ")" : role.getAsMention()) : "**" + (user == null ? "Anonymous#0000** (" + id + ")" : user.getAsTag() + "**")) + " no longer has any fake permissions").queue();
});
});
paged.execute(event);
} else if (option.isAlternative()) {
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply(event.getAuthor().getName() + ", are you sure you want to delete **all** fake permissions data?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.unset("fakePermissions.holders")).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException)) {
return;
}
e.reply("All fake permission data has been deleted in this server " + event.getConfig().getSuccessEmote()).queue();
});
});
} else {
IPermissionHolder holder = option.getValue();
boolean role = holder instanceof Role;
event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.pull("fakePermissions.holders", Filters.eq("id", holder.getIdLong()))).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("That " + (role ? "role" : "user") + " doesn't have any fake permissions").queue();
return;
}
event.replySuccess((role ? ((Role) holder).getAsMention() : "**" + ((Member) holder).getUser().getAsTag() + "**") + " no longer has any fake permissions").queue();
});
}
}
use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.
the class FakePermissionsCommand method add.
@Command(value = "add", description = "Adds permissions to a user or role within the bot")
@CommandId(170)
@Examples({ "fake permissions add @Shea#6653 message_manage", "fake permissions add @Mods kick_members ban_members" })
@AuthorPermissions(permissions = { Permission.ADMINISTRATOR })
public void add(Sx4CommandEvent event, @Argument(value = "user | role") IPermissionHolder holder, @Argument(value = "permissions") Permission... permissions) {
long rawPermissions = Permission.getRaw(permissions);
boolean role = holder instanceof Role;
Document data = new Document("id", holder.getIdLong()).append("type", role ? HolderType.ROLE.getType() : HolderType.USER.getType()).append("permissions", rawPermissions);
Bson filter = Operators.filter("$fakePermissions.holders", Operators.eq("$$this.id", holder.getIdLong()));
List<Bson> update = List.of(Operators.set("fakePermissions.holders", Operators.cond(Operators.extinct("$fakePermissions.holders"), List.of(data), Operators.cond(Operators.isEmpty(filter), Operators.concatArrays("$fakePermissions.holders", List.of(data)), Operators.concatArrays(Operators.filter("$fakePermissions.holders", Operators.ne("$$this.id", holder.getIdLong())), List.of(new Document(data).append("permissions", Operators.toLong(Operators.bitwiseOr(Operators.first(Operators.map(filter, "$$this.permissions")), rawPermissions)))))))));
event.getMongo().updateGuildById(event.getGuild().getIdLong(), update).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("That " + (role ? "role" : "user") + " already has all those permissions").queue();
return;
}
event.replySuccess((role ? ((Role) holder).getAsMention() : "**" + ((Member) holder).getUser().getAsTag() + "**") + " now has those permissions").queue();
});
}
use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.
the class FakePermissionsCommand method remove.
@Command(value = "remove", description = "Removes permissions from a user or role within the bot")
@CommandId(171)
@Examples({ "fake permissions remove @Shea#6653 message_manage", "fake permissions remove @Mods kick_members ban_members", "fake permissions remove @Mods all" })
@AuthorPermissions(permissions = { Permission.ADMINISTRATOR })
public void remove(Sx4CommandEvent event, @Argument(value = "user | role") IPermissionHolder holder, @Argument(value = "permissions") Permission... permissions) {
long rawPermissions = Permission.getRaw(permissions);
boolean role = holder instanceof Role;
Document data = new Document("id", holder.getIdLong()).append("type", role ? 1 : 0);
Bson filter = Operators.filter("$fakePermissions.holders", Operators.eq("$$this.id", holder.getIdLong()));
Bson withoutHolder = Operators.filter("$fakePermissions.holders", Operators.ne("$$this.id", holder.getIdLong()));
Bson newPermissions = Operators.toLong(Operators.bitwiseAnd(Operators.first(Operators.map(filter, "$$this.permissions")), ~rawPermissions));
List<Bson> update = List.of(Operators.set("fakePermissions.holders", Operators.cond(Operators.or(Operators.extinct("$fakePermissions.holders"), Operators.isEmpty(filter)), "$fakePermissions.holders", Operators.cond(Operators.eq(newPermissions, 0L), withoutHolder, Operators.concatArrays(withoutHolder, List.of(data.append("permissions", newPermissions)))))));
event.getMongo().updateGuildById(event.getGuild().getIdLong(), update).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("That " + (role ? "role" : "user") + " doesn't have any of those permissions").queue();
return;
}
event.replySuccess((role ? ((Role) holder).getAsMention() : "**" + ((Member) holder).getUser().getAsTag() + "**") + " no longer has those permissions").queue();
});
}
use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.
the class FakePermissionsCommand method stats.
@Command(value = "stats", description = "Lists the permissions a role or user has")
@CommandId(173)
@Examples({ "fake permissions stats @Shea#6653", "fake permissions stats @Mods" })
public void stats(Sx4CommandEvent event, @Argument(value = "user | role", endless = true) IPermissionHolder holder) {
boolean role = holder instanceof Role;
List<Document> holders = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("fakePermissions.holders")).getEmbedded(List.of("fakePermissions", "holders"), Collections.emptyList());
long permissionsRaw = holders.stream().filter(data -> data.getLong("id") == holder.getIdLong()).map(data -> data.getLong("permissions")).findFirst().orElse(0L);
if (permissionsRaw == 0L) {
event.replyFailure("That " + (role ? "role" : "user") + " doesn't have any fake permissions").queue();
return;
}
EmbedBuilder embed = new EmbedBuilder().setDescription(Permission.getPermissions(permissionsRaw).stream().map(Permission::getName).collect(Collectors.joining("\n"))).setAuthor((role ? ((Role) holder).getName() : ((Member) holder).getEffectiveName()) + "'s Fake Permissions", null, role ? event.getGuild().getIconUrl() : ((Member) holder).getUser().getEffectiveAvatarUrl());
event.reply(embed.build()).queue();
}
use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.
the class LoggerHandler method onPermissionOverrideDelete.
public void onPermissionOverrideDelete(PermissionOverrideDeleteEvent event) {
Guild guild = event.getGuild();
GuildChannel channel = event.getChannel();
ChannelType channelType = event.getChannelType();
Member member = event.getMember();
Role role = event.getRole();
boolean roleOverride = event.isRoleOverride();
LoggerEvent loggerEvent = channelType == ChannelType.CATEGORY ? LoggerEvent.CATEGORY_OVERRIDE_DELETE : channelType == ChannelType.STORE ? LoggerEvent.STORE_CHANNEL_OVERRIDE_DELETE : channelType == ChannelType.VOICE ? LoggerEvent.VOICE_CHANNEL_OVERRIDE_DELETE : LoggerEvent.TEXT_CHANNEL_OVERRIDE_DELETE;
LoggerContext loggerContext = new LoggerContext().setUser(roleOverride ? 0L : member.getIdLong()).setRole(roleOverride ? role.getIdLong() : 0L).setChannel(channel);
WebhookEmbedBuilder embed = new WebhookEmbedBuilder();
embed.setColor(this.bot.getConfig().getRed());
embed.setTimestamp(Instant.now());
embed.setAuthor(new EmbedAuthor(guild.getName(), guild.getIconUrl(), null));
embed.setFooter(new EmbedFooter(String.format("%s ID: %s", roleOverride ? "Role" : "User", event.getPermissionHolder().getIdLong()), null));
// wait for member leave or role delete event if needed
this.delay(() -> {
this.bot.getMongo().aggregateLoggers(this.getPipeline(guild.getIdLong())).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
return;
}
if (documents.isEmpty()) {
return;
}
Document data = documents.get(0);
List<Document> loggers = LoggerUtility.getValidLoggers(data.getList("loggers", Document.class), loggerEvent, loggerContext);
if (loggers.isEmpty()) {
return;
}
boolean deleted = (roleOverride ? event.getRole() : event.getMember()) == null;
StringBuilder description = new StringBuilder(String.format("The %s %s has had permission overrides deleted for %s", LoggerUtility.getChannelTypeReadable(channelType), channelType == ChannelType.CATEGORY ? "`" + channel.getName() + "`" : channel.getAsMention(), roleOverride ? (deleted ? "`" + role.getName() + "`" : role.getAsMention()) : "`" + member.getEffectiveName() + "`"));
if (deleted) {
description.append(String.format(" by **%s**", roleOverride ? "role deletion" : "member leave"));
}
if (!deleted && guild.getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
this.retrieveAuditLogs(guild, ActionType.CHANNEL_OVERRIDE_DELETE).whenComplete((logs, auditException) -> {
User moderator = logs == null ? null : logs.stream().filter(e -> Duration.between(e.getTimeCreated(), ZonedDateTime.now(ZoneOffset.UTC)).toSeconds() <= 5).filter(e -> e.getTargetIdLong() == channel.getIdLong()).map(AuditLogEntry::getUser).findFirst().orElse(null);
if (moderator != null) {
loggerContext.setModerator(moderator);
description.append(String.format(" by **%s**", moderator.getAsTag()));
}
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
});
} else {
embed.setDescription(description.toString());
this.queue(guild, loggers, loggerEvent, loggerContext, embed.build());
}
});
});
}
Aggregations