From d2c455a9a605bbef599cd914ef634e9c0efd15f6 Mon Sep 17 00:00:00 2001 From: appelemac Date: Fri, 17 Jun 2016 22:10:51 +0200 Subject: [PATCH 1/5] Custom Random range --- .../CustomReactions/CustomReactions.cs | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/NadekoBot/Modules/CustomReactions/CustomReactions.cs index 39d59efd..4eee432a 100644 --- a/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -1,38 +1,42 @@ using Discord.Commands; using Discord.Modules; -using NadekoBot.Extensions; using NadekoBot.Modules.Permissions.Classes; using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; namespace NadekoBot.Modules.CustomReactions { - class CustomReactionsModule : DiscordModule + internal class CustomReactionsModule : DiscordModule { public override string Prefix { get; } = ""; - Random rng = new Random(); + private Random rng = new Random(); - private Dictionary> commandFuncs; + private Dictionary> commandFuncs; public CustomReactionsModule() { - commandFuncs = new Dictionary> + commandFuncs = new Dictionary> { - {"%rng%", (e) => rng.Next().ToString()}, - {"%mention%", (e) => NadekoBot.BotMention }, - {"%user%", e => e.User.Mention }, - {"%target%", e => e.GetArg("args")?.Trim() ?? "" }, + {new Regex(@"%rng:?(\d{0,9})-?(\d{0,9})%"), (e,m) => { + int start, end; + if (int.TryParse(m.Groups[1].Value, out start) && int.TryParse(m.Groups[2].Value, out end)) { + return rng.Next(start, end).ToString(); + } + else return rng.Next().ToString(); + } }, + {new Regex("%mention%"), (e,m) => NadekoBot.BotMention }, + {new Regex("%user%"), (e,m) => e.User.Mention }, + {new Regex("%target%"), (e,m) => e.GetArg("args")?.Trim() ?? "" }, }; } public override void Install(ModuleManager manager) { - manager.CreateCommands("", cgb => { - cgb.AddCheck(PermissionChecker.Instance); foreach (var command in NadekoBot.Config.CustomReactions) @@ -47,11 +51,17 @@ namespace NadekoBot.Modules.CustomReactions .Do(async e => { string str = command.Value[rng.Next(0, command.Value.Count())]; - commandFuncs.Keys.ForEach(k => str = str.Replace(k, commandFuncs[k](e))); + foreach (var key in commandFuncs.Keys) + { + foreach (Match m in key.Matches(str)) + { + str = str.Replace(m.Value, commandFuncs[key](e, m)); + } + } + await e.Channel.SendMessage(str).ConfigureAwait(false); }); } - }); } } From 4ed29ea38e4c9e45e27c02b901a862aa0b7cd919 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sat, 18 Jun 2016 13:26:31 +0200 Subject: [PATCH 2/5] made it a one-liner --- NadekoBot/Modules/CustomReactions/CustomReactions.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/NadekoBot/Modules/CustomReactions/CustomReactions.cs index 4eee432a..f4ebceac 100644 --- a/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -1,5 +1,6 @@ using Discord.Commands; using Discord.Modules; +using NadekoBot.Extensions; using NadekoBot.Modules.Permissions.Classes; using System; using System.Collections.Generic; @@ -30,6 +31,7 @@ namespace NadekoBot.Modules.CustomReactions {new Regex("%mention%"), (e,m) => NadekoBot.BotMention }, {new Regex("%user%"), (e,m) => e.User.Mention }, {new Regex("%target%"), (e,m) => e.GetArg("args")?.Trim() ?? "" }, + }; } @@ -51,13 +53,8 @@ namespace NadekoBot.Modules.CustomReactions .Do(async e => { string str = command.Value[rng.Next(0, command.Value.Count())]; - foreach (var key in commandFuncs.Keys) - { - foreach (Match m in key.Matches(str)) - { - str = str.Replace(m.Value, commandFuncs[key](e, m)); - } - } + commandFuncs.Keys.ForEach(key => str = key.Replace(str, m => commandFuncs[key](e, m))); + await e.Channel.SendMessage(str).ConfigureAwait(false); }); From c78f98bf19dd0d77490e9a90d94af21a478cd3c3 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sat, 18 Jun 2016 13:40:22 +0200 Subject: [PATCH 3/5] pfft, nitpick --- NadekoBot/Modules/CustomReactions/CustomReactions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/NadekoBot/Modules/CustomReactions/CustomReactions.cs index f4ebceac..e735efc7 100644 --- a/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -21,7 +21,7 @@ namespace NadekoBot.Modules.CustomReactions { commandFuncs = new Dictionary> { - {new Regex(@"%rng:?(\d{0,9})-?(\d{0,9})%"), (e,m) => { + {new Regex(@"%rng(?:%|:(\d{0,9})-(\d{0,9})%)"), (e,m) => { int start, end; if (int.TryParse(m.Groups[1].Value, out start) && int.TryParse(m.Groups[2].Value, out end)) { return rng.Next(start, end).ToString(); From 32c14aecbcd49ded3af9574dfa2f458e2baed3af Mon Sep 17 00:00:00 2001 From: appelemac Date: Sat, 18 Jun 2016 13:52:32 +0200 Subject: [PATCH 4/5] Slight changes --- NadekoBot/Modules/CustomReactions/CustomReactions.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/NadekoBot/Modules/CustomReactions/CustomReactions.cs index e735efc7..a2e3973c 100644 --- a/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -21,12 +21,14 @@ namespace NadekoBot.Modules.CustomReactions { commandFuncs = new Dictionary> { - {new Regex(@"%rng(?:%|:(\d{0,9})-(\d{0,9})%)"), (e,m) => { + {new Regex(@"(?:%rng%|%rng:(\d{1,9})-(\d{1,9})%)"), (e,m) => { int start, end; - if (int.TryParse(m.Groups[1].Value, out start) && int.TryParse(m.Groups[2].Value, out end)) { + if (m.Groups[1].Success) + { + start = int.Parse(m.Groups[1].Value); + end = int.Parse(m.Groups[2].Value); return rng.Next(start, end).ToString(); - } - else return rng.Next().ToString(); + }else return rng.Next().ToString(); } }, {new Regex("%mention%"), (e,m) => NadekoBot.BotMention }, {new Regex("%user%"), (e,m) => e.User.Mention }, @@ -54,7 +56,7 @@ namespace NadekoBot.Modules.CustomReactions { string str = command.Value[rng.Next(0, command.Value.Count())]; commandFuncs.Keys.ForEach(key => str = key.Replace(str, m => commandFuncs[key](e, m))); - + await e.Channel.SendMessage(str).ConfigureAwait(false); }); From 12d3c2ad951f2652385aed469fd8be6a0f96870f Mon Sep 17 00:00:00 2001 From: appelemac Date: Sat, 18 Jun 2016 15:08:15 +0200 Subject: [PATCH 5/5] removed comments --- .../Conversations/Commands/RipCommand.cs | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/NadekoBot/Modules/Conversations/Commands/RipCommand.cs b/NadekoBot/Modules/Conversations/Commands/RipCommand.cs index a0075488..5557ade8 100644 --- a/NadekoBot/Modules/Conversations/Commands/RipCommand.cs +++ b/NadekoBot/Modules/Conversations/Commands/RipCommand.cs @@ -39,7 +39,8 @@ namespace NadekoBot.Modules.Conversations.Commands file = RipName(text, string.IsNullOrWhiteSpace(e.GetArg("year")) ? null : e.GetArg("year")); - } else + } + else { var avatar = await GetAvatar(usr.AvatarUrl); text = usr.Name; @@ -63,16 +64,13 @@ namespace NadekoBot.Modules.Conversations.Commands 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 + int width = 300; + var fontSize = width / name.Length -2; + if (fontSize > 20) fontSize = 20; var g = Graphics.FromImage(bm); - g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 220); + Font nameFont = new Font("Comic Sans MS", fontSize, FontStyle.Bold, GraphicsUnit.Pixel); + SizeF nameSize = g.MeasureString(name, nameFont); + g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, (bm.Width /2 - 8) - (nameSize.Width /2), 243 - nameSize.Height); g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 240); g.DrawImage(avatar, 80, 135); @@ -86,19 +84,14 @@ namespace NadekoBot.Modules.Conversations.Commands public Stream RipName(string name, string year = null) { var bm = Resources.rip; - + int width = 190; var offset = name.Length * 5; - - var fontSize = 20; - - if (name.Length > 10) - { - fontSize -= (name.Length - 10) / 2; - } - - //TODO use measure string + var fontSize = width / name.Length; + if (fontSize > 20) fontSize = 20; var g = Graphics.FromImage(bm); - g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 200); + Font nameFont = new Font("Comic Sans MS", fontSize, FontStyle.Bold, GraphicsUnit.Pixel); + SizeF nameSize = g.MeasureString(name, nameFont); + g.DrawString(name, nameFont, Brushes.Black, (bm.Width / 2) - (nameSize.Width / 2), 200); g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 235); g.Flush(); g.Dispose();