Search in sources :

Example 1 with ImplMessage

use of de.btobastian.javacord.entities.message.impl.ImplMessage 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 2 with ImplMessage

use of de.btobastian.javacord.entities.message.impl.ImplMessage 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 3 with ImplMessage

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

the class MessageCreateHandler method handle.

@Override
public void handle(JSONObject packet) {
    String messageId = packet.getString("id");
    Message messageTemp = api.getMessageById(messageId);
    if (messageTemp == null) {
        messageTemp = new ImplMessage(packet, api, null);
    }
    final Message message = messageTemp;
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<MessageCreateListener> listeners = api.getListeners(MessageCreateListener.class);
            synchronized (listeners) {
                for (MessageCreateListener listener : listeners) {
                    try {
                        listener.onMessageCreate(api, message);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in MessageCreateListener!", t);
                    }
                }
            }
        }
    });
}
Also used : 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) MessageCreateListener(de.btobastian.javacord.listener.message.MessageCreateListener)

Example 4 with ImplMessage

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

the class MessageDeleteHandler method handle.

@Override
public void handle(JSONObject packet) {
    String messageId = packet.getString("id");
    final Message message = api.getMessageById(messageId);
    if (message == null) {
        // no cached version available
        return;
    }
    synchronized (message) {
        if (message.isDeleted()) {
            // already called listener
            return;
        } else {
            ((ImplMessage) message).setDeleted(true);
        }
    }
    listenerExecutorService.submit(new Runnable() {

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

Example 5 with ImplMessage

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

the class MessageReactionAddHandler method handle.

@Override
public void handle(JSONObject packet) {
    String userId = packet.getString("user_id");
    String messageId = packet.getString("message_id");
    JSONObject emoji = packet.getJSONObject("emoji");
    boolean isCustomEmoji = !emoji.isNull("id");
    Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    Reaction reaction = null;
    if (isCustomEmoji) {
        String emojiId = emoji.getString("id");
        if (message.isPrivateMessage()) {
            // Private messages with custom emoji? Maybe with Nitro, but there's no documentation so far.
            return;
        }
        CustomEmoji customEmoji = message.getChannelReceiver().getServer().getCustomEmojiById(emojiId);
        if (customEmoji == null) {
            // We don't know this emoji
            return;
        }
        reaction = ((ImplMessage) message).addCustomEmojiReactionToCache(customEmoji, api.getYourself().getId().equals(userId));
    } else {
        reaction = ((ImplMessage) message).addUnicodeReactionToCache(emoji.getString("name"), api.getYourself().getId().equals(userId));
    }
    if (reaction != null) {
        final User user = api.getCachedUserById(userId);
        if (user != null) {
            final Reaction reactionFinal = reaction;
            listenerExecutorService.submit(new Runnable() {

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

Aggregations

Message (de.btobastian.javacord.entities.message.Message)12 ImplMessage (de.btobastian.javacord.entities.message.impl.ImplMessage)12 HttpResponse (com.mashape.unirest.http.HttpResponse)6 MessageReceiver (de.btobastian.javacord.entities.message.MessageReceiver)6 List (java.util.List)6 JSONException (org.json.JSONException)6 MultipartBody (com.mashape.unirest.request.body.MultipartBody)4 JSONObject (org.json.JSONObject)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 MessageCreateListener (de.btobastian.javacord.listener.message.MessageCreateListener)1 MessageDeleteListener (de.btobastian.javacord.listener.message.MessageDeleteListener)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 ReactionRemoveListener (de.btobastian.javacord.listener.message.ReactionRemoveListener)1