Search in sources :

Example 1 with PermissionsException

use of io.discloader.discloader.common.exceptions.PermissionsException in project DiscLoader by R3alCl0ud.

the class TextChannel method edit.

@Override
public CompletableFuture<IGuildTextChannel> edit(String name, String topic) {
    if (!this.permissionsOf(guild.getCurrentMember()).hasAny(Permissions.MANAGE_CHANNELS, Permissions.ADMINISTRATOR) && !guild.isOwner()) {
        throw new PermissionsException("Insufficient Permissions");
    }
    if (name.length() < 2 || name.length() > 100) {
        throw new RuntimeException("Name.length() out of bounds [2-100]");
    }
    if (topic.length() > 1024) {
        throw new RuntimeException("Topic.length() out of bounds [" + topic.length() + " > 1024]");
    }
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("name", sanitizeChannelName(name)).put("topic", topic);
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(payload), ChannelJSON.class);
    cf.thenAcceptAsync(data -> {
        IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), getGuild(), false);
        future.complete(channel);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 2 with PermissionsException

use of io.discloader.discloader.common.exceptions.PermissionsException in project DiscLoader by R3alCl0ud.

the class TextChannel method setNSFW.

@Override
public CompletableFuture<IGuildTextChannel> setNSFW(boolean nsfw) {
    if (!this.permissionsOf(guild.getCurrentMember()).hasAny(Permissions.MANAGE_CHANNELS, Permissions.ADMINISTRATOR) && !guild.isOwner()) {
        throw new PermissionsException("Insufficient Permissions");
    }
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("nsfw", nsfw);
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(payload), ChannelJSON.class);
    cf.thenAcceptAsync(data -> {
        IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), getGuild(), false);
        future.complete(channel);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 3 with PermissionsException

use of io.discloader.discloader.common.exceptions.PermissionsException in project DiscLoader by R3alCl0ud.

the class Guild method beginPrune.

@Override
public CompletableFuture<Integer> beginPrune(int days) {
    if (!getCurrentMember().getPermissions().hasPermission(Permissions.KICK_MEMBERS))
        throw new PermissionsException("Pruning members requires the 'KICK_MEMBERS' permission");
    CompletableFuture<Integer> future = new CompletableFuture<>();
    loader.rest.request(Methods.POST, Endpoints.guildPrune(getID()), new RESTOptions(), Integer.class).thenAcceptAsync(pruned -> {
        future.complete(pruned);
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 4 with PermissionsException

use of io.discloader.discloader.common.exceptions.PermissionsException in project DiscLoader by R3alCl0ud.

the class Guild method kick.

@Override
public CompletableFuture<IGuildMember> kick(IGuildMember member, String reason) {
    if (!isOwner() && getCurrentMember().getPermissions().hasPermission(Permissions.KICK_MEMBERS))
        throw new PermissionsException("Insufficient Permissions");
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    CompletableFuture<Void> kickFuture = loader.rest.request(Methods.DELETE, Endpoints.guildMember(getID(), member.getID()), new RESTOptions(reason), Void.class);
    kickFuture.thenAcceptAsync(n -> {
        future.complete(member);
    });
    kickFuture.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 5 with PermissionsException

use of io.discloader.discloader.common.exceptions.PermissionsException in project DiscLoader by R3alCl0ud.

the class Guild method edit.

public CompletableFuture<IGuild> edit(String name, String icon, IGuildVoiceChannel afkChannel) throws IOException {
    if (!isOwner() && !getCurrentMember().getPermissions().hasPermission(Permissions.MANAGE_GUILD)) {
        throw new PermissionsException();
    }
    CompletableFuture<IGuild> future = new CompletableFuture<>();
    String base64 = new String("data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(Paths.get(icon))));
    JSONObject payload = new JSONObject().put("name", name).put("icon", base64);
    CompletableFuture<GuildJSON> cf = getLoader().rest.request(Methods.PATCH, Endpoints.guild(getID()), new RESTOptions(payload), GuildJSON.class);
    cf.thenAcceptAsync(guildJSON -> {
        IGuild guild = clone();
        guild.setup(guildJSON);
        future.complete(guild);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildJSON(io.discloader.discloader.network.json.GuildJSON) IGuild(io.discloader.discloader.entity.guild.IGuild) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Aggregations

PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)20 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)19 CompletableFuture (java.util.concurrent.CompletableFuture)19 JSONObject (org.json.JSONObject)10 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)7 ChannelJSON (io.discloader.discloader.network.json.ChannelJSON)7 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)6 IGuildTextChannel (io.discloader.discloader.entity.channel.IGuildTextChannel)5 IEventListener (io.discloader.discloader.common.event.IEventListener)4 IRole (io.discloader.discloader.entity.guild.IRole)3 GuildEmojiUpdateEvent (io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent)2 GuildMemberUpdateEvent (io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent)2 IGuildChannel (io.discloader.discloader.entity.channel.IGuildChannel)2 IGuild (io.discloader.discloader.entity.guild.IGuild)2 IGuildEmoji (io.discloader.discloader.entity.guild.IGuildEmoji)2 IMessage (io.discloader.discloader.entity.message.IMessage)2 EmojiJSON (io.discloader.discloader.network.json.EmojiJSON)2 ChannelPayload (io.discloader.discloader.network.rest.payloads.ChannelPayload)2 ArrayList (java.util.ArrayList)2 HttpResponse (com.mashape.unirest.http.HttpResponse)1