Search in sources :

Example 1 with MongoDatabase

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;
}
Also used : Role(net.dv8tion.jda.api.entities.Role) CompletableFuture(java.util.concurrent.CompletableFuture) BotPermissionException(com.sx4.bot.exceptions.mod.BotPermissionException) MaxRolesException(com.sx4.bot.exceptions.mod.MaxRolesException) Member(net.dv8tion.jda.api.entities.Member)

Aggregations

BotPermissionException (com.sx4.bot.exceptions.mod.BotPermissionException)1 MaxRolesException (com.sx4.bot.exceptions.mod.MaxRolesException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Member (net.dv8tion.jda.api.entities.Member)1 Role (net.dv8tion.jda.api.entities.Role)1