Ejemplo n.º 1
0
        protected override async Task OnLoginAsync(TokenType tokenType, string token)
        {
            if (_automaticShards)
            {
                var response = await ApiClient.GetBotGatewayAsync().ConfigureAwait(false);

                _shardIds    = Enumerable.Range(0, response.Shards).ToArray();
                _totalShards = _shardIds.Length;
                _shards      = new DiscordSocketClient[_shardIds.Length];
                for (int i = 0; i < _shardIds.Length; i++)
                {
                    _shardIdsToIndex.Add(_shardIds[i], i);
                    var newConfig = _baseConfig.Clone();
                    newConfig.ShardId     = _shardIds[i];
                    newConfig.TotalShards = _totalShards;
                    _shards[i]            = new DiscordSocketClient(newConfig, _connectionGroupLock, i != 0 ? _shards[0] : null);
                    RegisterEvents(_shards[i]);
                }
            }

            //Assume threadsafe: already in a connection lock
            for (int i = 0; i < _shards.Length; i++)
            {
                await _shards[i].LoginAsync(tokenType, token, false);
            }
        }
Ejemplo n.º 2
0
        private DiscordShardedClient(int[] ids, DiscordSocketConfig config, API.DiscordSocketApiClient client)
            : base(config, client)
        {
            if (config.ShardId != null)
            {
                throw new ArgumentException($"{nameof(config.ShardId)} must not be set.");
            }
            if (ids != null && config.TotalShards == null)
            {
                throw new ArgumentException($"Custom ids are not supported when {nameof(config.TotalShards)} is not specified.");
            }

            _shardIdsToIndex         = new Dictionary <int, int>();
            config.DisplayInitialLog = false;
            _baseConfig          = config;
            _connectionGroupLock = new SemaphoreSlim(1, 1);

            if (config.TotalShards == null)
            {
                _automaticShards = true;
            }
            else
            {
                _totalShards = config.TotalShards.Value;
                _shardIds    = ids ?? Enumerable.Range(0, _totalShards).ToArray();
                _shards      = new DiscordSocketClient[_shardIds.Length];
                for (int i = 0; i < _shardIds.Length; i++)
                {
                    _shardIdsToIndex.Add(_shardIds[i], i);
                    var newConfig = config.Clone();
                    newConfig.ShardId = _shardIds[i];
                    _shards[i]        = new DiscordSocketClient(newConfig, _connectionGroupLock, i != 0 ? _shards[0] : null);
                    RegisterEvents(_shards[i]);
                }
            }
        }