Search in sources :

Example 11 with Message

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

the class ImplMessageHistory method request.

/**
 * Requests messages.
 *
 * @param api The used api.
 * @param channelId The id of the channel.
 * @param messageId Gets the messages before or after the message with the given id.
 * @param before Whether it should get the messages before or after the given message.
 * @param limit The maximum number of messages.
 * @return The amount of requested messages.
 * @throws Exception if something went wrong.
 */
private int request(ImplDiscordAPI api, String channelId, String messageId, boolean before, int limit) throws Exception {
    if (limit <= 0) {
        return 0;
    }
    logger.debug("Requesting part of message history (channel id: {}, message id: {}, before: {}, limit: {}", channelId, messageId == null ? "none" : messageId, before, limit);
    String link = messageId == null ? "https://discordapp.com/api/v6/channels/" + channelId + "/messages?&limit=" + limit : "https://discordapp.com/api/v6/channels/" + channelId + "/messages?&" + (before ? "before" : "after") + "=" + messageId + "&limit=" + limit;
    HttpResponse<JsonNode> response = Unirest.get(link).header("authorization", api.getToken()).asJson();
    api.checkResponse(response);
    api.checkRateLimit(response, RateLimitType.UNKNOWN, null, null);
    JSONArray messages = response.getBody().getArray();
    for (int i = 0; i < messages.length(); i++) {
        JSONObject messageJson = messages.getJSONObject(i);
        String id = messageJson.getString("id");
        Message message = api.getMessageById(id);
        if (message == null) {
            message = new ImplMessage(messageJson, api, null);
        }
        if (newestMessage == null || message.compareTo(newestMessage) > 0) {
            newestMessage = message;
        }
        if (oldestMessage == null || message.compareTo(oldestMessage) < 0) {
            oldestMessage = message;
        }
        this.messages.put(id, message);
    }
    return messages.length();
}
Also used : JSONObject(org.json.JSONObject) Message(de.btobastian.javacord.entities.message.Message) JSONArray(org.json.JSONArray) JsonNode(com.mashape.unirest.http.JsonNode)

Example 12 with Message

use of de.btobastian.javacord.entities.message.Message 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)

Example 13 with Message

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

the class MessageReactionRemoveAllHandler method handle.

@Override
public void handle(JSONObject packet) {
    // {"message_id":"269166028959776768","channel_id":"81402706320699392"}
    String messageId = packet.getString("message_id");
    final Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    final List<Reaction> reactions = message.getReactions();
    ((ImplMessage) message).removeAllReactionsFromCache();
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<ReactionRemoveAllListener> listeners = api.getListeners(ReactionRemoveAllListener.class);
            synchronized (listeners) {
                for (ReactionRemoveAllListener listener : listeners) {
                    try {
                        listener.onReactionRemoveAll(api, message, reactions);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in ReactionRemoveAllListener!", t);
                    }
                }
            }
        }
    });
}
Also used : ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) List(java.util.List) ReactionRemoveAllListener(de.btobastian.javacord.listener.message.ReactionRemoveAllListener) Reaction(de.btobastian.javacord.entities.message.Reaction)

Example 14 with Message

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

the class MessageUpdateHandler method handle.

@Override
public void handle(JSONObject packet) {
    String messageId = packet.getString("id");
    final Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    final String oldContent = message.getContent();
    if (!packet.has("content")) {
        return;
    }
    ((ImplMessage) message).setContent(packet.getString("content"));
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<MessageEditListener> listeners = api.getListeners(MessageEditListener.class);
            synchronized (listeners) {
                for (MessageEditListener listener : listeners) {
                    try {
                        listener.onMessageEdit(api, message, oldContent);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in MessageEditListener!", t);
                    }
                }
            }
        }
    });
}
Also used : MessageEditListener(de.btobastian.javacord.listener.message.MessageEditListener) Message(de.btobastian.javacord.entities.message.Message) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) List(java.util.List)

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