Search in sources :

Example 1 with SendableMessage

use of io.discloader.discloader.entity.sendable.SendableMessage in project DiscLoader by R3alCl0ud.

the class PrivateChannel method sendMessage.

/*
	 * (non-Javadoc)
	 * 
	 * @see io.discloader.discloader.entity.channel.ITextChannel#sendMessage(java.
	 * lang.String, io.discloader.discloader.core.entity.RichEmbed,
	 * io.discloader.discloader.entity.sendable.Attachment)
	 */
@Override
public CompletableFuture<IMessage> sendMessage(String content, RichEmbed embed, Attachment attachment) {
    SendableMessage sendable = new SendableMessage(content, false, embed, attachment, new File(attachment.filename));
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    CompletableFuture<MessageJSON> mcf = loader.rest.request(Methods.POST, Endpoints.messages(getID()), new RESTOptions(sendable), MessageJSON.class);
    mcf.thenAcceptAsync(e -> {
        future.complete(new Message<>(this, e));
    });
    mcf.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) File(java.io.File)

Example 2 with SendableMessage

use of io.discloader.discloader.entity.sendable.SendableMessage 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 3 with SendableMessage

use of io.discloader.discloader.entity.sendable.SendableMessage in project DiscLoader by R3alCl0ud.

the class TextChannel method sendMessage.

@Override
public CompletableFuture<IMessage> sendMessage(String content, RichEmbed embed, File file) {
    Attachment attachment = file == null ? null : new Attachment(file.getName());
    SendableMessage sendable = new SendableMessage(content, false, embed, attachment, file);
    CompletableFuture<IMessage> future = new CompletableFuture<>();
    CompletableFuture<MessageJSON> mcf = loader.rest.request(Methods.POST, Endpoints.messages(getID()), new RESTOptions(sendable), MessageJSON.class);
    mcf.thenAcceptAsync(e -> {
        future.complete(new Message<>(this, e));
    });
    mcf.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)

Example 4 with SendableMessage

use of io.discloader.discloader.entity.sendable.SendableMessage in project DiscLoader by R3alCl0ud.

the class APIRequest method createRequest.

public BaseRequest createRequest() throws IOException {
    BaseRequest request = null;
    switch(this.method) {
        case Methods.GET:
            request = Unirest.get(this.route);
            break;
        case Methods.POST:
            request = Unirest.post(this.route);
            if (multi) {
                // try {
                SendableMessage sdata = (SendableMessage) this.data;
                File file = sdata.file;
                Resource resource = sdata.resource;
                byte[] bytes = new byte[0];
                if (file != null)
                    bytes = DLUtil.readAllBytes(file);
                if (resource != null)
                    bytes = DLUtil.readAllBytes(resource);
                MultipartBody body = ((HttpRequestWithBody) request).fields(null);
                String loc = "";
                if (file != null)
                    loc = file.getName();
                if (resource != null)
                    loc = resource.getFileName();
                body.field("Content-type", "multipart/form-data").field("file", bytes, loc).field("payload_json", gson.toJson(sdata));
            // } catch (IOException ex) {
            // ex.printStackTrace();
            // }
            } else {
                ((HttpRequestWithBody) request).body(gson.toJson(data));
            }
            break;
        case Methods.PATCH:
            request = Unirest.patch(this.route);
            ((HttpRequestWithBody) request).body(gson.toJson(data));
            break;
        case Methods.DELETE:
            request = Unirest.delete(this.route);
            break;
        case DLUtil.Methods.PUT:
            request = Unirest.put(this.route);
            ((HttpRequestWithBody) request).body(gson.toJson(this.data));
            break;
        default:
            request = Unirest.get(this.route);
            break;
    }
    return request;
}
Also used : SendableMessage(io.discloader.discloader.entity.sendable.SendableMessage) MultipartBody(com.mashape.unirest.request.body.MultipartBody) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) BaseRequest(com.mashape.unirest.request.BaseRequest) Resource(io.discloader.discloader.client.render.util.Resource) File(java.io.File)

Example 5 with SendableMessage

use of io.discloader.discloader.entity.sendable.SendableMessage in project DiscLoader by R3alCl0ud.

the class Route method createRequest.

