More cleanup

This commit is contained in:
Master Kwoth 2017-07-15 05:23:46 +02:00
parent 4eca5be1d4
commit 30bafa8a89
3 changed files with 31 additions and 36 deletions

View File

@ -28,14 +28,14 @@ namespace NadekoBot.Modules.Utility
private readonly DiscordSocketClient _client; private readonly DiscordSocketClient _client;
private readonly IStatsService _stats; private readonly IStatsService _stats;
private readonly IBotCredentials _creds; 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; _client = client;
_stats = stats; _stats = stats;
_creds = creds; _creds = creds;
_bot = bot; _shardCoord = shardCoord;
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -286,7 +286,7 @@ namespace NadekoBot.Modules.Utility
{ {
if (--page < 0) if (--page < 0)
return; return;
var statuses = _bot.ShardCoord.Statuses.ToArray() var statuses = _shardCoord.Statuses.ToArray()
.Where(x => x != null); .Where(x => x != null);
var status = string.Join(", ", statuses var status = string.Join(", ", statuses
@ -331,7 +331,7 @@ namespace NadekoBot.Modules.Utility
.WithIconUrl("https://cdn.discordapp.com/avatars/116275390695079945/b21045e778ef21c96d175400e779f0fb.jpg")) .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("author")).WithValue(_stats.Author).WithIsInline(true))
.AddField(efb => efb.WithName(GetText("botid")).WithValue(_client.CurrentUser.Id.ToString()).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("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("messages")).WithValue($"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)").WithIsInline(true))
.AddField(efb => efb.WithName(GetText("memory")).WithValue($"{_stats.Heap} MB").WithIsInline(true)) .AddField(efb => efb.WithName(GetText("memory")).WithValue($"{_stats.Heap} MB").WithIsInline(true))

View File

@ -49,7 +49,6 @@ namespace NadekoBot
public INServiceProvider Services { get; private set; } public INServiceProvider Services { get; private set; }
public int ShardId { get; }
public ShardsCoordinator ShardCoord { get; private set; } public ShardsCoordinator ShardCoord { get; private set; }
private readonly ShardComClient _comClient; private readonly ShardComClient _comClient;
@ -63,8 +62,6 @@ namespace NadekoBot
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
TerribleElevatedPermissionCheck(); TerribleElevatedPermissionCheck();
ShardId = shardId;
Credentials = new BotCredentials(); Credentials = new BotCredentials();
Db = new DbService(Credentials); Db = new DbService(Credentials);
Client = new DiscordSocketClient(new DiscordSocketConfig Client = new DiscordSocketClient(new DiscordSocketConfig
@ -92,7 +89,7 @@ namespace NadekoBot
ErrorColor = new Color(Convert.ToUInt32(BotConfig.ErrorColor, 16)); ErrorColor = new Color(Convert.ToUInt32(BotConfig.ErrorColor, 16));
} }
SetupShard(shardId, parentProcessId, port.Value); SetupShard(parentProcessId, port.Value);
#if GLOBAL_NADEKO #if GLOBAL_NADEKO
Client.Log += Client_Log; Client.Log += Client_Log;
@ -187,7 +184,7 @@ namespace NadekoBot
} }
//connect //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.LoginAsync(TokenType.Bot, token).ConfigureAwait(false);
await Client.StartAsync().ConfigureAwait(false); await Client.StartAsync().ConfigureAwait(false);
Client.Ready += SetClientReady; Client.Ready += SetClientReady;
@ -195,7 +192,7 @@ namespace NadekoBot
Client.Ready -= SetClientReady; Client.Ready -= SetClientReady;
Client.JoinedGuild += Client_JoinedGuild; Client.JoinedGuild += Client_JoinedGuild;
Client.LeftGuild += Client_LeftGuild; 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) private Task Client_LeftGuild(SocketGuild arg)
@ -212,18 +209,18 @@ namespace NadekoBot
public async Task RunAsync(params string[] args) public async Task RunAsync(params string[] args)
{ {
if(ShardId == 0) if(Client.ShardId == 0)
_log.Info("Starting NadekoBot v" + StatsService.BotVersion); _log.Info("Starting NadekoBot v" + StatsService.BotVersion);
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
await LoginAsync(Credentials.Token).ConfigureAwait(false); await LoginAsync(Credentials.Token).ConfigureAwait(false);
_log.Info($"Shard {ShardId} loading services..."); _log.Info($"Shard {Client.ShardId} loading services...");
AddServices(); AddServices();
sw.Stop(); 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<IStatsService>(); var stats = Services.GetService<IStatsService>();
stats.Initialize(); stats.Initialize();
@ -257,7 +254,7 @@ namespace NadekoBot
.ForEach(x => CommandService.RemoveModuleAsync(x)); .ForEach(x => CommandService.RemoveModuleAsync(x));
Ready.TrySetResult(true); Ready.TrySetResult(true);
_log.Info($"Shard {ShardId} ready."); _log.Info($"Shard {Client.ShardId} ready.");
//_log.Info(await stats.Print().ConfigureAwait(false)); //_log.Info(await stats.Print().ConfigureAwait(false));
} }
@ -297,10 +294,13 @@ namespace NadekoBot
} }
} }
private void SetupShard(int shardId, int parentProcessId, int port) private void SetupShard(int parentProcessId, int port)
{ {
if (shardId != 0) if (Client.ShardId == 0)
{ {
ShardCoord = new ShardsCoordinator(port);
return;
}
new Thread(new ThreadStart(() => new Thread(new ThreadStart(() =>
{ {
try try
@ -316,10 +316,5 @@ namespace NadekoBot
} }
})).Start(); })).Start();
} }
else
{
ShardCoord = new ShardsCoordinator(port);
}
}
} }
} }

View File

@ -42,9 +42,9 @@ namespace NadekoBot.Services
responseStrings = allLangsDict.ToImmutableDictionary(); responseStrings = allLangsDict.ToImmutableDictionary();
sw.Stop(); sw.Stop();
_log.Info("Loaded {0} languages ({1}) in {2:F2}s", _log.Info("Loaded {0} languages in {1:F2}s",
responseStrings.Count, responseStrings.Count,
string.Join(",", responseStrings.Keys), //string.Join(",", responseStrings.Keys),
sw.Elapsed.TotalSeconds); sw.Elapsed.TotalSeconds);
////improper string format checks ////improper string format checks