use of org.javacord.api.entity.channel.ServerTextChannel in project Javacord by BtoBastian.
the class WebhooksUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
long channelId = packet.get("channel_id").asLong();
Optional<ServerTextChannel> optionalChannel = api.getServerTextChannelById(channelId);
if (optionalChannel.isPresent()) {
ServerTextChannel channel = optionalChannel.get();
WebhooksUpdateEvent event = new WebhooksUpdateEventImpl(channel);
api.getEventDispatcher().dispatchWebhooksUpdateEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
} else {
LoggerUtil.logMissingChannel(logger, channelId);
}
}
use of org.javacord.api.entity.channel.ServerTextChannel in project Javacord by BtoBastian.
the class GuildRoleUpdateHandler method handle.
@Override
public void handle(JsonNode packet) {
JsonNode roleJson = packet.get("role");
long roleId = roleJson.get("id").asLong();
api.getRoleById(roleId).map(role -> (RoleImpl) role).ifPresent(role -> {
Color oldColorObject = role.getColor().orElse(null);
int oldColor = role.getColorAsInt();
int newColor = roleJson.get("color").asInt(0);
if (oldColor != newColor) {
role.setColor(newColor);
RoleChangeColorEvent event = new RoleChangeColorEventImpl(role, role.getColor().orElse(null), oldColorObject);
api.getEventDispatcher().dispatchRoleChangeColorEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
boolean oldHoist = role.isDisplayedSeparately();
boolean newHoist = roleJson.get("hoist").asBoolean(false);
if (oldHoist != newHoist) {
role.setHoist(newHoist);
RoleChangeHoistEvent event = new RoleChangeHoistEventImpl(role, oldHoist);
api.getEventDispatcher().dispatchRoleChangeHoistEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
boolean oldMentionable = role.isMentionable();
boolean newMentionable = roleJson.get("mentionable").asBoolean(false);
if (oldMentionable != newMentionable) {
role.setMentionable(newMentionable);
RoleChangeMentionableEvent event = new RoleChangeMentionableEventImpl(role, oldMentionable);
api.getEventDispatcher().dispatchRoleChangeMentionableEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
String oldName = role.getName();
String newName = roleJson.get("name").asText();
if (!oldName.equals(newName)) {
role.setName(newName);
RoleChangeNameEvent event = new RoleChangeNameEventImpl(role, newName, oldName);
api.getEventDispatcher().dispatchRoleChangeNameEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
Permissions oldPermissions = role.getPermissions();
PermissionsImpl newPermissions = new PermissionsImpl(roleJson.get("permissions").asLong(), 0);
if (!oldPermissions.equals(newPermissions)) {
role.setPermissions(newPermissions);
RoleChangePermissionsEvent event = new RoleChangePermissionsEventImpl(role, newPermissions, oldPermissions);
api.getEventDispatcher().dispatchRoleChangePermissionsEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
// If bot is affected remove messages from cache that are no longer visible
if (role.getUsers().stream().anyMatch(User::isYourself)) {
Set<Long> unreadableChannels = role.getServer().getTextChannels().stream().filter(((Predicate<ServerTextChannel>) ServerTextChannel::canYouSee).negate()).map(ServerTextChannel::getId).collect(Collectors.toSet());
api.forEachCachedMessageWhere(msg -> unreadableChannels.contains(msg.getChannel().getId()), msg -> {
api.removeMessageFromCache(msg.getId());
((MessageCacheImpl) msg.getChannel().getMessageCache()).removeMessage(msg);
});
}
}
int oldPosition = role.getPosition();
int oldRawPosition = role.getRawPosition();
int newRawPosition = roleJson.get("position").asInt();
if (oldRawPosition != newRawPosition) {
role.setRawPosition(newRawPosition);
int newPosition = role.getPosition();
RoleChangePositionEvent event = new RoleChangePositionEventImpl(role, newRawPosition, oldRawPosition, newPosition, oldPosition);
api.getEventDispatcher().dispatchRoleChangePositionEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), event);
}
});
}
use of org.javacord.api.entity.channel.ServerTextChannel in project Javacord by BtoBastian.
the class MessageCreateHandler method handle.
@Override
public void handle(JsonNode packet) {
long channelId = packet.get("channel_id").asLong();
// See https://github.com/discord/discord-api-docs/issues/2248
if (!packet.hasNonNull("guild_id")) {
// Check for EPHEMERAL messages as they do NOT include a guild_id when the EPHEMERAL flag is set.
if (packet.hasNonNull("flags") && (packet.get("flags").asInt() & MessageFlag.EPHEMERAL.getId()) > 0) {
Optional<ServerTextChannel> serverTextChannel = api.getServerTextChannelById(channelId);
if (serverTextChannel.isPresent()) {
handle(serverTextChannel.get(), packet);
return;
}
Optional<ServerThreadChannel> serverThreadChannel = api.getServerThreadChannelById(channelId);
if (serverThreadChannel.isPresent()) {
handle(serverThreadChannel.get(), packet);
return;
}
}
UserImpl author = new UserImpl(api, packet.get("author"), (MemberImpl) null, null);
PrivateChannelImpl privateChannel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, author.getId(), author);
handle(privateChannel, packet);
return;
}
Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
if (optionalChannel.isPresent()) {
handle(optionalChannel.get(), packet);
} else {
LoggerUtil.logMissingChannel(logger, channelId);
}
}
use of org.javacord.api.entity.channel.ServerTextChannel 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