use of com.sx4.bot.database.mongo.MongoDatabase in project Sx4 by sx4-discord-bot.
the class ModUtility method createMuteRole.
private static CompletableFuture<Role> createMuteRole(MongoDatabase database, Guild guild, boolean autoUpdate) {
Member selfMember = guild.getSelfMember();
if (guild.getRoleCache().size() >= 250) {
return CompletableFuture.failedFuture(new MaxRolesException("The guild has the max roles possible (250) so I was unable to make the mute role"));
}
if (!selfMember.hasPermission(Permission.MANAGE_ROLES)) {
return CompletableFuture.failedFuture(new BotPermissionException(Permission.MANAGE_ROLES));
}
CompletableFuture<Role> future = new CompletableFuture<>();
guild.createRole().setName("Muted - " + selfMember.getUser().getName()).queue(newRole -> {
database.updateGuildById(guild.getIdLong(), Updates.set("mute.roleId", newRole.getIdLong())).whenComplete((result, exception) -> {
if (ExceptionUtility.sendErrorMessage(exception)) {
future.completeExceptionally(exception);
} else {
future.complete(newRole);
if (autoUpdate) {
guild.getTextChannels().forEach(channel -> {
if (selfMember.hasPermission(channel, Permission.MANAGE_PERMISSIONS)) {
channel.upsertPermissionOverride(newRole).deny(Permission.MESSAGE_WRITE).queue();
}
});
}
}
});
});
return future;
}
Aggregations