Search in sources :

Example 16 with PermissionsException

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

the class GuildMember method giveRole.

/**
 * Gives a member a new role
 *
 * @param roles
 *            The roles to give to the member
 * @return A CompletableFuture that completes with {@code this} if successful
 * @throws PermissionsException
 *             thrown if a role with a higher position than the current user's
 *             highest role is attempted to be given to the member. Also thrown
 *             if the current user doesn't have the MANAGE_ROLE permission.
 */
@Override
public CompletableFuture<IGuildMember> giveRole(IRole... roles) {
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    if (!guild.hasPermission(Permissions.MANAGE_ROLES)) {
        future.completeExceptionally(new PermissionsException("Insufficient Permissions"));
        return future;
    }
    for (IRole role : roles) {
        if (role == null)
            continue;
        if (!guild.isOwner() && role.getPosition() >= guild.getCurrentMember().getHighestRole().getPosition()) {
            future.completeExceptionally(new PermissionsException("Can not assign higher role"));
            return future;
        // throw ;
        }
    }
    List<IRole> rls = mergeRoles(roles);
    String[] ids = new String[rls.size()];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = SnowflakeUtil.asString(rls.get(i));
    }
    JSONObject payload = new JSONObject().put("roles", ids);
    System.out.println(payload);
    CompletableFuture<Void> vcf = getLoader().rest.request(Methods.PATCH, Endpoints.guildMember(getGuild().getID(), getID()), new RESTOptions(payload), Void.class);
    vcf.thenAcceptAsync(v -> {
        getLoader().addEventListener(new EventListenerAdapter() {

            public void GuildMemberUpdate(GuildMemberUpdateEvent e) {
                future.complete(e.getMember());
                getLoader().removeEventListener(this);
            }
        });
    });
    vcf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    // }
    return future;
}
Also used : IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) CompletableFuture(java.util.concurrent.CompletableFuture) IRole(io.discloader.discloader.entity.guild.IRole) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 17 with PermissionsException

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

the class GuildMember method setNick.

/**
 * Sets the member's nickname if the {@link DiscLoader loader} has sufficient
 * permissions. Requires the {@link Permissions#MANAGE_NICKNAMES} permission.
 *
 * @param nick
 *            The member's new nickname
 * @see Permission
 * @return A CompletableFuture that completes with {@code this} if successful
 * @throws PermissionsException
 *             thrown if the current user doesn't have the
 *             {@link Permissions#MANAGE_NICKNAMES} permission.
 */
