From 00eb8acaf674dcec2cb4351dd69fad009b9db9be Mon Sep 17 00:00:00 2001 From: appelemac Date: Wed, 15 Jun 2016 16:28:28 +0200 Subject: [PATCH] Hofix --- .../Conversations/Commands/RipCommand.cs | 127 ++++++++++++++++++ .../Modules/Conversations/Conversations.cs | 70 +--------- NadekoBot/NadekoBot.csproj | 1 + 3 files changed, 132 insertions(+), 66 deletions(-) create mode 100644 NadekoBot/Modules/Conversations/Commands/RipCommand.cs diff --git a/NadekoBot/Modules/Conversations/Commands/RipCommand.cs b/NadekoBot/Modules/Conversations/Commands/RipCommand.cs new file mode 100644 index 00000000..a0075488 --- /dev/null +++ b/NadekoBot/Modules/Conversations/Commands/RipCommand.cs @@ -0,0 +1,127 @@ +using NadekoBot.Classes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Commands; +using System.Drawing; +using System.Drawing.Drawing2D; +using NadekoBot.Properties; +using System.IO; +using System.Drawing.Imaging; +using NadekoBot.Extensions; + +namespace NadekoBot.Modules.Conversations.Commands +{ + class RipCommand : DiscordCommand + { + public RipCommand(DiscordModule module) : base(module) + { + } + + internal override void Init(CommandGroupBuilder cgb) + { + cgb.CreateCommand("rip") + .Description("Shows a grave image of someone with a start year\n**Usage**: @NadekoBot rip @Someone 2000") + .Parameter("user", ParameterType.Required) + .Parameter("year", ParameterType.Optional) + .Do(async e => + { + if (string.IsNullOrWhiteSpace(e.GetArg("user"))) + return; + var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); + var text = ""; + Stream file; + if (usr == null) + { + text = e.GetArg("user"); + file = RipName(text, string.IsNullOrWhiteSpace(e.GetArg("year")) + ? null + : e.GetArg("year")); + } else + { + var avatar = await GetAvatar(usr.AvatarUrl); + text = usr.Name; + file = RipUser(text, avatar, string.IsNullOrWhiteSpace(e.GetArg("year")) + ? null + : e.GetArg("year")); + } + await e.Channel.SendFile("ripzor_m8.png", + file); + }); + } + + + /// + /// 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 * 2; + var fontSize = 20; + if (name.Length > 10) + { + fontSize -= (name.Length - 10) / 2; + } + + //TODO use measure string + var g = Graphics.FromImage(bm); + 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 Stream RipName(string name, string year = null) + { + var bm = Resources.rip; + + var offset = name.Length * 5; + + var fontSize = 20; + + if (name.Length > 10) + { + fontSize -= (name.Length - 10) / 2; + } + + //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.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; + + } + } +} diff --git a/NadekoBot/Modules/Conversations/Conversations.cs b/NadekoBot/Modules/Conversations/Conversations.cs index 227814c7..7ce103f5 100644 --- a/NadekoBot/Modules/Conversations/Conversations.cs +++ b/NadekoBot/Modules/Conversations/Conversations.cs @@ -15,6 +15,7 @@ using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Threading.Tasks; +using NadekoBot.Modules.Conversations.Commands; namespace NadekoBot.Modules.Conversations { @@ -25,6 +26,7 @@ namespace NadekoBot.Modules.Conversations { commands.Add(new CopyCommand(this)); commands.Add(new RequestsCommand(this)); + commands.Add(new RipCommand(this)); } public override string Prefix { get; } = String.Format(NadekoBot.Config.CommandPrefixes.Conversations, NadekoBot.Creds.BotId); @@ -186,24 +188,7 @@ namespace NadekoBot.Modules.Conversations await e.Channel.SendMessage(str).ConfigureAwait(false); }); - cgb.CreateCommand("rip") - .Description("Shows a grave image of someone with a start year\n**Usage**: @NadekoBot rip @Someone 2000") - .Parameter("user", ParameterType.Required) - .Parameter("year", ParameterType.Optional) - .Do(async e => - { - if (string.IsNullOrWhiteSpace(e.GetArg("user"))) - return; - var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); - var text = ""; - var avatar = await GetAvatar(usr.AvatarUrl); - text = usr?.Name ?? e.GetArg("user"); - var file = RipUser(text, avatar, string.IsNullOrWhiteSpace(e.GetArg("year")) - ? null - : e.GetArg("year")); - await e.Channel.SendFile("ripzor_m8.png", - file); - }); + if (!NadekoBot.Config.DontJoinServers) { cgb.CreateCommand("j") @@ -321,54 +306,7 @@ namespace NadekoBot.Modules.Conversations } - /// - /// 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 * 2; - var fontSize = 20; - if (name.Length > 10) - { - fontSize -= (name.Length - 10) / 2; - } - - //TODO use measure string - var g = Graphics.FromImage(bm); - 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/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index afbc37af..3876300b 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -140,6 +140,7 @@ +