Search in sources :

Example 1 with AuthorizationConfig

use of net.dv8tion.jda.internal.utils.config.AuthorizationConfig in project JDA by DV8FromTheWorld.

the class DefaultShardManager method buildInstance.

protected JDAImpl buildInstance(final int shardId) throws LoginException {
    OkHttpClient httpClient = sessionConfig.getHttpClient();
    if (httpClient == null) {
        // httpClient == null implies we have a builder
        // noinspection ConstantConditions
        httpClient = sessionConfig.getHttpBuilder().build();
    }
    // imagine if we had macros or closures or destructuring :)
    ExecutorPair<ScheduledExecutorService> rateLimitPair = resolveExecutor(threadingConfig.getRateLimitPoolProvider(), shardId);
    ScheduledExecutorService rateLimitPool = rateLimitPair.executor;
    boolean shutdownRateLimitPool = rateLimitPair.automaticShutdown;
    ExecutorPair<ScheduledExecutorService> gatewayPair = resolveExecutor(threadingConfig.getGatewayPoolProvider(), shardId);
    ScheduledExecutorService gatewayPool = gatewayPair.executor;
    boolean shutdownGatewayPool = gatewayPair.automaticShutdown;
    ExecutorPair<ExecutorService> callbackPair = resolveExecutor(threadingConfig.getCallbackPoolProvider(), shardId);
    ExecutorService callbackPool = callbackPair.executor;
    boolean shutdownCallbackPool = callbackPair.automaticShutdown;
    ExecutorPair<ExecutorService> eventPair = resolveExecutor(threadingConfig.getEventPoolProvider(), shardId);
    ExecutorService eventPool = eventPair.executor;
    boolean shutdownEventPool = eventPair.automaticShutdown;
    ExecutorPair<ScheduledExecutorService> audioPair = resolveExecutor(threadingConfig.getAudioPoolProvider(), shardId);
    ScheduledExecutorService audioPool = audioPair.executor;
    boolean shutdownAudioPool = audioPair.automaticShutdown;
    AuthorizationConfig authConfig = new AuthorizationConfig(token);
    SessionConfig sessionConfig = this.sessionConfig.toSessionConfig(httpClient);
    ThreadingConfig threadingConfig = new ThreadingConfig();
    threadingConfig.setRateLimitPool(rateLimitPool, shutdownRateLimitPool);
    threadingConfig.setGatewayPool(gatewayPool, shutdownGatewayPool);
    threadingConfig.setCallbackPool(callbackPool, shutdownCallbackPool);
    threadingConfig.setEventPool(eventPool, shutdownEventPool);
    threadingConfig.setAudioPool(audioPool, shutdownAudioPool);
    MetaConfig metaConfig = new MetaConfig(this.metaConfig.getMaxBufferSize(), this.metaConfig.getContextMap(shardId), this.metaConfig.getCacheFlags(), this.sessionConfig.getFlags());
    final JDAImpl jda = new JDAImpl(authConfig, sessionConfig, threadingConfig, metaConfig);
    jda.setMemberCachePolicy(shardingConfig.getMemberCachePolicy());
    threadingConfig.init(jda::getIdentifierString);
    // We can only do member chunking with the GUILD_MEMBERS intent
    if ((shardingConfig.getIntents() & GatewayIntent.GUILD_MEMBERS.getRawValue()) == 0)
        jda.setChunkingFilter(ChunkingFilter.NONE);
    else
        jda.setChunkingFilter(chunkingFilter);
    jda.setShardManager(this);
    if (eventConfig.getEventManagerProvider() != null)
        jda.setEventManager(this.eventConfig.getEventManagerProvider().apply(shardId));
    if (this.sessionConfig.getAudioSendFactory() != null)
        jda.setAudioSendFactory(this.sessionConfig.getAudioSendFactory());
    this.eventConfig.getListeners().forEach(jda::addEventListener);
    this.eventConfig.getListenerProviders().forEach(provider -> jda.addEventListener(provider.apply(shardId)));
    // Set the presence information before connecting to have the correct information ready when sending IDENTIFY
    PresenceImpl presence = ((PresenceImpl) jda.getPresence());
    if (presenceConfig.getActivityProvider() != null)
        presence.setCacheActivity(presenceConfig.getActivityProvider().apply(shardId));
    if (presenceConfig.getIdleProvider() != null)
        presence.setCacheIdle(presenceConfig.getIdleProvider().apply(shardId));
    if (presenceConfig.getStatusProvider() != null)
        presence.setCacheStatus(presenceConfig.getStatusProvider().apply(shardId));
    if (this.gatewayURL == null) {
        try {
            SessionController.ShardedGateway gateway = jda.getShardedGateway();
            this.sessionConfig.getSessionController().setConcurrency(gateway.getConcurrency());
            this.gatewayURL = gateway.getUrl();
            if (this.gatewayURL == null)
                LOG.error("Acquired null gateway url from SessionController");
            else
                LOG.info("Login Successful!");
            if (getShardsTotal() == -1) {
                shardingConfig.setShardsTotal(gateway.getShardTotal());
                this.shards = new ShardCacheViewImpl(getShardsTotal());
                synchronized (queue) {
                    for (int i = 0; i < getShardsTotal(); i++) queue.add(i);
                }
            }
        } catch (CompletionException e) {
            if (e.getCause() instanceof LoginException)
                // complete() can't throw this because its a checked-exception so we have to unwrap it first
                throw (LoginException) e.getCause();
            throw e;
        }
    }
    final JDA.ShardInfo shardInfo = new JDA.ShardInfo(shardId, getShardsTotal());
    // Initialize SelfUser instance before logging in
    SelfUser selfUser = getShardCache().applyStream(s -> // this should never throw!
    s.map(JDA::getSelfUser).findFirst().orElse(null));
    // Copy from other JDA instance or do initial fetch
    if (selfUser == null)
        selfUser = retrieveSelfUser(jda);
    else
        selfUser = SelfUserImpl.copyOf((SelfUserImpl) selfUser, jda);
    jda.setSelfUser(selfUser);
    // This is already set by JDA internally, but this is to make sure the listeners catch it.
    jda.setStatus(JDA.Status.INITIALIZED);
    final int shardTotal = jda.login(this.gatewayURL, shardInfo, this.metaConfig.getCompression(), false, shardingConfig.getIntents(), this.metaConfig.getEncoding());
    if (getShardsTotal() == -1)
        shardingConfig.setShardsTotal(shardTotal);
    return jda;
}
Also used : ShardCacheViewImpl(net.dv8tion.jda.internal.utils.cache.ShardCacheViewImpl) ThreadingConfig(net.dv8tion.jda.internal.utils.config.ThreadingConfig) OkHttpClient(okhttp3.OkHttpClient) PresenceImpl(net.dv8tion.jda.internal.managers.PresenceImpl) JDA(net.dv8tion.jda.api.JDA) MetaConfig(net.dv8tion.jda.internal.utils.config.MetaConfig) SessionConfig(net.dv8tion.jda.internal.utils.config.SessionConfig) JDAImpl(net.dv8tion.jda.internal.JDAImpl) AuthorizationConfig(net.dv8tion.jda.internal.utils.config.AuthorizationConfig) SessionController(net.dv8tion.jda.api.utils.SessionController) LoginException(javax.security.auth.login.LoginException) SelfUser(net.dv8tion.jda.api.entities.SelfUser)

