Search in sources :

Example 6 with ServerTextChannel

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);
    }
}
Also used : ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) WebhooksUpdateEvent(org.javacord.api.event.channel.server.text.WebhooksUpdateEvent) WebhooksUpdateEventImpl(org.javacord.core.event.channel.server.text.WebhooksUpdateEventImpl)

Example 7 with ServerTextChannel

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);
        }
    });
}
Also used : Color(java.awt.Color) RoleChangePositionEventImpl(org.javacord.core.event.server.role.RoleChangePositionEventImpl) RoleChangePositionEvent(org.javacord.api.event.server.role.RoleChangePositionEvent) RoleChangeColorEvent(org.javacord.api.event.server.role.RoleChangeColorEvent) RoleChangeColorEventImpl(org.javacord.core.event.server.role.RoleChangeColorEventImpl) Permissions(org.javacord.api.entity.permission.Permissions) RoleImpl(org.javacord.core.entity.permission.RoleImpl) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) RoleChangeMentionableEventImpl(org.javacord.core.event.server.role.RoleChangeMentionableEventImpl) RoleChangePermissionsEvent(org.javacord.api.event.server.role.RoleChangePermissionsEvent) RoleChangeNameEventImpl(org.javacord.core.event.server.role.RoleChangeNameEventImpl) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) JsonNode(com.fasterxml.jackson.databind.JsonNode) RoleChangePermissionsEventImpl(org.javacord.core.event.server.role.RoleChangePermissionsEventImpl) RoleChangeHoistEventImpl(org.javacord.core.event.server.role.RoleChangeHoistEventImpl) PermissionsImpl(org.javacord.core.entity.permission.PermissionsImpl) Predicate(java.util.function.Predicate) Set(java.util.Set) Collectors(java.util.stream.Collectors) User(org.javacord.api.entity.user.User) RoleChangeMentionableEvent(org.javacord.api.event.server.role.RoleChangeMentionableEvent) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) RoleChangeNameEvent(org.javacord.api.event.server.role.RoleChangeNameEvent) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) RoleChangeHoistEvent(org.javacord.api.event.server.role.RoleChangeHoistEvent) User(org.javacord.api.entity.user.User) RoleChangeMentionableEvent(org.javacord.api.event.server.role.RoleChangeMentionableEvent) RoleChangeHoistEventImpl(org.javacord.core.event.server.role.RoleChangeHoistEventImpl) RoleChangePositionEvent(org.javacord.api.event.server.role.RoleChangePositionEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) RoleChangeHoistEvent(org.javacord.api.event.server.role.RoleChangeHoistEvent) RoleChangeNameEvent(org.javacord.api.event.server.role.RoleChangeNameEvent) PermissionsImpl(org.javacord.core.entity.permission.PermissionsImpl) Permissions(org.javacord.api.entity.permission.Permissions) RoleChangeColorEvent(org.javacord.api.event.server.role.RoleChangeColorEvent) RoleChangeMentionableEventImpl(org.javacord.core.event.server.role.RoleChangeMentionableEventImpl) RoleChangeNameEventImpl(org.javacord.core.event.server.role.RoleChangeNameEventImpl) RoleChangePositionEventImpl(org.javacord.core.event.server.role.RoleChangePositionEventImpl) RoleImpl(org.javacord.core.entity.permission.RoleImpl) Color(java.awt.Color) RoleChangePermissionsEventImpl(org.javacord.core.event.server.role.RoleChangePermissionsEventImpl) RoleChangeColorEventImpl(org.javacord.core.event.server.role.RoleChangeColorEventImpl) RoleChangePermissionsEvent(org.javacord.api.event.server.role.RoleChangePermissionsEvent) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl)

Example 8 with ServerTextChannel

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);
    }
}
Also used : ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) TextChannel(org.javacord.api.entity.channel.TextChannel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) UserImpl(org.javacord.core.entity.user.UserImpl) PrivateChannelImpl(org.javacord.core.entity.channel.PrivateChannelImpl)

