Search in sources :

Example 6 with Message

use of de.btobastian.javacord.entities.message.Message in project Javacord by BtoBastian.

the class ImplUser method sendFile.

@Override
public Future<Message> sendFile(final File file, final String comment, FutureCallback<Message> callback) {
    final MessageReceiver receiver = this;
    ListenableFuture<Message> future = api.getThreadPool().getListeningExecutorService().submit(new Callable<Message>() {

        @Override
        public Message call() throws Exception {
            logger.debug("Trying to send a file to user {} (name: {}, comment: {})", ImplUser.this, file.getName(), comment);
            api.checkRateLimit(null, RateLimitType.PRIVATE_MESSAGE, null, null);
            MultipartBody body = Unirest.post("https://discordapp.com/api/v6/channels/" + getUserChannelIdBlocking() + "/messages").header("authorization", api.getToken()).field("file", file);
            if (comment != null) {
                body.field("content", comment);
            }
            HttpResponse<JsonNode> response = body.asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.PRIVATE_MESSAGE, null, null);
            logger.debug("Sent a file to user {} (name: {}, comment: {})", ImplUser.this, file.getName(), comment);
            return new ImplMessage(response.getBody().getObject(), api, receiver);
        }
    });
    if (callback != null) {
        Futures.addCallback(future, callback);
    }
    return future;
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) MultipartBody(com.mashape.unirest.request.body.MultipartBody) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) HttpResponse(com.mashape.unirest.http.HttpResponse) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Example 7 with Message

use of de.btobastian.javacord.entities.message.Message in project Javacord by BtoBastian.

the class ImplUser method sendMessage.

@Override
public Future<Message> sendMessage(final String content, final EmbedBuilder embed, final boolean tts, final String nonce, FutureCallback<Message> callback) {
    final MessageReceiver receiver = this;
    ListenableFuture<Message> future = api.getThreadPool().getListeningExecutorService().submit(new Callable<Message>() {

        @Override
        public Message call() throws Exception {
            logger.debug("Trying to send message to user {} (content: \"{}\", tts: {})", ImplUser.this, content, tts);
            api.checkRateLimit(null, RateLimitType.PRIVATE_MESSAGE, null, null);
            JSONObject body = new JSONObject().put("content", content).put("tts", tts).put("mentions", new String[0]);
            if (embed != null) {
                body.put("embed", embed.toJSONObject());
            }
            if (nonce != null) {
                body.put("nonce", nonce);
            }
            HttpResponse<JsonNode> response = Unirest.post("https://discordapp.com/api/v6/channels/" + getUserChannelIdBlocking() + "/messages").header("authorization", api.getToken()).header("content-type", "application/json").body(body.toString()).asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.PRIVATE_MESSAGE, null, null);
            logger.debug("Sent message to user {} (content: \"{}\", tts: {})", ImplUser.this, content, tts);
            return new ImplMessage(response.getBody().getObject(), api, receiver);
        }
    });
    if (callback != null) {
        Futures.addCallback(future, callback);
    }
    return future;
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) JSONObject(org.json.JSONObject) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) HttpResponse(com.mashape.unirest.http.HttpResponse) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Example 8 with Message

use of de.btobastian.javacord.entities.message.Message in project Javacord by BtoBastian.

the class ImplChannel method sendFile.

@Override
public Future<Message> sendFile(final File file, final String comment, FutureCallback<Message> callback) {
    final MessageReceiver receiver = this;
    ListenableFuture<Message> future = api.getThreadPool().getListeningExecutorService().submit(new Callable<Message>() {

        @Override
        public Message call() throws Exception {
            logger.debug("Trying to send a file in channel {} (name: {}, comment: {})", ImplChannel.this, file.getName(), comment);
            api.checkRateLimit(null, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            MultipartBody body = Unirest.post("https://discordapp.com/api/v6/channels/" + id + "/messages").header("authorization", api.getToken()).field("file", file);
            if (comment != null) {
                body.field("content", comment);
            }
            HttpResponse<JsonNode> response = body.asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            logger.debug("Sent a file in channel {} (name: {}, comment: {})", ImplChannel.this, file.getName(), comment);
            return new ImplMessage(response.getBody().getObject(), api, receiver);
        }
    });
    if (callback != null) {
        Futures.addCallback(future, callback);
    }
    return future;
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) MultipartBody(com.mashape.unirest.request.body.MultipartBody) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) HttpResponse(com.mashape.unirest.http.HttpResponse) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JSONException(org.json.JSONException)

