From 30bafa8a89ed719eb0f55e3b26ab648e02932f7d Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 15 Jul 2017 05:23:46 +0200 Subject: [PATCH] More cleanup --- src/NadekoBot/Modules/Utility/Utility.cs | 10 ++-- src/NadekoBot/NadekoBot.cs | 53 +++++++++----------- src/NadekoBot/Services/Impl/NadekoStrings.cs | 4 +- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index e1cf1226..b4c8b003 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -28,14 +28,14 @@ namespace NadekoBot.Modules.Utility private readonly DiscordSocketClient _client; private readonly IStatsService _stats; private readonly IBotCredentials _creds; - private readonly NadekoBot _bot; + private readonly ShardsCoordinator _shardCoord; - public Utility(NadekoBot bot, DiscordSocketClient client, IStatsService stats, IBotCredentials creds) + public Utility(ShardsCoordinator shardCoord, DiscordSocketClient client, IStatsService stats, IBotCredentials creds) { _client = client; _stats = stats; _creds = creds; - _bot = bot; + _shardCoord = shardCoord; } [NadekoCommand, Usage, Description, Aliases] @@ -286,7 +286,7 @@ namespace NadekoBot.Modules.Utility { if (--page < 0) return; - var statuses = _bot.ShardCoord.Statuses.ToArray() + var statuses = _shardCoord.Statuses.ToArray() .Where(x => x != null); var status = string.Join(", ", statuses @@ -331,7 +331,7 @@ namespace NadekoBot.Modules.Utility .WithIconUrl("https://cdn.discordapp.com/avatars/116275390695079945/b21045e778ef21c96d175400e779f0fb.jpg")) .AddField(efb => efb.WithName(GetText("author")).WithValue(_stats.Author).WithIsInline(true)) .AddField(efb => efb.WithName(GetText("botid")).WithValue(_client.CurrentUser.Id.ToString()).WithIsInline(true)) - .AddField(efb => efb.WithName(GetText("shard")).WithValue($"#{_bot.ShardId} / {_creds.TotalShards}").WithIsInline(true)) + .AddField(efb => efb.WithName(GetText("shard")).WithValue($"#{_client.ShardId} / {_creds.TotalShards}").WithIsInline(true)) .AddField(efb => efb.WithName(GetText("commands_ran")).WithValue(_stats.CommandsRan.ToString()).WithIsInline(true)) .AddField(efb => efb.WithName(GetText("messages")).WithValue($"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)").WithIsInline(true)) .AddField(efb => efb.WithName(GetText("memory")).WithValue($"{_stats.Heap} MB").WithIsInline(true)) diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 71a5a504..41bdda06 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -49,7 +49,6 @@ namespace NadekoBot public INServiceProvider Services { get; private set; } - public int ShardId { get; } public ShardsCoordinator ShardCoord { get; private set; } private readonly ShardComClient _comClient; @@ -63,8 +62,6 @@ namespace NadekoBot _log = LogManager.GetCurrentClassLogger(); TerribleElevatedPermissionCheck(); - ShardId = shardId; - Credentials = new BotCredentials(); Db = new DbService(Credentials); Client = new DiscordSocketClient(new DiscordSocketConfig @@ -92,7 +89,7 @@ namespace NadekoBot ErrorColor = new Color(Convert.ToUInt32(BotConfig.ErrorColor, 16)); } - SetupShard(shardId, parentProcessId, port.Value); + SetupShard(parentProcessId, port.Value); #if GLOBAL_NADEKO Client.Log += Client_Log; @@ -187,7 +184,7 @@ namespace NadekoBot } //connect - _log.Info("Shard {0} logging in ...", ShardId); + _log.Info("Shard {0} logging in ...", Client.ShardId); await Client.LoginAsync(TokenType.Bot, token).ConfigureAwait(false); await Client.StartAsync().ConfigureAwait(false); Client.Ready += SetClientReady; @@ -195,7 +192,7 @@ namespace NadekoBot Client.Ready -= SetClientReady; Client.JoinedGuild += Client_JoinedGuild; Client.LeftGuild += Client_LeftGuild; - _log.Info("Shard {0} logged in.", ShardId); + _log.Info("Shard {0} logged in.", Client.ShardId); } private Task Client_LeftGuild(SocketGuild arg) @@ -212,18 +209,18 @@ namespace NadekoBot public async Task RunAsync(params string[] args) { - if(ShardId == 0) + if(Client.ShardId == 0) _log.Info("Starting NadekoBot v" + StatsService.BotVersion); var sw = Stopwatch.StartNew(); await LoginAsync(Credentials.Token).ConfigureAwait(false); - _log.Info($"Shard {ShardId} loading services..."); + _log.Info($"Shard {Client.ShardId} loading services..."); AddServices(); sw.Stop(); - _log.Info($"Shard {ShardId} connected in {sw.Elapsed.TotalSeconds:F2}s"); + _log.Info($"Shard {Client.ShardId} connected in {sw.Elapsed.TotalSeconds:F2}s"); var stats = Services.GetService(); stats.Initialize(); @@ -257,7 +254,7 @@ namespace NadekoBot .ForEach(x => CommandService.RemoveModuleAsync(x)); Ready.TrySetResult(true); - _log.Info($"Shard {ShardId} ready."); + _log.Info($"Shard {Client.ShardId} ready."); //_log.Info(await stats.Print().ConfigureAwait(false)); } @@ -297,29 +294,27 @@ namespace NadekoBot } } - private void SetupShard(int shardId, int parentProcessId, int port) + private void SetupShard(int parentProcessId, int port) { - if (shardId != 0) - { - new Thread(new ThreadStart(() => - { - try - { - var p = Process.GetProcessById(parentProcessId); - if (p == null) - return; - p.WaitForExit(); - } - finally - { - Environment.Exit(10); - } - })).Start(); - } - else + if (Client.ShardId == 0) { ShardCoord = new ShardsCoordinator(port); + return; } + new Thread(new ThreadStart(() => + { + try + { + var p = Process.GetProcessById(parentProcessId); + if (p == null) + return; + p.WaitForExit(); + } + finally + { + Environment.Exit(10); + } + })).Start(); } } } diff --git a/src/NadekoBot/Services/Impl/NadekoStrings.cs b/src/NadekoBot/Services/Impl/NadekoStrings.cs index f6273770..651805b8 100644 --- a/src/NadekoBot/Services/Impl/NadekoStrings.cs +++ b/src/NadekoBot/Services/Impl/NadekoStrings.cs @@ -42,9 +42,9 @@ namespace NadekoBot.Services responseStrings = allLangsDict.ToImmutableDictionary(); sw.Stop(); - _log.Info("Loaded {0} languages ({1}) in {2:F2}s", + _log.Info("Loaded {0} languages in {1:F2}s", responseStrings.Count, - string.Join(",", responseStrings.Keys), + //string.Join(",", responseStrings.Keys), sw.Elapsed.TotalSeconds); ////improper string format checks