Search in sources :

Example 46 with RESTOptions

use of io.discloader.discloader.network.rest.RESTOptions 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 47 with RESTOptions

use of io.discloader.discloader.network.rest.RESTOptions 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 48 with RESTOptions

use of io.discloader.discloader.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.

the class Message method delete.

@Override
public CompletableFuture<IMessage> delete() {
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    CompletableFuture<Void> cf = loader.rest.request(Methods.DELETE, Endpoints.message(getChannel().getID(), getID()), new RESTOptions(), Void.class);
    cf.thenAcceptAsync(Null -> {
        future.complete(this);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IMessage(io.discloader.discloader.entity.message.IMessage)

Example 49 with RESTOptions

use of io.discloader.discloader.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.

the class DLUser method setAvatar.

/**
 * Sets the currently logged in user's avatar
 *
 * @param avatarLocation
 *            The location on disk of the new avatar image
 * @return A CompletableFuture that completes with {@code this} if successfull,
 *         or the error response if failed. Returns null if
 *         {@code this.id != this.loader.user.id}
 */
public CompletableFuture<DLUser> setAvatar(String avatarLocation) throws IOException {
    CompletableFuture<DLUser> future = new CompletableFuture<>();
    String base64 = "data:image/jpg;base64," + Base64.encodeBase64String(Files.readAllBytes(Paths.get(avatarLocation)));
    CompletableFuture<UserJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.currentUser, new RESTOptions(new JSONObject().put("avatar", base64)), UserJSON.class);
    cf.thenAcceptAsync(data -> {
        setup(data);
        future.complete(this);
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : UserJSON(io.discloader.discloader.network.json.UserJSON) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) JSONObject(org.json.JSONObject)

Example 50 with RESTOptions

use of io.discloader.discloader.network.rest.RESTOptions in project DiscLoader by R3alCl0ud.

the class ChannelCategory method createTextChannel.

@Override
public CompletableFuture<IGuildTextChannel> createTextChannel(String name) {
    CompletableFuture<IGuildTextChannel> future = new CompletableFuture<>();
    JSONObject data = new JSONObject().put("parent_id", SnowflakeUtil.asString(this)).put("name", name).put("type", 0);
    // System.out.println(data.toString());
    CompletableFuture<ChannelJSON> cf = loader.rest.request(Methods.POST, Endpoints.guildChannels(getGuild().getID()), new RESTOptions(data), ChannelJSON.class);
    cf.thenAcceptAsync(channelJSON -> {
        if (channelJSON != null) {
            IGuildTextChannel channel = (IGuildTextChannel) EntityBuilder.getChannelFactory().buildChannel(channelJSON, getLoader(), getGuild(), 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) JSONObject(org.json.JSONObject) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) IGuildTextChannel(io.discloader.discloader.entity.channel.IGuildTextChannel)

Aggregations

RESTOptions (io.discloader.discloader.network.rest.RESTOptions)62 CompletableFuture (java.util.concurrent.CompletableFuture)61 JSONObject (org.json.JSONObject)29 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)23 ChannelJSON (io.discloader.discloader.network.json.ChannelJSON)21 IGuildMember (io.discloader.discloader.entity.guild.IGuildMember)11 EventListenerAdapter (io.discloader.discloader.common.event.EventListenerAdapter)10 IGuildChannel (io.discloader.discloader.entity.channel.IGuildChannel)10 IGuildTextChannel (io.discloader.discloader.entity.channel.IGuildTextChannel)9 AuditLog (io.discloader.discloader.core.entity.auditlog.AuditLog)8 IAuditLog (io.discloader.discloader.entity.auditlog.IAuditLog)8 IChannelCategory (io.discloader.discloader.entity.channel.IChannelCategory)8 IMessage (io.discloader.discloader.entity.message.IMessage)8 AuditLogJSON (io.discloader.discloader.network.json.AuditLogJSON)8 ArrayList (java.util.ArrayList)8 IOverwrite (io.discloader.discloader.entity.IOverwrite)7 IGuild (io.discloader.discloader.entity.guild.IGuild)7 IInvite (io.discloader.discloader.entity.invite.IInvite)7 HashMap (java.util.HashMap)7 List (java.util.List)7