diff --git a/NadekoBot/Classes/Extensions.cs b/NadekoBot/Classes/Extensions.cs index 9d0230e7..93527acd 100644 --- a/NadekoBot/Classes/Extensions.cs +++ b/NadekoBot/Classes/Extensions.cs @@ -8,6 +8,7 @@ using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; +using System.Text; using System.Threading.Tasks; namespace NadekoBot.Extensions @@ -180,6 +181,52 @@ namespace NadekoBot.Extensions } } + public static string GetOnPage(this IEnumerable source, int pageIndex, int itemsPerPage = 5) + { + var items = source.Skip(pageIndex * itemsPerPage).Take(itemsPerPage); + if (!items.Any()) + { + return $"No items on page {pageIndex + 1}."; + } + var sb = new StringBuilder($"---page {pageIndex + 1} --\n"); + var itemsDC = items as IEnumerable>>; + var itemsDS = items as IEnumerable>; + if (itemsDC != null) + { + foreach (var item in itemsDC) + { + sb.Append($"{ Format.Code(item.Key)}\n"); + int i = 1; + var last = item.Value.Last(); + foreach (var value in item.Value) + { + if (last != value) + sb.AppendLine(" `├" + i++ + "─`" + Format.Bold(value)); + else + sb.AppendLine(" `└" + i++ + "─`" + Format.Bold(value)); + } + + } + } + else if (itemsDS != null) + { + foreach (var item in itemsDS) + { + sb.Append($"{ Format.Code(item.Key)}\n"); + sb.AppendLine(" `└─`" + Format.Bold(item.Value)); + } + + } + else + { + foreach (var item in items) + { + sb.Append($"{ Format.Code(item.ToString())} \n"); + } + } + + return sb.ToString(); + } /// /// Gets the program runtime /// diff --git a/NadekoBot/Modules/Conversations/Conversations.cs b/NadekoBot/Modules/Conversations/Conversations.cs index a88841d9..227814c7 100644 --- a/NadekoBot/Modules/Conversations/Conversations.cs +++ b/NadekoBot/Modules/Conversations/Conversations.cs @@ -1,9 +1,11 @@ -using Discord; +using Discord; using Discord.Commands; using Discord.Modules; using NadekoBot.Classes.Conversations.Commands; using NadekoBot.DataModels; using NadekoBot.Extensions; +using NadekoBot.Classes; +using System.Drawing.Drawing2D; using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Properties; using System; @@ -194,12 +196,13 @@ namespace NadekoBot.Modules.Conversations return; var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); var text = ""; + var avatar = await GetAvatar(usr.AvatarUrl); text = usr?.Name ?? e.GetArg("user"); - await e.Channel.SendFile("ripzor_m8.png", - RipName(text, string.IsNullOrWhiteSpace(e.GetArg("year")) + var file = RipUser(text, avatar, string.IsNullOrWhiteSpace(e.GetArg("year")) ? null - : e.GetArg("year"))) - .ConfigureAwait(false); + : e.GetArg("year")); + await e.Channel.SendFile("ripzor_m8.png", + file); }); if (!NadekoBot.Config.DontJoinServers) { @@ -317,14 +320,19 @@ namespace NadekoBot.Modules.Conversations }); } - public Stream RipName(string name, string year = null) + + /// + /// Create a RIP image of the given name and avatar, with an optional year + /// + /// + /// + /// + /// + public Stream RipUser(string name, Image avatar, string year = null) { var bm = Resources.rip; - - var offset = name.Length * 5; - + var offset = name.Length * 2; var fontSize = 20; - if (name.Length > 10) { fontSize -= (name.Length - 10) / 2; @@ -332,14 +340,35 @@ namespace NadekoBot.Modules.Conversations //TODO use measure string var g = Graphics.FromImage(bm); - g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 200); - g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 235); + g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 220); + g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 240); + + g.DrawImage(avatar, 80, 135); + g.DrawImage((Image)Resources.rose_overlay, 0, 0); g.Flush(); g.Dispose(); return bm.ToStream(ImageFormat.Png); } + + public static async Task GetAvatar(string url) + { + var stream = await SearchHelper.GetResponseStreamAsync(url); + Bitmap bmp = new Bitmap(100, 100); + using (GraphicsPath gp = new GraphicsPath()) + { + gp.AddEllipse(0, 0, bmp.Width, bmp.Height); + using (Graphics gr = Graphics.FromImage(bmp)) + { + gr.SetClip(gp); + gr.DrawImage(Image.FromStream(stream), Point.Empty); + + } + } + return bmp; + + } private static Func SayYes() => async e => await e.Channel.SendMessage("Yes. :)").ConfigureAwait(false); } diff --git a/NadekoBot/Modules/Help/HelpModule.cs b/NadekoBot/Modules/Help/HelpModule.cs index 593fbd5f..f4fa618e 100644 --- a/NadekoBot/Modules/Help/HelpModule.cs +++ b/NadekoBot/Modules/Help/HelpModule.cs @@ -17,6 +17,8 @@ namespace NadekoBot.Modules.Help public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Help; + + public override void Install(ModuleManager manager) { manager.CreateCommands("", cgb => diff --git a/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs b/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs index 44f311dc..34ea9112 100644 --- a/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs +++ b/NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net; using System.Web; @@ -75,6 +74,7 @@ namespace NadekoBot.Modules.Translator.Helpers this.TranslationTime = TimeSpan.Zero; DateTime tmStart = DateTime.Now; string translation = string.Empty; + string text = string.Empty; try { @@ -83,31 +83,26 @@ namespace NadekoBot.Modules.Translator.Helpers GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), HttpUtility.UrlEncode(sourceText)); - string outputFile = Path.GetTempFileName(); using (WebClient wc = new WebClient()) { wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); - wc.DownloadFile(url, outputFile); + text = wc.DownloadString(url); } // Get translated text - if (File.Exists(outputFile)) + // Get phrase collection + // string text = File.ReadAllText(outputFile); + int index = text.IndexOf(string.Format(",,\"{0}\"", GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage))); + if (index == -1) { - - // Get phrase collection - string text = File.ReadAllText(outputFile); - int index = text.IndexOf(string.Format(",,\"{0}\"", GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage))); - if (index == -1) + // Translation of single word + int startQuote = text.IndexOf('\"'); + if (startQuote != -1) { - // Translation of single word - int startQuote = text.IndexOf('\"'); - if (startQuote != -1) + int endQuote = text.IndexOf('\"', startQuote + 1); + if (endQuote != -1) { - int endQuote = text.IndexOf('\"', startQuote + 1); - if (endQuote != -1) - { - translation = text.Substring(startQuote + 1, endQuote - startQuote - 1); - } + translation = text.Substring(startQuote + 1, endQuote - startQuote - 1); } } else diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index df37a039..afbc37af 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -492,6 +492,7 @@ +