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();
}
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;
}
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);
}
}
}
}
});
}
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);
}
}
}
}
});
}
Aggregations