Example 9 with ServerTextChannel

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);
}
Also used : AudioConnection(org.javacord.api.audio.AudioConnection) RegularServerChannelImpl(org.javacord.core.entity.channel.RegularServerChannelImpl) ServerChannel(org.javacord.api.entity.channel.ServerChannel) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl) IconImpl(org.javacord.core.entity.IconImpl) Member(org.javacord.core.entity.user.Member) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) URL(java.net.URL) AuditLogImpl(org.javacord.core.entity.auditlog.AuditLogImpl) RestMethod(org.javacord.core.util.rest.RestMethod) ServerStageVoiceChannelImpl(org.javacord.core.entity.channel.ServerStageVoiceChannelImpl) StickerImpl(org.javacord.core.entity.sticker.StickerImpl) Ban(org.javacord.api.entity.server.Ban) BoostLevel(org.javacord.api.entity.server.BoostLevel) RoleImpl(org.javacord.core.entity.permission.RoleImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerTextChannelImpl(org.javacord.core.entity.channel.ServerTextChannelImpl) Locale(java.util.Locale) Map(java.util.Map) RestRequest(org.javacord.core.util.rest.RestRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) VanityUrlCodeImpl(org.javacord.core.entity.VanityUrlCodeImpl) AuditLog(org.javacord.api.entity.auditlog.AuditLog) Webhook(org.javacord.api.entity.webhook.Webhook) NsfwLevel(org.javacord.api.entity.server.NsfwLevel) Activity(org.javacord.api.entity.activity.Activity) InternalServerAttachableListenerManager(org.javacord.core.listener.server.InternalServerAttachableListenerManager) InviteImpl(org.javacord.core.entity.server.invite.InviteImpl) UserImpl(org.javacord.core.entity.user.UserImpl) Collection(java.util.Collection) ChannelCategoryImpl(org.javacord.core.entity.channel.ChannelCategoryImpl) WebhookImpl(org.javacord.core.entity.webhook.WebhookImpl) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SlashCommand(org.javacord.api.interaction.SlashCommand) MultiFactorAuthenticationLevel(org.javacord.api.entity.server.MultiFactorAuthenticationLevel) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) VanityUrlCode(org.javacord.api.entity.VanityUrlCode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) Objects(java.util.Objects) ExplicitContentFilterLevel(org.javacord.api.entity.server.ExplicitContentFilterLevel) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) Sticker(org.javacord.api.entity.sticker.Sticker) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AudioConnectionImpl(org.javacord.core.audio.AudioConnectionImpl) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Role(org.javacord.api.entity.permission.Role) Optional(java.util.Optional) IncomingWebhookImpl(org.javacord.core.entity.webhook.IncomingWebhookImpl) AuditLogActionType(org.javacord.api.entity.auditlog.AuditLogActionType) ActiveThreads(org.javacord.api.entity.server.ActiveThreads) RichInvite(org.javacord.api.entity.server.invite.RichInvite) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) ChannelType(org.javacord.api.entity.channel.ChannelType) KnownCustomEmoji(org.javacord.api.entity.emoji.KnownCustomEmoji) ActivityImpl(org.javacord.core.entity.activity.ActivityImpl) ServerThreadChannelImpl(org.javacord.core.entity.channel.ServerThreadChannelImpl) Channel(org.javacord.api.entity.channel.Channel) DiscordClient(org.javacord.api.entity.DiscordClient) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscordEntity(org.javacord.api.entity.DiscordEntity) Region(org.javacord.api.entity.Region) Icon(org.javacord.api.entity.Icon) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Cleanupable(org.javacord.core.util.Cleanupable) DefaultMessageNotificationLevel(org.javacord.api.entity.server.DefaultMessageNotificationLevel) ReentrantLock(java.util.concurrent.locks.ReentrantLock) MalformedURLException(java.net.MalformedURLException) MemberImpl(org.javacord.core.entity.user.MemberImpl) DiscordApiImpl(org.javacord.core.DiscordApiImpl) AuditLogEntry(org.javacord.api.entity.auditlog.AuditLogEntry) Javacord(org.javacord.api.Javacord) Consumer(java.util.function.Consumer) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) ServerFeature(org.javacord.api.entity.server.ServerFeature) ChannelCategory(org.javacord.api.entity.channel.ChannelCategory) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ServerStageVoiceChannel(org.javacord.api.entity.channel.ServerStageVoiceChannel) Intent(org.javacord.api.entity.intent.Intent) User(org.javacord.api.entity.user.User) RestRequestResult(org.javacord.core.util.rest.RestRequestResult) VerificationLevel(org.javacord.api.entity.server.VerificationLevel) UserStatus(org.javacord.api.entity.user.UserStatus) DiscordApi(org.javacord.api.DiscordApi) Server(org.javacord.api.entity.server.Server) Comparator(java.util.Comparator) Collections(java.util.Collections) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) Optional(java.util.Optional) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ServerChannel(org.javacord.api.entity.channel.ServerChannel) ServerThreadChannel(org.javacord.api.entity.channel.ServerThreadChannel) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) Channel(org.javacord.api.entity.channel.Channel) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) ServerStageVoiceChannel(org.javacord.api.entity.channel.ServerStageVoiceChannel) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ServerChannel(org.javacord.api.entity.channel.ServerChannel) RegularServerChannel(org.javacord.api.entity.channel.RegularServerChannel)

Aggregations

ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)9 DiscordApi (org.javacord.api.DiscordApi)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 User (org.javacord.api.entity.user.User)4 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Objects (java.util.Objects)3 Predicate (java.util.function.Predicate)3 Collectors (java.util.stream.Collectors)3 ServerThreadChannel (org.javacord.api.entity.channel.ServerThreadChannel)3 Role (org.javacord.api.entity.permission.Role)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Instant (java.time.Instant)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Locale (java.util.Locale)2 Optional (java.util.Optional)2