Example 9 with Message

use of de.btobastian.javacord.entities.message.Message in project Javacord by BtoBastian.

the class ImplChannel method sendFile.

@Override
public Future<Message> sendFile(final InputStream inputStream, final String filename, final String comment, FutureCallback<Message> callback) {
    final MessageReceiver receiver = this;
    ListenableFuture<Message> future = api.getThreadPool().getListeningExecutorService().submit(new Callable<Message>() {

        @Override
        public Message call() throws Exception {
            logger.debug("Trying to send an input stream in channel {} (comment: {})", ImplChannel.this, comment);
            api.checkRateLimit(null, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            MultipartBody body = Unirest.post("https://discordapp.com/api/v6/channels/" + id + "/messages").header("authorization", api.getToken()).field("file", inputStream, filename);
            if (comment != null) {
                body.field("content", comment);
            }
            HttpResponse<JsonNode> response = body.asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            logger.debug("Sent an input stream in channel {} (comment: {})", ImplChannel.this, comment);
            return new ImplMessage(response.getBody().getObject(), api, receiver);
        }
    });
    if (callback != null) {
        Futures.addCallback(future, callback);
    }
    return future;
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) MultipartBody(com.mashape.unirest.request.body.MultipartBody) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) HttpResponse(com.mashape.unirest.http.HttpResponse) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JSONException(org.json.JSONException)

Example 10 with Message

use of de.btobastian.javacord.entities.message.Message in project Javacord by BtoBastian.

the class ImplUser method sendFile.

@Override
public Future<Message> sendFile(final InputStream inputStream, final String filename, final String comment, FutureCallback<Message> callback) {
    final MessageReceiver receiver = this;
    ListenableFuture<Message> future = api.getThreadPool().getListeningExecutorService().submit(new Callable<Message>() {

        @Override
        public Message call() throws Exception {
            logger.debug("Trying to send an input stream to user {} (comment: {})", ImplUser.this, comment);
            api.checkRateLimit(null, RateLimitType.PRIVATE_MESSAGE, null, null);
            MultipartBody body = Unirest.post("https://discordapp.com/api/v6/channels/" + getUserChannelIdBlocking() + "/messages").header("authorization", api.getToken()).field("file", inputStream, filename);
            if (comment != null) {
                body.field("content", comment);
            }
            HttpResponse<JsonNode> response = body.asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.PRIVATE_MESSAGE, null, null);
            logger.debug("Sent an input stream to user {} (comment: {})", ImplUser.this, comment);
            return new ImplMessage(response.getBody().getObject(), api, receiver);
        }
    });
    if (callback != null) {
        Futures.addCallback(future, callback);
    }
    return future;
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) MessageReceiver(de.btobastian.javacord.entities.message.MessageReceiver) MultipartBody(com.mashape.unirest.request.body.MultipartBody) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) HttpResponse(com.mashape.unirest.http.HttpResponse) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Aggregations

Message (de.btobastian.javacord.entities.message.Message)14 ImplMessage (de.btobastian.javacord.entities.message.impl.ImplMessage)12 List (java.util.List)7 HttpResponse (com.mashape.unirest.http.HttpResponse)6 MessageReceiver (de.btobastian.javacord.entities.message.MessageReceiver)6 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)5 MultipartBody (com.mashape.unirest.request.body.MultipartBody)4 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)3 Reaction (de.btobastian.javacord.entities.message.Reaction)3 MalformedURLException (java.net.MalformedURLException)3 CustomEmoji (de.btobastian.javacord.entities.CustomEmoji)2 User (de.btobastian.javacord.entities.User)2 MessageDeleteListener (de.btobastian.javacord.listener.message.MessageDeleteListener)2 JSONArray (org.json.JSONArray)2 JsonNode (com.mashape.unirest.http.JsonNode)1 MessageCreateListener (de.btobastian.javacord.listener.message.MessageCreateListener)1 MessageEditListener (de.btobastian.javacord.listener.message.MessageEditListener)1 ReactionAddListener (de.btobastian.javacord.listener.message.ReactionAddListener)1 ReactionRemoveAllListener (de.btobastian.javacord.listener.message.ReactionRemoveAllListener)1