use of de.btobastian.javacord.entities.message.impl.ImplMessage in project Javacord by BtoBastian.
the class MessageReactionRemoveHandler 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).removeCustomEmojiReactionToCache(customEmoji, api.getYourself().getId().equals(userId));
} else {
reaction = ((ImplMessage) message).removeUnicodeReactionToCache(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<ReactionRemoveListener> listeners = api.getListeners(ReactionRemoveListener.class);
synchronized (listeners) {
for (ReactionRemoveListener listener : listeners) {
try {
listener.onReactionRemove(api, reactionFinal, user);
} catch (Throwable t) {
logger.warn("Uncaught exception in ReactionRemoveListener!", t);
}
}
}
}
});
}
}
}
use of de.btobastian.javacord.entities.message.impl.ImplMessage 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;
}
use of de.btobastian.javacord.entities.message.impl.ImplMessage 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;
}
use of de.btobastian.javacord.entities.message.impl.ImplMessage 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;
}
use of de.btobastian.javacord.entities.message.impl.ImplMessage 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;
}
Aggregations