use of org.javacord.api.entity.channel.ServerChannel in project Javacord by BtoBastian.
the class ChannelPinsUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
long channelId = packet.get("channel_id").asLong();
Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
if (optionalChannel.isPresent()) {
TextChannel channel = optionalChannel.get();
Instant lastPinTimestamp = packet.hasNonNull("last_pin_timestamp") ? OffsetDateTime.parse(packet.get("last_pin_timestamp").asText()).toInstant() : null;
ChannelPinsUpdateEvent event = new ChannelPinsUpdateEventImpl(channel, lastPinTimestamp);
Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
api.getEventDispatcher().dispatchChannelPinsUpdateEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), optionalServer.orElse(null), channel, event);
} else {
LoggerUtil.logMissingChannel(logger, channelId);
}
}
use of org.javacord.api.entity.channel.ServerChannel in project Javacord by BtoBastian.
the class MessageUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
long messageId = packet.get("id").asLong();
long channelId = packet.get("channel_id").asLong();
Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
if (!optionalChannel.isPresent()) {
LoggerUtil.logMissingChannel(logger, channelId);
return;
}
TextChannel channel = optionalChannel.get();
Optional<MessageImpl> message = api.getCachedMessageById(messageId).map(msg -> (MessageImpl) msg);
message.ifPresent(msg -> {
boolean newPinnedFlag = packet.hasNonNull("pinned") ? packet.get("pinned").asBoolean() : msg.isPinned();
boolean oldPinnedFlag = msg.isPinned();
if (newPinnedFlag != oldPinnedFlag) {
msg.setPinned(newPinnedFlag);
if (newPinnedFlag) {
CachedMessagePinEvent event = new CachedMessagePinEventImpl(msg);
Optional<Server> optionalServer = msg.getChannel().asServerChannel().map(ServerChannel::getServer);
api.getEventDispatcher().dispatchCachedMessagePinEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), msg, optionalServer.orElse(null), msg.getChannel(), event);
} else {
CachedMessageUnpinEvent event = new CachedMessageUnpinEventImpl(msg);
Optional<Server> optionalServer = msg.getChannel().asServerChannel().map(ServerChannel::getServer);
api.getEventDispatcher().dispatchCachedMessageUnpinEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), msg, optionalServer.orElse(null), msg.getChannel(), event);
}
}
});
MessageEditEvent editEvent = null;
if (packet.has("edited_timestamp") && !packet.get("edited_timestamp").isNull()) {
message.ifPresent(msg -> {
msg.setLastEditTime(OffsetDateTime.parse(packet.get("edited_timestamp").asText()).toInstant());
msg.setMentionsEveryone(packet.get("mention_everyone").asBoolean());
});
long editTimestamp = OffsetDateTime.parse(packet.get("edited_timestamp").asText()).toInstant().toEpochMilli();
long lastKnownEditTimestamp = lastKnownEditTimestamps.getOrDefault(messageId, 0L);
lastKnownEditTimestamps.put(messageId, editTimestamp);
boolean isMostLikelyAnEdit = true;
long offset = api.getTimeOffset() == null ? 0 : api.getTimeOffset();
if (editTimestamp == lastKnownEditTimestamp) {
isMostLikelyAnEdit = false;
} else if (System.currentTimeMillis() + offset - editTimestamp > 5000) {
isMostLikelyAnEdit = false;
}
String oldContent = message.map(Message::getContent).orElse(null);
List<Embed> oldEmbeds = message.map(Message::getEmbeds).orElse(null);
String newContent = null;
if (packet.has("content")) {
newContent = packet.get("content").asText();
String finalNewContent = newContent;
message.ifPresent(msg -> msg.setContent(finalNewContent));
}
List<Embed> newEmbeds = null;
if (packet.has("embeds")) {
newEmbeds = new ArrayList<>();
for (JsonNode embedJson : packet.get("embeds")) {
Embed embed = new EmbedImpl(embedJson);
newEmbeds.add(embed);
}
List<Embed> finalNewEmbeds = newEmbeds;
message.ifPresent(msg -> msg.setEmbeds(finalNewEmbeds));
}
if (oldContent != null && newContent != null && !oldContent.equals(newContent)) {
// If the old content doesn't match the new content it's for sure an edit
isMostLikelyAnEdit = true;
}
if (oldEmbeds != null && newEmbeds != null) {
if (newEmbeds.size() != oldEmbeds.size()) {
isMostLikelyAnEdit = true;
} else {
for (int i = 0; i < newEmbeds.size(); i++) {
if (!((EmbedBuilderDelegateImpl) newEmbeds.get(i).toBuilder().getDelegate()).toJsonNode().toString().equals(((EmbedBuilderDelegateImpl) oldEmbeds.get(i).toBuilder().getDelegate()).toJsonNode().toString())) {
isMostLikelyAnEdit = true;
}
}
}
}
if (isMostLikelyAnEdit) {
editEvent = new MessageEditEventImpl(api, messageId, channel, newContent, newEmbeds, oldContent, oldEmbeds);
}
}
if (editEvent != null) {
dispatchEditEvent(editEvent);
}
}
use of org.javacord.api.entity.channel.ServerChannel in project Javacord by BtoBastian.
the class MessageDeleteHandler method handle.
@Override
public void handle(JsonNode packet) {
long messageId = packet.get("id").asLong();
long channelId = packet.get("channel_id").asLong();
Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
if (optionalChannel.isPresent()) {
TextChannel channel = optionalChannel.get();
MessageDeleteEvent event = new MessageDeleteEventImpl(api, messageId, channel);
api.getCachedMessageById(messageId).ifPresent(((MessageCacheImpl) channel.getMessageCache())::removeMessage);
api.removeMessageFromCache(messageId);
Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
api.getEventDispatcher().dispatchMessageDeleteEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, event);
api.removeObjectListeners(Message.class, messageId);
} else {
LoggerUtil.logMissingChannel(logger, channelId);
}
}
use of org.javacord.api.entity.channel.ServerChannel in project Javacord by BtoBastian.
the class ServerImpl method getChannels.
@Override
public List<ServerChannel> getChannels() {
final List<ServerChannel> channels = getUnorderedChannels().stream().filter(channel -> channel.asCategorizable().map(categorizable -> !categorizable.getCategory().isPresent()).orElse(false)).map(Channel::asRegularServerChannel).filter(Optional::isPresent).map(Optional::get).sorted(Comparator.<RegularServerChannel>comparingInt(channel -> channel.getType().getId()).thenComparing(RegularServerChannelImpl.COMPARE_BY_RAW_POSITION)).collect(Collectors.toList());
getChannelCategories().forEach(category -> {
channels.add(category);
channels.addAll(category.getChannels());
});
final Map<ServerTextChannel, List<ServerThreadChannel>> serverTextChannelThreads = new HashMap<>();
getThreadChannels().forEach(serverThreadChannel -> {
final ServerTextChannel serverTextChannel = serverThreadChannel.getParent();
serverTextChannelThreads.merge(serverTextChannel, new ArrayList<>(Collections.singletonList(serverThreadChannel)), (serverThreadChannels, serverThreadChannels2) -> {
serverThreadChannels.addAll(serverThreadChannels2);
return new ArrayList<>(serverThreadChannels);
});
});
serverTextChannelThreads.forEach((serverTextChannel, serverThreadChannels) -> channels.addAll(channels.indexOf(serverTextChannel) + 1, serverThreadChannels));
return Collections.unmodifiableList(channels);
}
Aggregations