use of de.btobastian.javacord.entities.message.Message in project Javacord by BtoBastian.
the class MessageBulkDeleteHandler method handle.
@Override
public void handle(JSONObject packet) {
JSONArray messageIds = packet.getJSONArray("ids");
for (int i = 0; i < messageIds.length(); i++) {
String messageId = messageIds.getString(i);
final Message message = api.getMessageById(messageId);
if (message == null) {
// no cached version available
return;
}
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);
}
}
}
}
});
}
}
use of de.btobastian.javacord.entities.message.Message 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);
}
}
}
}
});
}
use of de.btobastian.javacord.entities.message.Message 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);
}
}
}
}
});
}
use of de.btobastian.javacord.entities.message.Message 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);
}
}
}
}
});
}
}
}
use of de.btobastian.javacord.entities.message.Message 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);
}
}
}
}
});
}
}
}
Aggregations