public BaseRequest createRequest(Request<T> request) {
    BaseRequest base = null;
    switch(method) {
        case GET:
            base = Unirest.get(endpoint);
            break;
        case POST:
            base = Unirest.post(endpoint);
            if (request.getData() instanceof SendableMessage) {
                MultipartBody body = ((HttpRequestWithBody) base).fields(null);
                body.field("Content-Type", "multipart/form-data");
                SendableMessage sdata = (SendableMessage) request.getData();
                if (sdata.file != null || sdata.resource != null) {
                    try {
                        File file = sdata.file;
                        Resource resource = sdata.resource;
                        byte[] bytes = new byte[0];
                        if (file != null)
                            bytes = DLUtil.readAllBytes(file);
                        if (resource != null && resource.getResourceAsStream() != null)
                            bytes = DLUtil.readAllBytes(resource);
                        String loc = "";
                        if (file != null)
                            loc = file.getName();
                        if (resource != null)
                            loc = resource.getFileName();
                        body.field("file", bytes, loc);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                try {
                    if (request.getData() instanceof JSONObject) {
                        body.field("payload_json", request.getData().toString());
                    } else if (request.getData() instanceof String) {
                        body.field("payload_json", request.getData());
                    } else {
                        body.field("payload_json", gson.toJson(request.getData()));
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                base.getHttpRequest().header("Content-Type", "application/json");
                if (request.getData() instanceof JSONObject) {
                    ((HttpRequestWithBody) base).body(request.getData().toString());
                } else if (request.getData() instanceof String) {
                    ((HttpRequestWithBody) base).body(request.getData());
                } else {
                    ((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
                }
            }
            break;
        case DELETE:
            base = Unirest.delete(endpoint);
            break;
        case PATCH:
            base = Unirest.patch(endpoint);
            base.getHttpRequest().header("Content-Type", "application/json");
            if (request.getData() instanceof JSONObject) {
                ((HttpRequestWithBody) base).body(request.getData().toString());
            } else if (request.getData() instanceof String) {
                ((HttpRequestWithBody) base).body(request.getData().toString());
            } else {
                ((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
            }
            break;
        case PUT:
            base = Unirest.put(endpoint);
            base.getHttpRequest().header("Content-Type", "application/json");
            if (request.getData() instanceof JSONObject) {
                ((HttpRequestWithBody) base).body(request.getData().toString());
            } else if (request.getData() instanceof String) {
                ((HttpRequestWithBody) base).body(request.getData());
            } else {
                ((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
            }
            break;
        default:
            base = Unirest.get(endpoint);
            break;
    }
    // body.
    HttpRequest httprequest = base.getHttpRequest();
    if (auth && rest.loader.token != null)
        httprequest.header("Authorization", rest.loader.token);
    if (request.getOptions() != null && request.getOptions().getReason() != null) {
        httprequest.header("X-Audit-Log-Reason", request.getOptions().getReason());
        System.out.println("is this thing working?");
    }
    httprequest.header("user-agent", "DiscordBot (http://discloader.io, v0.2.0)");
    httprequest.header("Accept-Encoding", "gzip");
    return base;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) JSONObject(org.json.JSONObject) SendableMessage(io.discloader.discloader.entity.sendable.SendableMessage) MultipartBody(com.mashape.unirest.request.body.MultipartBody) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) BaseRequest(com.mashape.unirest.request.BaseRequest) Resource(io.discloader.discloader.client.render.util.Resource) File(java.io.File) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) DiscordException(io.discloader.discloader.common.exceptions.DiscordException)

Aggregations

SendableMessage (io.discloader.discloader.entity.sendable.SendableMessage)8 IMessage (io.discloader.discloader.entity.message.IMessage)6 MessageJSON (io.discloader.discloader.network.json.MessageJSON)6 RESTOptions (io.discloader.discloader.network.rest.RESTOptions)6 File (java.io.File)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Attachment (io.discloader.discloader.entity.sendable.Attachment)3 BaseRequest (com.mashape.unirest.request.BaseRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 MultipartBody (com.mashape.unirest.request.body.MultipartBody)2 Resource (io.discloader.discloader.client.render.util.Resource)2 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 HttpRequest (com.mashape.unirest.request.HttpRequest)1 DiscordException (io.discloader.discloader.common.exceptions.DiscordException)1 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)1 IMessageAttachment (io.discloader.discloader.entity.message.IMessageAttachment)1 JSONObject (org.json.JSONObject)1