Example 2 with AuthorizationConfig

use of net.dv8tion.jda.internal.utils.config.AuthorizationConfig in project JDA by DV8FromTheWorld.

the class JDABuilder method build.

/**
 * Builds a new {@link net.dv8tion.jda.api.JDA} instance and uses the provided token to start the login process.
 * <br>The login process runs in a different thread, so while this will return immediately, {@link net.dv8tion.jda.api.JDA} has not
 * finished loading, thus many {@link net.dv8tion.jda.api.JDA} methods have the chance to return incorrect information.
 * For example {@link JDA#getGuilds()} might return an empty list or {@link net.dv8tion.jda.api.JDA#getUserById(long)} might return null
 * for arbitrary user IDs.
 *
 * <p>If you wish to be sure that the {@link net.dv8tion.jda.api.JDA} information is correct, please use
 * {@link net.dv8tion.jda.api.JDA#awaitReady() JDA.awaitReady()} or register an
 * {@link net.dv8tion.jda.api.hooks.EventListener EventListener} to listen for the
 * {@link net.dv8tion.jda.api.events.ReadyEvent ReadyEvent}.
 *
 * @throws LoginException
 *         If the provided token is invalid.
 * @throws IllegalArgumentException
 *         If the provided token is empty or null. Or the provided intents/cache configuration is not possible.
 *
 * @return A {@link net.dv8tion.jda.api.JDA} instance that has started the login process. It is unknown as
 *         to whether or not loading has finished when this returns.
 *
 * @see    net.dv8tion.jda.api.JDA#awaitReady()
 */
