From 477a34a6eeca228f2a5ca908954861b8103dd541 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 30 Nov 2016 20:15:22 +0100 Subject: [PATCH] .stats much prettier, .SendErrorAsync added for normalized error messages --- docs/Commands List.md | 4 +- .../Modules/Administration/Administration.cs | 5 +- src/NadekoBot/Modules/Help/Help.cs | 3 +- src/NadekoBot/Modules/Utility/Utility.cs | 69 ++++++++++++++++++- src/NadekoBot/Services/Impl/StatsService.cs | 37 ++++++---- src/NadekoBot/_Extensions/Extensions.cs | 3 + 6 files changed, 98 insertions(+), 23 deletions(-) diff --git a/docs/Commands List.md b/docs/Commands List.md index 716039eb..3b38e04f 100644 --- a/docs/Commands List.md +++ b/docs/Commands List.md @@ -1,5 +1,5 @@ -For more information and how to setup your own NadekoBot, go to: -You can support the project on patreon: or paypal: `nadekodiscordbot@gmail.com` +You can support the project on patreon: or paypal: + ##Table Of Contents - [Help](#help) - [Administration](#administration) diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index 42e025a0..b137c279 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -640,8 +640,9 @@ namespace NadekoBot.Modules.Administration var channel = (ITextChannel)umsg.Channel; var user = channel.Guild.GetCurrentUser(); - - var enumerable = (await umsg.Channel.GetMessagesAsync()).Where(x => x.Author.Id == user.Id); + + var enumerable = (await umsg.Channel.GetMessagesAsync()).AsEnumerable(); + enumerable = enumerable.Where(x => x.Author.Id == user.Id); await umsg.Channel.DeleteMessagesAsync(enumerable); } diff --git a/src/NadekoBot/Modules/Help/Help.cs b/src/NadekoBot/Modules/Help/Help.cs index 11dd9437..b0e1338b 100644 --- a/src/NadekoBot/Modules/Help/Help.cs +++ b/src/NadekoBot/Modules/Help/Help.cs @@ -121,8 +121,7 @@ namespace NadekoBot.Modules.Help public Task Hgit(IUserMessage umsg) { var helpstr = new StringBuilder(); - helpstr.AppendLine(@"For more information and how to setup your own NadekoBot, go to: -You can support the project on patreon: or paypal: `nadekodiscordbot@gmail.com`"); + helpstr.AppendLine("You can support the project on patreon: or paypal: \n"); helpstr.AppendLine("##Table Of Contents"); helpstr.AppendLine(string.Join("\n", NadekoBot.CommandService.Modules.Where(m => m.Name.ToLowerInvariant() != "help").OrderBy(m => m.Name).Prepend(NadekoBot.CommandService.Modules.FirstOrDefault(m=>m.Name.ToLowerInvariant()=="help")).Select(m => $"- [{m.Name}](#{m.Name.ToLowerInvariant()})"))); helpstr.AppendLine(); diff --git a/src/NadekoBot/Modules/Utility/Utility.cs b/src/NadekoBot/Modules/Utility/Utility.cs index 3e191577..4e0dddf0 100644 --- a/src/NadekoBot/Modules/Utility/Utility.cs +++ b/src/NadekoBot/Modules/Utility/Utility.cs @@ -10,6 +10,11 @@ using NadekoBot.Extensions; using System.Text.RegularExpressions; using System.Reflection; using Discord.WebSocket; +using NadekoBot.Services.Impl; +using Discord.API; +using Embed = Discord.API.Embed; +using EmbedAuthor = Discord.API.EmbedAuthor; +using EmbedField = Discord.API.EmbedField; namespace NadekoBot.Modules.Utility { @@ -37,7 +42,7 @@ namespace NadekoBot.Modules.Utility int i = 0; if (!arr.Any()) - await channel.SendMessageAsync(_l["🚧 `Nobody is playing that game.`"]).ConfigureAwait(false); + await channel.SendErrorAsync("Nobody is playing that game.").ConfigureAwait(false); else await channel.SendMessageAsync("```css\n" + string.Join("\n", arr.GroupBy(item => (i++) / 3).Select(ig => string.Concat(ig.Select(el => $"• {el,-35}")))) + "\n```").ConfigureAwait(false); } @@ -158,7 +163,67 @@ namespace NadekoBot.Modules.Utility { var channel = umsg.Channel; - await channel.SendMessageAsync(await NadekoBot.Stats.Print()); + var stats = NadekoBot.Stats; + + await channel.EmbedAsync( + new Embed() + { + Author = new EmbedAuthor() + { + Name = $"NadekoBot v{StatsService.BotVersion}", + Url = "http://nadekobot.readthedocs.io/en/latest/", + IconUrl = "https://cdn.discordapp.com/avatars/116275390695079945/b21045e778ef21c96d175400e779f0fb.jpg" + }, + Fields = new[] { + new EmbedField() { + Name = "Author", + Value = stats.Author, + Inline = true + }, + new EmbedField() { + Name = "Library", + Value = stats.Library, + Inline = true + }, + new EmbedField() { + Name = "Bot ID", + Value = NadekoBot.Client.GetCurrentUser().Id.ToString(), + Inline = true + }, + new EmbedField() { + Name = "Commands Ran", + Value = stats.CommandsRan.ToString(), + Inline = true + }, + new EmbedField() { + Name = "Messages", + Value = $"{stats.MessageCounter} [{stats.MessagesPerSecond:F2}/sec]", + Inline = true + }, + new EmbedField() { + Name = "Memory", + Value = $"{stats.Heap} MB", + Inline = true + }, + new EmbedField() { + Name = "Owner ID(s)", + Value = stats.OwnerIds, + Inline = true + }, + new EmbedField() { + Name = "Uptime", + Value = stats.GetUptimeString("\n"), + Inline = true + }, + new EmbedField() { + Name = "Presence", + Value = $"{NadekoBot.Client.GetGuilds().Count} servers\n{stats.TextChannels} Text Channels\n{stats.VoiceChannels} Voice Channels", + Inline = true + }, + + }, + Color = NadekoBot.OkColor + }); } private Regex emojiFinder { get; } = new Regex(@"<:(?.+?):(?\d*)>", RegexOptions.Compiled); diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index 1c1cb367..e0916306 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -12,26 +12,33 @@ namespace NadekoBot.Services.Impl { public class StatsService : IStatsService { - private int messageCounter; - private ShardedDiscordClient client; + private ShardedDiscordClient client; private DateTime started; - private int commandsRan = 0; public const string BotVersion = "1.0-rc2"; + public string Author => "Kwoth#2560"; + public string Library => "Discord.Net"; + public int MessageCounter { get; private set; } = 0; + public int CommandsRan { get; private set; } = 0; public string Heap => Math.Round((double)GC.GetTotalMemory(false) / 1.MiB(), 2).ToString(); + public double MessagesPerSecond => MessageCounter / (double)GetUptime().TotalSeconds; + public int TextChannels => client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is ITextChannel)).Count(); + public int VoiceChannels => client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is IVoiceChannel)).Count(); + public string OwnerIds => string.Join(", ", NadekoBot.Credentials.OwnerIds); + + Timer carbonitexTimer { get; } - public StatsService(ShardedDiscordClient client, CommandHandler cmdHandler) { this.client = client; Reset(); - this.client.MessageReceived += _ => Task.FromResult(messageCounter++); - cmdHandler.CommandExecuted += (_, e) => commandsRan++; + this.client.MessageReceived += _ => Task.FromResult(MessageCounter++); + cmdHandler.CommandExecuted += (_, e) => CommandsRan++; this.client.Disconnected += _ => Reset(); @@ -61,20 +68,20 @@ namespace NadekoBot.Services.Impl public async Task Print() { var curUser = await client.GetCurrentUserAsync(); - return $@"```css -Author: [Kwoth#2560] | Library: [Discord.Net] + return $@" +Author: [{Author}] | Library: [{Library}] Bot Version: [{BotVersion}] Bot ID: {curUser.Id} -Owner ID(s): {string.Join(", ", NadekoBot.Credentials.OwnerIds)} +Owner ID(s): {OwnerIds} Uptime: {GetUptimeString()} -Servers: {client.GetGuilds().Count} | TextChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is ITextChannel)).Count()} | VoiceChannels: {client.GetGuilds().SelectMany(g => g.GetChannels().Where(c => c is IVoiceChannel)).Count()} -Commands Ran this session: {commandsRan} -Messages: {messageCounter} [{messageCounter / (double)GetUptime().TotalSeconds:F2}/sec] Heap: [{Heap} MB]```"; +Servers: {client.GetGuilds().Count} | TextChannels: {TextChannels} | VoiceChannels: {VoiceChannels} +Commands Ran this session: {CommandsRan} +Messages: {MessageCounter} [{MessagesPerSecond:F2}/sec] Heap: [{Heap} MB]"; } public Task Reset() { - messageCounter = 0; + MessageCounter = 0; started = DateTime.Now; return Task.CompletedTask; } @@ -82,10 +89,10 @@ Messages: {messageCounter} [{messageCounter / (double)GetUptime().TotalSeconds:F public TimeSpan GetUptime() => DateTime.Now - started; - public string GetUptimeString() + public string GetUptimeString(string separator = ", ") { var time = GetUptime(); - return time.Days + " days, " + time.Hours + " hours, and " + time.Minutes + " minutes."; + return $"{time.Days} days{separator}{time.Hours} hours{separator}{time.Minutes} minutes"; } } } diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index dc330324..0859d080 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -159,6 +159,9 @@ namespace NadekoBot.Extensions public static Task EmbedAsync(this IMessageChannel ch, Discord.API.Embed embed) => ch.SendMessageAsync("", embed: embed); + public static Task SendErrorAsync(this IMessageChannel ch, string error, string title = null, string url = null) + => ch.SendMessageAsync("", embed: new Embed() { Description = error, Title = title, Url = url, Color = NadekoBot.ErrorColor }); + public static Task SendTableAsync(this IMessageChannel ch, string seed, IEnumerable items, Func howToPrint, int columns = 3) { var i = 0;