diff --git a/src/NadekoBot/Modules/Administration/SelfCommands.cs b/src/NadekoBot/Modules/Administration/SelfCommands.cs index fb15bfa8b..5062bd55c 100644 --- a/src/NadekoBot/Modules/Administration/SelfCommands.cs +++ b/src/NadekoBot/Modules/Administration/SelfCommands.cs @@ -256,38 +256,49 @@ namespace NadekoBot.Modules.Administration var statuses = _coord.GetAllShardStatuses(); - var status = string.Join(", ", statuses - .GroupBy(x => x.ConnectionState) - .Select(x => $"{x.Count()} {x.Key}") + var status = string.Join(" : ", statuses + .Select(x => (ConnectionStateToEmoji(x), x)) + .GroupBy(x => x.Item1) + .Select(x => $"`{x.Count()} {x.Key}`") .ToArray()); var allShardStrings = statuses - .Select(x => + .Select(st => { - var timeDiff = DateTime.UtcNow - x.Time; - if (timeDiff >= TimeSpan.FromSeconds(30)) - return $"Shard #{Format.Bold(x.ShardId.ToString())} **UNRESPONSIVE** for {timeDiff.ToString(@"hh\:mm\:ss")}"; - return GetText("shard_stats_txt", x.ShardId.ToString(), - Format.Bold(x.ConnectionState.ToString()), Format.Bold(x.Guilds.ToString()), timeDiff.ToString(@"hh\:mm\:ss")); + var stateStr = ConnectionStateToEmoji(st); + var timeDiff = DateTime.UtcNow - st.LastUpdate; + var maxGuildCountLength = statuses.Max(x => x.GuildCount).ToString().Length; + return $"`{stateStr} " + + $"| #{st.ShardId.ToString().PadBoth(3)} " + + $"| {timeDiff:mm\\:ss} " + + $"| {st.GuildCount.ToString().PadBoth(maxGuildCountLength)} `"; }) .ToArray(); - await ctx.SendPaginatedConfirmAsync(page, (curPage) => { - var str = string.Join("\n", allShardStrings.Skip(25 * curPage).Take(25)); if (string.IsNullOrWhiteSpace(str)) str = GetText("no_shards_on_page"); return new EmbedBuilder() - .WithAuthor(a => a.WithName(GetText("shard_stats"))) - .WithTitle(status) .WithOkColor() - .WithDescription(str); + .WithDescription($"{status}\n\n{str}"); }, allShardStrings.Length, 25).ConfigureAwait(false); } + private static string ConnectionStateToEmoji(ShardStatus status) + { + var timeDiff = DateTime.UtcNow - status.LastUpdate; + return status.ConnectionState switch + { + ConnectionState.Connected => "✅", + ConnectionState.Disconnected => "🔻", + _ when timeDiff > TimeSpan.FromSeconds(30) => " ❗ ", + _ => " ⏳" + }; + } + [NadekoCommand, Usage, Description, Aliases] [OwnerOnly] public async Task RestartShard(int shardId) diff --git a/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs b/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs index f2cac996f..d71f3422c 100644 --- a/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs +++ b/src/NadekoBot/Modules/Music/Services/extractor/YtLoader.cs @@ -126,7 +126,7 @@ namespace NadekoBot.Modules.Music.Services var responseSpan = response.AsSpan().Slice(140_000); var startIndex = responseSpan.IndexOf(YT_RESULT_INITIAL_DATA); if (startIndex == -1) - return null; // todo try selecting html + return null; // todo future try selecting html startIndex += YT_RESULT_INITIAL_DATA.Length; var endIndex = 140_000 + startIndex + responseSpan.Slice(startIndex + 20_000).IndexOf(YT_RESULT_JSON_END) + 20_000; diff --git a/src/NadekoBot/Services/ICoordinator.cs b/src/NadekoBot/Services/ICoordinator.cs index e104ea5d9..dd3f6922f 100644 --- a/src/NadekoBot/Services/ICoordinator.cs +++ b/src/NadekoBot/Services/ICoordinator.cs @@ -8,15 +8,15 @@ namespace NadekoBot.Services bool RestartBot(); void Die(); bool RestartShard(int shardId); - IEnumerable GetAllShardStatuses(); + IList GetAllShardStatuses(); int GetGuildCount(); } public class ShardStatus { public Discord.ConnectionState ConnectionState { get; set; } - public DateTime Time { get; set; } + public DateTime LastUpdate { get; set; } public int ShardId { get; set; } - public int Guilds { get; set; } + public int GuildCount { get; set; } } } \ No newline at end of file diff --git a/src/NadekoBot/Services/Impl/RemoteGrpcCoordinator.cs b/src/NadekoBot/Services/Impl/RemoteGrpcCoordinator.cs index 9525a58af..ba8440ba1 100644 --- a/src/NadekoBot/Services/Impl/RemoteGrpcCoordinator.cs +++ b/src/NadekoBot/Services/Impl/RemoteGrpcCoordinator.cs @@ -54,7 +54,7 @@ namespace NadekoBot.Services return true; } - public IEnumerable GetAllShardStatuses() + public IList GetAllShardStatuses() { var res = _coordClient.GetAllStatuses(new GetAllStatusesRequest()); @@ -63,9 +63,9 @@ namespace NadekoBot.Services .Map(s => new ShardStatus() { ConnectionState = FromCoordConnState(s.State), - Guilds = s.GuildCount, + GuildCount = s.GuildCount, ShardId = s.ShardId, - Time = s.LastUpdate.ToDateTime(), + LastUpdate = s.LastUpdate.ToDateTime(), }); } @@ -88,7 +88,7 @@ namespace NadekoBot.Services var reply = await _coordClient.HeartbeatAsync(new HeartbeatRequest { State = ToCoordConnState(_client.ConnectionState), - GuildCount = _client.ConnectionState == Discord.ConnectionState.Connected ? _client.Guilds.Count : 0, + GuildCount = _client.ConnectionState == ConnectionState.Connected ? _client.Guilds.Count : 0, ShardId = _client.ShardId, }, deadline: DateTime.UtcNow + TimeSpan.FromSeconds(10)); gracefulImminent = reply.GracefulImminent; @@ -119,20 +119,20 @@ namespace NadekoBot.Services return Task.CompletedTask; } - private ConnState ToCoordConnState(Discord.ConnectionState state) + private ConnState ToCoordConnState(ConnectionState state) => state switch { - Discord.ConnectionState.Connecting => ConnState.Connecting, - Discord.ConnectionState.Connected => ConnState.Connected, + ConnectionState.Connecting => ConnState.Connecting, + ConnectionState.Connected => ConnState.Connected, _ => ConnState.Disconnected }; - private Discord.ConnectionState FromCoordConnState(ConnState state) + private ConnectionState FromCoordConnState(ConnState state) => state switch { - ConnState.Connecting => Discord.ConnectionState.Connecting, - ConnState.Connected => Discord.ConnectionState.Connected, - _ => Discord.ConnectionState.Disconnected + ConnState.Connecting => ConnectionState.Connecting, + ConnState.Connected => ConnectionState.Connected, + _ => ConnectionState.Disconnected }; } } \ No newline at end of file diff --git a/src/NadekoBot/_Extensions/StringExtensions.cs b/src/NadekoBot/_Extensions/StringExtensions.cs index f221ac1a4..3a43c2bc3 100644 --- a/src/NadekoBot/_Extensions/StringExtensions.cs +++ b/src/NadekoBot/_Extensions/StringExtensions.cs @@ -12,6 +12,13 @@ namespace NadekoBot.Extensions { public static class StringExtensions { + public static string PadBoth(this string str, int length) + { + int spaces = length - str.Length; + int padLeft = spaces / 2 + str.Length; + return str.PadLeft(padLeft).PadRight(length); + } + public static T MapJson(this string str) => JsonConvert.DeserializeObject(str); diff --git a/src/NadekoBot/data/strings/commands/commands.en-US.yml b/src/NadekoBot/data/strings/commands/commands.en-US.yml index aad5ef979..088d908b1 100644 --- a/src/NadekoBot/data/strings/commands/commands.en-US.yml +++ b/src/NadekoBot/data/strings/commands/commands.en-US.yml @@ -1446,7 +1446,9 @@ stringsreload: args: - "" shardstats: - desc: "Stats for shards. Paginated with 25 shards per page." + desc: |- + Stats for shards. Paginated with 25 shards per page. + Format: `[status] | # [shard_id] | [last_heartbeat] | [server_count]` args: - "" - "2"