Search in sources :

Example 11 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, int position) {
    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]");
    }
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("name", sanitizeChannelName(name)).put("position", position);
    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 12 with PermissionsException

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

the class Guild method createVoiceChannel.

@Override
public CompletableFuture<IGuildVoiceChannel> createVoiceChannel(String name, int bitRate, int userLimit, IChannelCategory category, IOverwrite... overwrites) {
    CompletableFuture<IGuildVoiceChannel> future = new CompletableFuture<>();
    if (!hasPermission(Permissions.MANAGE_CHANNELS)) {
        PermissionsException ex = new PermissionsException("Insufficient Permissions");
        future.completeExceptionally(ex);
        // return early
        return future;
    }
    // normalize
    bitRate = Math.max(8, Math.min(96, bitRate)) * 1000;
    // normalize
    userLimit = Math.max(0, Math.min(99, userLimit));
    ChannelPayload data = new ChannelPayload(name, bitRate, userLimit, overwrites);
    if (category != null && getChannelCategoryByID(category.getID()) != null) {
        data.setParent(category);
    }
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getID()), new RESTOptions(data), ChannelJSON.class);
    cf.thenAcceptAsync(channelJSON -> {
        if (channelJSON != null) {
            IGuildVoiceChannel channel = (IGuildVoiceChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), this, false);
            if (channel != null)
                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) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) ChannelPayload(io.discloader.discloader.network.rest.payloads.ChannelPayload) IGuildVoiceChannel(io.discloader.discloader.entity.channel.IGuildVoiceChannel) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 13 with PermissionsException

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

the class Guild method ban.

@Override
public CompletableFuture<IGuildMember> ban(IGuildMember member, String reason) throws PermissionsException {
    if (!hasPermission(Permissions.BAN_MEMBERS))
        throw new PermissionsException("");
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    loader.rest.request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(reason), Void.class).thenAcceptAsync(action -> {
        future.complete(member);
    });
    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 14 with PermissionsException

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

the class Guild method ban.

@Override
public CompletableFuture<IGuildMember> ban(IGuildMember member) {
    if (!hasPermission(Permissions.BAN_MEMBERS))
        throw new PermissionsException("");
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    loader.rest.request(Methods.PUT, Endpoints.guildBanMember(getID(), member.getID()), new RESTOptions(), Void.class).thenAcceptAsync(action -> {
        future.complete(member);
    });
    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 15 with PermissionsException

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

the class GuildEmoji method setRoles.

@Override
public CompletableFuture<IGuildEmoji> setRoles(IRole... roles) {
    if (!guild.hasPermission(Permissions.MANAGE_EMOJIS)) {
        throw new PermissionsException("Insufficient Permissions: \"MANAGE_EMOJIS\" is required to use this endpoint");
    }
    CompletableFuture<IGuildEmoji> future = new CompletableFuture<>();
    String[] payload = new String[roles.length];
    for (int i = 0; i < roles.length; i++) {
        payload[i] = SnowflakeUtil.asString(roles[i]);
    }
    IEventListener el = new EventListenerAdapter() {

        @Override
        public void GuildEmojiUpdate(GuildEmojiUpdateEvent event) {
            if (getID() == event.getEmoji().getID()) {
                future.complete(event.getEmoji());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(el).rest.request(Methods.PATCH, Endpoints.guildEmoji(getGuild().getID(), getID()), new RESTOptions(payload), EmojiJSON.class).exceptionally(ex -> {
        future.completeExceptionally(ex);
        getLoader().removeEventListener(el);
        return null;
    });
    return future;
}
Also used : IGuildEmoji(io.discloader.discloader.entity.guild.IGuildEmoji) GuildEmojiUpdateEvent(io.discloader.discloader.common.event.guild.emoji.GuildEmojiUpdateEvent) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) EmojiJSON(io.discloader.discloader.network.json.EmojiJSON)

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