Search in sources :

Example 6 with MessageReceiver

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

the class ImplChannel 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 in channel {} (content: \"{}\", tts: {})", ImplChannel.this, content, tts);
            api.checkRateLimit(null, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            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/" + id + "/messages").header("authorization", api.getToken()).header("content-type", "application/json").body(body.toString()).asJson();
            api.checkResponse(response);
            api.checkRateLimit(response, RateLimitType.SERVER_MESSAGE, null, ImplChannel.this);
            logger.debug("Sent message in channel {} (content: \"{}\", tts: {})", ImplChannel.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) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JSONException(org.json.JSONException)

Aggregations

HttpResponse (com.mashape.unirest.http.HttpResponse)6 Message (de.btobastian.javacord.entities.message.Message)6 MessageReceiver (de.btobastian.javacord.entities.message.MessageReceiver)6 ImplMessage (de.btobastian.javacord.entities.message.impl.ImplMessage)6 JSONException (org.json.JSONException)6 MultipartBody (com.mashape.unirest.request.body.MultipartBody)4 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)3 MalformedURLException (java.net.MalformedURLException)3 JSONObject (org.json.JSONObject)2