@Override
public CompletableFuture<IGuildMember> setNick(String nick) {
    if ((equals(guild.getCurrentMember()) && !guild.hasPermission(Permissions.CHANGE_NICKNAME)) || (!equals(guild.getCurrentMember()) && !guild.hasPermission(Permissions.MANAGE_NICKNAMES))) {
        throw new PermissionsException("Insuccficient Permissions");
    }
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().put("nick", nick);
    String endpoint = equals(guild.getCurrentMember()) ? Endpoints.guildNick(guild.getID()) : Endpoints.guildMember(guild.getID(), getID());
    CompletableFuture<Void> cf = getLoader().rest.request(Methods.PATCH, endpoint, new RESTOptions(payload), Void.class);
    IEventListener iel = new EventListenerAdapter() {

        public void GuildMemberNicknameUpdated(NicknameUpdateEvent e) {
            if (e.getMember().getID() == getID() && e.getGuild().equals(guild)) {
                future.complete(e.getMember());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(iel);
    cf.exceptionally(ex -> {
        getLoader().removeEventListener(iel);
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) NicknameUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberEvent.NicknameUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 18 with PermissionsException

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

the class GuildMember method takeRole.

@Override
public CompletableFuture<IGuildMember> takeRole(IRole... roles) {
    if (!guild.hasPermission(Permissions.MANAGE_ROLES)) {
        throw new PermissionsException("Insuccficient Permissions");
    }
    List<IRole> rls = getRoles();
    for (IRole role : roles) {
        if (!guild.isOwner() && role.getPosition() >= guild.getCurrentMember().getHighestRole().getPosition()) {
            throw new PermissionsException("Cannot take away roles higher than your's");
        }
        if (hasRole(role)) {
            rls.remove(role);
        }
    }
    CompletableFuture<IGuildMember> future = new CompletableFuture<>();
    String[] ids = new String[rls.size()];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = SnowflakeUtil.asString(rls.get(i));
    }
    JSONObject payload = new JSONObject().put("roles", ids);
    CompletableFuture<Void> tcf = getLoader().rest.request(Methods.PATCH, Endpoints.guildMember(getGuild().getID(), getID()), new RESTOptions(payload), Void.class);
    IEventListener iel = new EventListenerAdapter() {

        public void GuildMemberUpdate(GuildMemberUpdateEvent e) {
            if (e.getMember().getID() == getID()) {
                future.complete(e.getMember());
                getLoader().removeEventListener(this);
            }
        }
    };
    getLoader().addEventListener(iel);
    tcf.exceptionally(ex -> {
        getLoader().removeEventListener(iel);
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) IEventListener(io.discloader.discloader.common.event.IEventListener) CompletableFuture(java.util.concurrent.CompletableFuture) IRole(io.discloader.discloader.entity.guild.IRole) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) GuildMemberUpdateEvent(io.discloader.discloader.common.event.guild.member.GuildMemberUpdateEvent) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter)

Example 19 with PermissionsException

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

the class GuildChannel method edit.

@Override
public CompletableFuture<? extends IGuildChannel> edit(String name, int position, boolean nsfw, IOverwrite... overwrites) throws PermissionsException {
    CompletableFuture<IGuildChannel> future = new CompletableFuture<>();
    JSONObject settings = new JSONObject().put("name", name).put("position", position).put("nsfw", nsfw).put("permission_overwrites", overwrites);
    loader.rest.request(Methods.PATCH, Endpoints.channel(getID()), new RESTOptions(settings), ChannelJSON.class).thenAcceptAsync(data -> {
        IChannel newChannel = EntityBuilder.getChannelFactory().buildChannel(data, getLoader(), getGuild(), false);
        if (newChannel instanceof IGuildChannel)
            future.complete((IGuildChannel) newChannel);
    }).exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : Endpoints(io.discloader.discloader.network.util.Endpoints) IChannel(io.discloader.discloader.entity.channel.IChannel) Methods(io.discloader.discloader.network.util.Methods) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IPermission(io.discloader.discloader.entity.IPermission) ArrayList(java.util.ArrayList) SnowflakeUtil(io.discloader.discloader.entity.util.SnowflakeUtil) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) JSONObject(org.json.JSONObject) Map(java.util.Map) Permission(io.discloader.discloader.core.entity.Permission) IGuild(io.discloader.discloader.entity.guild.IGuild) EventListenerAdapter(io.discloader.discloader.common.event.EventListenerAdapter) FetchInvites(io.discloader.discloader.network.rest.actions.channel.FetchInvites) Role(io.discloader.discloader.core.entity.guild.Role) IGuildMember(io.discloader.discloader.entity.guild.IGuildMember) SetOverwrite(io.discloader.discloader.network.rest.actions.channel.SetOverwrite) CloseGuildChannel(io.discloader.discloader.network.rest.actions.channel.close.CloseGuildChannel) IOverwrite(io.discloader.discloader.entity.IOverwrite) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IRole(io.discloader.discloader.entity.guild.IRole) Permissions(io.discloader.discloader.entity.util.Permissions) EntityBuilder(io.discloader.discloader.common.registry.EntityBuilder) List(java.util.List) Guild(io.discloader.discloader.core.entity.guild.Guild) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) IInvite(io.discloader.discloader.entity.invite.IInvite) ChannelJSON(io.discloader.discloader.network.json.ChannelJSON) GuildChannelUpdateEvent(io.discloader.discloader.common.event.channel.GuildChannelUpdateEvent) Overwrite(io.discloader.discloader.core.entity.Overwrite) IChannelCategory(io.discloader.discloader.entity.channel.IChannelCategory) EntityRegistry(io.discloader.discloader.common.registry.EntityRegistry) CompletableFuture(java.util.concurrent.CompletableFuture) IChannel(io.discloader.discloader.entity.channel.IChannel) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel)

Example 20 with PermissionsException

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

the class TextChannel method setTopic.

public CompletableFuture<IGuildTextChannel> setTopic(String topic) {
    if (!this.permissionsOf(guild.getCurrentMember()).hasAny(Permissions.MANAGE_CHANNELS, Permissions.ADMINISTRATOR) && !guild.isOwner()) {
        throw new PermissionsException("Insufficient Permissions");
    }
    if (topic.length() > 1024) {
        throw new RuntimeException("topic length [" + topic.length() + "] > 1024");
    }
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    JSONObject payload = new JSONObject().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)

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