@Nonnull
public JDA build() throws LoginException {
    checkIntents();
    OkHttpClient httpClient = this.httpClient;
    if (httpClient == null) {
        if (this.httpClientBuilder == null)
            this.httpClientBuilder = IOUtil.newHttpClientBuilder();
        httpClient = this.httpClientBuilder.build();
    }
    WebSocketFactory wsFactory = this.wsFactory == null ? new WebSocketFactory() : this.wsFactory;
    if (controller == null && shardInfo != null)
        controller = new ConcurrentSessionController();
    AuthorizationConfig authConfig = new AuthorizationConfig(token);
    ThreadingConfig threadingConfig = new ThreadingConfig();
    threadingConfig.setCallbackPool(callbackPool, shutdownCallbackPool);
    threadingConfig.setGatewayPool(mainWsPool, shutdownMainWsPool);
    threadingConfig.setRateLimitPool(rateLimitPool, shutdownRateLimitPool);
    threadingConfig.setEventPool(eventPool, shutdownEventPool);
    threadingConfig.setAudioPool(audioPool, shutdownAudioPool);
    SessionConfig sessionConfig = new SessionConfig(controller, httpClient, wsFactory, voiceDispatchInterceptor, flags, maxReconnectDelay, largeThreshold);
    MetaConfig metaConfig = new MetaConfig(maxBufferSize, contextMap, cacheFlags, flags);
    JDAImpl jda = new JDAImpl(authConfig, sessionConfig, threadingConfig, metaConfig);
    jda.setMemberCachePolicy(memberCachePolicy);
    // We can only do member chunking with the GUILD_MEMBERS intent
    if ((intents & GatewayIntent.GUILD_MEMBERS.getRawValue()) == 0)
        jda.setChunkingFilter(ChunkingFilter.NONE);
    else
        jda.setChunkingFilter(chunkingFilter);
    if (eventManager != null)
        jda.setEventManager(eventManager);
    if (audioSendFactory != null)
        jda.setAudioSendFactory(audioSendFactory);
    listeners.forEach(jda::addEventListener);
    // This is already set by JDA internally, but this is to make sure the listeners catch it.
    jda.setStatus(JDA.Status.INITIALIZED);
    // Set the presence information before connecting to have the correct information ready when sending IDENTIFY
    ((PresenceImpl) jda.getPresence()).setCacheActivity(activity).setCacheIdle(idle).setCacheStatus(status);
    jda.login(shardInfo, compression, true, intents, encoding);
    return jda;
}
Also used : ThreadingConfig(net.dv8tion.jda.internal.utils.config.ThreadingConfig) OkHttpClient(okhttp3.OkHttpClient) PresenceImpl(net.dv8tion.jda.internal.managers.PresenceImpl) AuthorizationConfig(net.dv8tion.jda.internal.utils.config.AuthorizationConfig) MetaConfig(net.dv8tion.jda.internal.utils.config.MetaConfig) WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) SessionConfig(net.dv8tion.jda.internal.utils.config.SessionConfig) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Nonnull(javax.annotation.Nonnull)

Aggregations

JDAImpl (net.dv8tion.jda.internal.JDAImpl)2 PresenceImpl (net.dv8tion.jda.internal.managers.PresenceImpl)2 AuthorizationConfig (net.dv8tion.jda.internal.utils.config.AuthorizationConfig)2 MetaConfig (net.dv8tion.jda.internal.utils.config.MetaConfig)2 SessionConfig (net.dv8tion.jda.internal.utils.config.SessionConfig)2 ThreadingConfig (net.dv8tion.jda.internal.utils.config.ThreadingConfig)2 OkHttpClient (okhttp3.OkHttpClient)2 WebSocketFactory (com.neovisionaries.ws.client.WebSocketFactory)1 Nonnull (javax.annotation.Nonnull)1 LoginException (javax.security.auth.login.LoginException)1 JDA (net.dv8tion.jda.api.JDA)1 SelfUser (net.dv8tion.jda.api.entities.SelfUser)1 SessionController (net.dv8tion.jda.api.utils.SessionController)1 ShardCacheViewImpl (net.dv8tion.jda.internal.utils.cache.ShardCacheViewImpl)1