Search in sources :

Example 26 with RESTOptions

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

the class Message method edit.

/**
 * Edit's the messages content. Only possible if the {@link DiscLoader loader}
 * is the message's {@link #author}
 *
 * @param content
 *            The new content of the message
 * @param embed
 *            The new embed for the message
 * @return A Future that completes with {@literal this} when sucessfull
 */
@Override
public CompletableFuture<IMessage> edit(String content, RichEmbed embed) {
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    if (!canEdit()) {
        future.completeExceptionally(new PermissionsException("Only messages author by you can be editted"));
        return future;
    }
    SendableMessage sendable = null;
    Attachment attachment = null;
    File file = null;
    if (embed != null) {
        if ((embed.getThumbnail() != null && embed.getThumbnail().resource != null)) {
            attachment = new Attachment(embed.getThumbnail().resource.getName());
        }
        if (embed.getThumbnail() != null && embed.getThumbnail().file != null) {
            attachment = new Attachment(embed.getThumbnail().file.getName());
        }
        if ((embed.getImage() != null && embed.getImage().resource != null)) {
            attachment = new Attachment(embed.getImage().resource.getName());
        }
        if (embed.getImage() != null && embed.getImage().file != null) {
            attachment = new Attachment(embed.getImage().file.getName());
        }
    }
    sendable = new SendableMessage(content, false, embed, attachment, file);
    CompletableFuture<MessageJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.message(getChannel().getID(), getID()), new RESTOptions(sendable), MessageJSON.class);
    cf.thenAcceptAsync(messageJSON -> {
        future.complete(EntityBuilder.getChannelFactory().buildMessage(channel, messageJSON));
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) MessageJSON(io.discloader.discloader.network.json.MessageJSON) SendableMessage(io.discloader.discloader.entity.sendable.SendableMessage) IMessage(io.discloader.discloader.entity.message.IMessage) Attachment(io.discloader.discloader.entity.sendable.Attachment) IMessageAttachment(io.discloader.discloader.entity.message.IMessageAttachment) File(java.io.File) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 27 with RESTOptions

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

the class Message method deleteAllReactions.

@Override
public CompletableFuture<IMessage> deleteAllReactions() {
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    if (channel instanceof IGuildChannel) {
        if (!((IGuildChannel) channel).permissionsOf(guild.getCurrentMember()).hasPermission(Permissions.MANAGE_MESSAGES)) {
            future.completeExceptionally(new PermissionsException());
            return future;
        }
    }
    CompletableFuture<Void> cf = loader.rest.request(Methods.DELETE, Endpoints.messageReactions(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) IGuildChannel(io.discloader.discloader.entity.channel.IGuildChannel) IMessage(io.discloader.discloader.entity.message.IMessage) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException)

Example 28 with RESTOptions

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

the class Reaction method getUsers.

@Override
public CompletableFuture<List<IUser>> getUsers() {
    CompletableFuture<List<IUser>> future = new CompletableFuture<>();
    CompletableFuture<IUser[]> cf = message.getLoader().rest.request(Methods.GET, Endpoints.messageReaction(message.getChannel().getID(), message.getID(), SnowflakeUtil.asString(emoji)), new RESTOptions(), IUser[].class);
    cf.thenAcceptAsync(ius -> {
        List<IUser> users = new ArrayList<>();
        for (IUser usr : ius) {
            users.add(usr);
        }
        future.complete(users);
    });
    return future;
// return new RESTAction<List<IUser>>(message.getLoader()) {
// 
// @Override
// public CompletableFuture<List<IUser>> execute() {
// return
// super.execute(loader.rest.makeRequest(Endpoints.messageReaction(message.getChannel().getID(),
// message.getID(), SnowflakeUtil.asString(emoji)), Methods.GET, true));
// }
// 
// @Override
// public void complete(String text, Throwable ex) {
// if (ex != null) {
// future.completeExceptionally(ex);
// return;
// }
// }
// }.execute();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) ArrayList(java.util.ArrayList) IUser(io.discloader.discloader.entity.user.IUser) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with RESTOptions

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

the class DLUser method getOAuth2Application.

/**
 * Gets the OAuth2 application of the logged in user, if {@link User#isBot()}
 * returns true
 *
 * @return A Future that completes with a new {@link OAuth2Application} if
 *         successful.
 * @throws AccountTypeException
 *             Thrown if the account the client is logged in as is a user
 *             account.
 */
public CompletableFuture<OAuth2Application> getOAuth2Application() {
    if (!isBot()) {
        throw new AccountTypeException("Cannot fetch the OAuth2Application details of a User Account.");
    }
    CompletableFuture<OAuth2Application> future = new CompletableFuture<>();
    CompletableFuture<OAuthApplicationJSON> cf = getLoader().rest.request(Methods.GET, Endpoints.currentOAuthApplication, new RESTOptions(), OAuthApplicationJSON.class);
    cf.thenAcceptAsync(appData -> {
        IUser owner = EntityRegistry.addUser(appData.owner);
        future.complete(new OAuth2Application(appData, owner));
    });
    cf.exceptionally(ex -> {
        future.completeExceptionally(ex);
        return null;
    });
    return future;
}
Also used : OAuthApplicationJSON(io.discloader.discloader.network.json.OAuthApplicationJSON) CompletableFuture(java.util.concurrent.CompletableFuture) RESTOptions(io.discloader.discloader.network.rest.RESTOptions) AccountTypeException(io.discloader.discloader.common.exceptions.AccountTypeException) IUser(io.discloader.discloader.entity.user.IUser)

Example 30 with RESTOptions

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

the class DLUser method setUsername.

/**
 * Set's the currently logged in user's username.
 *
 * @param username
 *            The new username for the account
 * @return A Future that completes with a {@link User} Object if successful
 */
public CompletableFuture<DLUser> setUsername(String username) {
    CompletableFuture<DLUser> future = new CompletableFuture<>();
    CompletableFuture<UserJSON> cf = loader.rest.request(Methods.PATCH, Endpoints.currentUser, new RESTOptions(new JSONObject().put("username", username)), 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)

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