improvements to sharded client
This commit is contained in:
		@@ -5,6 +5,7 @@ using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using NLog;
 | 
			
		||||
using System.Diagnostics;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot
 | 
			
		||||
{
 | 
			
		||||
@@ -28,6 +29,9 @@ namespace NadekoBot
 | 
			
		||||
        public event Action<IChannel, IChannel> ChannelUpdated = delegate { };
 | 
			
		||||
        public event Action<Exception> Disconnected = delegate { };
 | 
			
		||||
 | 
			
		||||
        private uint _connectedCount = 0;
 | 
			
		||||
        private uint _downloadedCount = 0;
 | 
			
		||||
 | 
			
		||||
        private IReadOnlyList<DiscordSocketClient> Clients { get; }
 | 
			
		||||
 | 
			
		||||
        public ShardedDiscordClient(DiscordSocketConfig discordSocketConfig)
 | 
			
		||||
@@ -87,14 +91,15 @@ namespace NadekoBot
 | 
			
		||||
        internal Task LoginAsync(TokenType tokenType, string token) =>
 | 
			
		||||
            Task.WhenAll(Clients.Select(async c => { await c.LoginAsync(tokenType, token).ConfigureAwait(false); _log.Info($"Shard #{c.ShardId} logged in."); }));
 | 
			
		||||
 | 
			
		||||
        internal async Task ConnectAsync()
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var c in Clients)
 | 
			
		||||
        internal Task ConnectAsync() =>
 | 
			
		||||
            Task.WhenAll(Clients.Select(async c =>
 | 
			
		||||
            {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    var sw = Stopwatch.StartNew();
 | 
			
		||||
                    await c.ConnectAsync().ConfigureAwait(false);
 | 
			
		||||
                    _log.Info($"Shard #{c.ShardId} connected.");
 | 
			
		||||
                    sw.Stop();
 | 
			
		||||
                    _log.Info($"Shard #{c.ShardId} connected after {sw.Elapsed.TotalSeconds}s ({++_connectedCount}/{Clients.Count})");
 | 
			
		||||
                }
 | 
			
		||||
                catch
 | 
			
		||||
                {
 | 
			
		||||
@@ -106,11 +111,16 @@ namespace NadekoBot
 | 
			
		||||
                        _log.Error(ex2);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
            }));
 | 
			
		||||
 | 
			
		||||
        internal Task DownloadAllUsersAsync() =>
 | 
			
		||||
            Task.WhenAll(Clients.Select(async c => { await c.DownloadAllUsersAsync().ConfigureAwait(false); _log.Info($"Shard #{c.ShardId} downloaded {c.GetGuilds().Sum(g => g.GetUsers().Count)} users."); }));
 | 
			
		||||
            Task.WhenAll(Clients.Select(async c =>
 | 
			
		||||
            {
 | 
			
		||||
                var sw = Stopwatch.StartNew();
 | 
			
		||||
                await c.DownloadAllUsersAsync().ConfigureAwait(false);
 | 
			
		||||
                sw.Stop();
 | 
			
		||||
                _log.Info($"Shard #{c.ShardId} downloaded {c.GetGuilds().Sum(g => g.GetUsers().Count)} users after {sw.Elapsed.TotalSeconds}s ({++_downloadedCount}/{Clients.Count}).");
 | 
			
		||||
            }));
 | 
			
		||||
 | 
			
		||||
        public async Task SetGame(string game)
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user