From d9657436a4c78f51645fbd3450af6431427448a2 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sat, 12 Dec 2015 23:39:58 +0100 Subject: [PATCH] Decently sized C#6 refactor Also removed unused using statements --- NadekoBot/Classes/Cards.cs | 16 +--- NadekoBot/Classes/Extensions.cs | 7 +- NadekoBot/Classes/Trivia.cs | 110 +++++++++++--------------- NadekoBot/Classes/_JSONModels.cs | 36 ++++----- NadekoBot/Commands/CopyCommand.cs | 32 +++----- NadekoBot/Commands/DiceRollCommand.cs | 7 +- NadekoBot/Commands/DrawCommand.cs | 88 ++++++++++----------- NadekoBot/Commands/FlipCoinCommand.cs | 24 +++--- NadekoBot/Commands/HelpCommand.cs | 45 +++++------ NadekoBot/Modules/Conversations.cs | 17 +--- 10 files changed, 161 insertions(+), 221 deletions(-) diff --git a/NadekoBot/Classes/Cards.cs b/NadekoBot/Classes/Cards.cs index bc6a3c70..ac0e7f82 100644 --- a/NadekoBot/Classes/Cards.cs +++ b/NadekoBot/Classes/Cards.cs @@ -58,14 +58,10 @@ public class Cards this.suit = s; this.number = card_num; } - public string GetName() { - return cardNames[number]; - } - public override string ToString() - { - return cardNames[number] + " Of " + suit; - } + public string GetName() => cardNames[number]; + + public override string ToString() => cardNames[number] + " Of " + suit; public int CompareTo(object obj) { @@ -95,11 +91,7 @@ public class Cards /// Restart the game of blackjack. It will only refill the pool for now. Probably wont be used, unless you want to have only 1 bjg running at one time, /// then you will restart the same game every time. /// - public void Restart() - { - // you dont have to uncover what is actually happening anda da hood - RefillPool(); - } + public void Restart() => RefillPool(); /// /// Removes all cards from the pool and refills the pool with all of the possible cards. NOTE: I think this is too expensive. diff --git a/NadekoBot/Classes/Extensions.cs b/NadekoBot/Classes/Extensions.cs index 212f31e9..af4580db 100644 --- a/NadekoBot/Classes/Extensions.cs +++ b/NadekoBot/Classes/Extensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; using Discord.Commands; @@ -33,10 +32,8 @@ namespace NadekoBot /// EventArg /// Message to be sent /// - public static async Task Send(this CommandEventArgs e, string message) - { - return await NadekoBot.client.SendMessage(e.Channel, message); - } + public static async Task Send(this CommandEventArgs e, string message) + => await NadekoBot.client.SendMessage(e.Channel, message); /// /// Sends a message to the channel from which MessageEventArg came. /// diff --git a/NadekoBot/Classes/Trivia.cs b/NadekoBot/Classes/Trivia.cs index f83139d2..1b162cdf 100644 --- a/NadekoBot/Classes/Trivia.cs +++ b/NadekoBot/Classes/Trivia.cs @@ -6,8 +6,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; -using System.Net.Http; -using System.Text; using System.Threading.Tasks; using System.Timers; @@ -31,51 +29,40 @@ namespace NadekoBot return tg; } - public TriviaQuestion GetCurrentQuestion(long serverId) { - return runningTrivias[serverId].currentQuestion; - } + public TriviaQuestion GetCurrentQuestion(long serverId) => runningTrivias[serverId].currentQuestion; - public override Func DoFunc() + public override Func DoFunc() => async e => { - return async e => + TriviaGame tg; + if ((tg = StartNewGame(e)) != null) { - TriviaGame tg; - if ((tg = StartNewGame(e))!=null) - { - await e.Send( "**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command [tq] if game was started by accident.\nTyping [idfk] 15 seconds after the question has started will give you a hint."); - } - else - await e.Send( "Trivia game is already running on this server. The question is:\n**"+GetCurrentQuestion(e.Server.Id).Question+"**"); - }; - } + await e.Send("**Trivia game started!**\nFirst player to get to 10 points wins! You have 30 seconds per question.\nUse command [tq] if game was started by accident.\nTyping [idfk] 15 seconds after the question has started will give you a hint."); + } + else + await e.Send("Trivia game is already running on this server. The question is:\n**" + GetCurrentQuestion(e.Server.Id).Question + "**"); + }; - private Func LbFunc() + private Func LbFunc() => async e => { - return async e => + if (runningTrivias.ContainsKey(e.Server.Id)) { - if (runningTrivias.ContainsKey(e.Server.Id)) - { - var lb = runningTrivias[e.User.Server.Id].GetLeaderboard(); - await e.Send( lb); - } - else - await e.Send( "Trivia game is not running on this server."); - }; - } + var lb = runningTrivias[e.User.Server.Id].GetLeaderboard(); + await e.Send(lb); + } + else + await e.Send("Trivia game is not running on this server."); + }; - private Func RepeatFunc() + private Func RepeatFunc() => async e => { - return async e => + if (runningTrivias.ContainsKey(e.Server.Id)) { - if (runningTrivias.ContainsKey(e.Server.Id)) - { - var lb = runningTrivias[e.User.Server.Id].GetLeaderboard(); - await e.Send( lb); - } - else - await e.Send( "Trivia game is not running on this server."); - }; - } + var lb = runningTrivias[e.User.Server.Id].GetLeaderboard(); + await e.Send(lb); + } + else + await e.Send("Trivia game is not running on this server."); + }; public override void Init(CommandGroupBuilder cgb) { @@ -97,18 +84,15 @@ namespace NadekoBot .Do(QuitFunc()); } - private Func QuitFunc() + private Func QuitFunc() => async e => { - return async e => + if (runningTrivias.ContainsKey(e.Server.Id) && runningTrivias[e.Server.Id].ChannelId == e.Channel.Id) { - if (runningTrivias.ContainsKey(e.Server.Id) && runningTrivias[e.Server.Id].ChannelId ==e.Channel.Id) - { - await e.Send( "Trivia will stop after this question. Run [**@NadekoBot clr**] to remove this bot's messages from the channel."); - runningTrivias[e.Server.Id].StopGame(); - } - else await e.Send( "No trivias are running on this channel."); - }; - } + await e.Send("Trivia will stop after this question. Run [**@NadekoBot clr**] to remove this bot's messages from the channel."); + runningTrivias[e.Server.Id].StopGame(); + } + else await e.Send("No trivias are running on this channel."); + }; internal static void FinishGame(TriviaGame triviaGame) { @@ -122,13 +106,7 @@ namespace NadekoBot private long _serverId; private long _channellId; - public long ChannelId - { - get - { - return _channellId; - } - } + public long ChannelId => _channellId; private Dictionary users; @@ -271,19 +249,21 @@ namespace NadekoBot } } - public class TriviaQuestion { + public class TriviaQuestion + { public string Category; public string Question; public string Answer; - public TriviaQuestion(string q, string a) { + public TriviaQuestion(string q, string a) + { this.Question = q; this.Answer = a; } - public override string ToString() - { - return this.Category == null ? "--------**Q**--------\nQuestion: **" + this.Question + "?**" : "--------Q--------\nCategory: " + this.Category + "\nQuestion: **"+ this.Question+ "?**"; - } + public override string ToString() => + this.Category == null ? + "--------**Q**--------\nQuestion: **" + this.Question + "?**" : + "--------Q--------\nCategory: " + this.Category + "\nQuestion: **" + this.Question + "?**"; } public class TriviaQuestionsPool { @@ -303,7 +283,8 @@ namespace NadekoBot private Random _r; - public TriviaQuestionsPool() { + public TriviaQuestionsPool() + { _r = new Random(); pool = new List(); JArray arr = JArray.Parse(File.ReadAllText("questions.txt")); @@ -311,9 +292,10 @@ namespace NadekoBot foreach (var item in arr) { TriviaQuestion tq; - tq = new TriviaQuestion((string)item["Question"],(string)item["Answer"]); + tq = new TriviaQuestion((string)item["Question"], (string)item["Answer"]); - if (item?["Category"] != null) { + if (item?["Category"] != null) + { tq.Category = item["Category"].ToString(); } diff --git a/NadekoBot/Classes/_JSONModels.cs b/NadekoBot/Classes/_JSONModels.cs index dd94dec9..30e13761 100644 --- a/NadekoBot/Classes/_JSONModels.cs +++ b/NadekoBot/Classes/_JSONModels.cs @@ -21,16 +21,14 @@ public string description; public string image_url_lge; - public override string ToString() - { - return "`Title:` **" + title_english + - "**\n`Status:` " + airing_status + - "\n`Episodes:` " + total_episodes + - "\n`Link:` http://anilist.co/anime/" + id + - "\n`Synopsis:` " + description.Substring(0, description.Length > 500 ? 500 : description.Length) + "..." + - "\n`img:` " + image_url_lge; - } - } + public override string ToString() => + "`Title:` **" + title_english + + "**\n`Status:` " + airing_status + + "\n`Episodes:` " + total_episodes + + "\n`Link:` http://anilist.co/anime/" + id + + "\n`Synopsis:` " + description.Substring(0, description.Length > 500 ? 500 : description.Length) + "..." + + "\n`img:` " + image_url_lge; + } class MangaResult { @@ -42,15 +40,13 @@ public int total_volumes; public string description; - public override string ToString() - { - return "`Title:` **" + title_english + - "**\n`Status:` " + publishing_status + - "\n`Chapters:` " + total_chapters + - "\n`Volumes:` " + total_volumes + - "\n`Link:` http://anilist.co/manga/" + id + - "\n`Synopsis:` " + description.Substring(0, description.Length > 500 ? 500 : description.Length) + "..." + - "\n`img:` " + image_url_lge; - } + public override string ToString() => + "`Title:` **" + title_english + + "**\n`Status:` " + publishing_status + + "\n`Chapters:` " + total_chapters + + "\n`Volumes:` " + total_volumes + + "\n`Link:` http://anilist.co/manga/" + id + + "\n`Synopsis:` " + description.Substring(0, description.Length > 500 ? 500 : description.Length) + "..." + + "\n`img:` " + image_url_lge; } } \ No newline at end of file diff --git a/NadekoBot/Commands/CopyCommand.cs b/NadekoBot/Commands/CopyCommand.cs index ed6e51a8..5f3657dd 100644 --- a/NadekoBot/Commands/CopyCommand.cs +++ b/NadekoBot/Commands/CopyCommand.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using Discord.Commands; @@ -24,17 +22,14 @@ namespace NadekoBot } } - public override Func DoFunc() + public override Func DoFunc() => async e => { - return async e => - { - if (CopiedUsers.Contains(e.User.Id)) return; + if (CopiedUsers.Contains(e.User.Id)) return; - CopiedUsers.Add(e.User.Id); - await e.Send( " I'll start copying you now."); - return; - }; - } + CopiedUsers.Add(e.User.Id); + await e.Send(" I'll start copying you now."); + return; + }; public override void Init(CommandGroupBuilder cgb) { @@ -49,16 +44,13 @@ namespace NadekoBot .Do(StopCopy()); } - private Func StopCopy() + private Func StopCopy() => async e => { - return async e => - { - if (!CopiedUsers.Contains(e.User.Id)) return; + if (!CopiedUsers.Contains(e.User.Id)) return; - CopiedUsers.Remove(e.User.Id); - await e.Send( " I wont copy anymore."); - return; - }; - } + CopiedUsers.Remove(e.User.Id); + await e.Send(" I wont copy anymore."); + return; + }; } } diff --git a/NadekoBot/Commands/DiceRollCommand.cs b/NadekoBot/Commands/DiceRollCommand.cs index 5b6dfca4..7e5ff425 100644 --- a/NadekoBot/Commands/DiceRollCommand.cs +++ b/NadekoBot/Commands/DiceRollCommand.cs @@ -1,11 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using Discord.Commands; -using Discord.Modules; -using Discord; using System.Drawing; using System.Drawing.Imaging; @@ -85,9 +82,7 @@ namespace NadekoBot }; } - private Image GetDice(int num) { - return Image.FromFile("images/dice/"+num+".png"); - } + private Image GetDice(int num) => Image.FromFile("images/dice/" + num + ".png"); public override void Init(CommandGroupBuilder cgb) { diff --git a/NadekoBot/Commands/DrawCommand.cs b/NadekoBot/Commands/DrawCommand.cs index 0e6fc02e..db0f0ebd 100644 --- a/NadekoBot/Commands/DrawCommand.cs +++ b/NadekoBot/Commands/DrawCommand.cs @@ -13,51 +13,51 @@ namespace NadekoBot public DrawCommand() : base() { } - public override Func DoFunc() { - return async (e) => - { - if (cards == null) - { - await e.Send("Shuffling cards..."); - cards = new Cards(); - } - - try - { - int num = 1; - var isParsed = int.TryParse(e.GetArg("count"), out num); - if (!isParsed || num <2) - { - await client.SendFile(e.Channel, cards.DrawACard().Path); - return; - } - if (num > 5) - num = 5; + public override Func DoFunc() => async (e) => + { + if (cards == null) + { + await e.Send("Shuffling cards..."); + cards = new Cards(); + } - List images = new List(); - List cardObjects = new List(); - for (int i = 0; i < num; i++) - { - if (cards.CardPool.Count == 0 && i!= 0) - { - await e.Send( "No more cards in a deck."); - break; - } - var currentCard = cards.DrawACard(); - cardObjects.Add(currentCard); - images.Add(Image.FromFile(currentCard.Path)); - } - Bitmap bitmap = ImageHandler.MergeImages(images); - await client.SendFile(e.Channel, images.Count+" cards.jpg",ImageHandler.ImageToStream(bitmap, ImageFormat.Jpeg)); - if (cardObjects.Count == 5) { - await e.Send(Cards.GetHandValue(cardObjects)); - } - } - catch (Exception ex) { - Console.WriteLine("Error drawing (a) card(s) "+ex.ToString()); - } - }; - } + try + { + int num = 1; + var isParsed = int.TryParse(e.GetArg("count"), out num); + if (!isParsed || num < 2) + { + await client.SendFile(e.Channel, cards.DrawACard().Path); + return; + } + if (num > 5) + num = 5; + + List images = new List(); + List cardObjects = new List(); + for (int i = 0; i < num; i++) + { + if (cards.CardPool.Count == 0 && i != 0) + { + await e.Send("No more cards in a deck."); + break; + } + var currentCard = cards.DrawACard(); + cardObjects.Add(currentCard); + images.Add(Image.FromFile(currentCard.Path)); + } + Bitmap bitmap = ImageHandler.MergeImages(images); + await client.SendFile(e.Channel, images.Count + " cards.jpg", ImageHandler.ImageToStream(bitmap, ImageFormat.Jpeg)); + if (cardObjects.Count == 5) + { + await e.Send(Cards.GetHandValue(cardObjects)); + } + } + catch (Exception ex) + { + Console.WriteLine("Error drawing (a) card(s) " + ex.ToString()); + } + }; public override void Init(CommandGroupBuilder cgb) { diff --git a/NadekoBot/Commands/FlipCoinCommand.cs b/NadekoBot/Commands/FlipCoinCommand.cs index 60d00bbf..6b59597c 100644 --- a/NadekoBot/Commands/FlipCoinCommand.cs +++ b/NadekoBot/Commands/FlipCoinCommand.cs @@ -13,20 +13,18 @@ namespace NadekoBot _r = new Random(); } - public override Func DoFunc() + public override Func DoFunc() => async e => { - return async e => { - int num = _r.Next(0, 2); - if (num == 1) - { - await client.SendFile(e.Channel, @"images/coins/heads.png"); - } - else - { - await client.SendFile(e.Channel, @"images/coins/tails.png"); - } - }; - } + int num = _r.Next(0, 2); + if (num == 1) + { + await client.SendFile(e.Channel, @"images/coins/heads.png"); + } + else + { + await client.SendFile(e.Channel, @"images/coins/tails.png"); + } + }; public override void Init(CommandGroupBuilder cgb) { diff --git a/NadekoBot/Commands/HelpCommand.cs b/NadekoBot/Commands/HelpCommand.cs index 1c26806a..ba64f244 100644 --- a/NadekoBot/Commands/HelpCommand.cs +++ b/NadekoBot/Commands/HelpCommand.cs @@ -6,32 +6,29 @@ namespace NadekoBot { class HelpCommand : DiscordCommand { - public override Func DoFunc() + public override Func DoFunc() => async e => { - return async e => - { - string helpstr = "Official repo: **github.com/Kwoth/NadekoBot/** \n"; + string helpstr = "Official repo: **github.com/Kwoth/NadekoBot/** \n"; - string lastCategory = ""; - foreach (var com in client.Commands().AllCommands) - { - if (com.Category != lastCategory) - { - helpstr += "\n`----`**`" + com.Category + "`**`----`\n"; - lastCategory = com.Category; - } - helpstr += PrintCommandHelp(com); - } - helpstr = helpstr.Replace(NadekoBot.botMention, "@BotName"); - while (helpstr.Length > 2000) - { - var curstr = helpstr.Substring(0, 2000); - await client.SendMessage(e.User, curstr.Substring(0, curstr.LastIndexOf("\n")+1)); - helpstr = curstr.Substring(curstr.LastIndexOf("\n")+1) + helpstr.Substring(2000); - } - await client.SendMessage(e.User, helpstr); - }; - } + string lastCategory = ""; + foreach (var com in client.Commands().AllCommands) + { + if (com.Category != lastCategory) + { + helpstr += "\n`----`**`" + com.Category + "`**`----`\n"; + lastCategory = com.Category; + } + helpstr += PrintCommandHelp(com); + } + helpstr = helpstr.Replace(NadekoBot.botMention, "@BotName"); + while (helpstr.Length > 2000) + { + var curstr = helpstr.Substring(0, 2000); + await client.SendMessage(e.User, curstr.Substring(0, curstr.LastIndexOf("\n") + 1)); + helpstr = curstr.Substring(curstr.LastIndexOf("\n") + 1) + helpstr.Substring(2000); + } + await client.SendMessage(e.User, helpstr); + }; public override void Init(CommandGroupBuilder cgb) { diff --git a/NadekoBot/Modules/Conversations.cs b/NadekoBot/Modules/Conversations.cs index dcb0b574..3a85709a 100644 --- a/NadekoBot/Modules/Conversations.cs +++ b/NadekoBot/Modules/Conversations.cs @@ -8,7 +8,6 @@ using System.Timers; using System.Threading.Tasks; using System.Diagnostics; using System.Drawing; -using Newtonsoft.Json.Linq; using System.IO; using System.Text; using System.Drawing.Imaging; @@ -29,10 +28,7 @@ namespace NadekoBot.Modules return cb; } - private CommandBuilder AliasCommand(CommandBuilder cb, string txt) - { - return cb.Alias(new string[] { "," + txt, "-" + txt }); - } + private CommandBuilder AliasCommand(CommandBuilder cb, string txt) => cb.Alias(new string[] { "," + txt, "-" + txt }); public override void Install(ModuleManager manager) { @@ -546,7 +542,7 @@ namespace NadekoBot.Modules invites+=invite.Url+"\n"; i++; } - catch (Exception ex) { + catch (Exception) { j++; continue; } @@ -640,12 +636,7 @@ namespace NadekoBot.Modules return ImageHandler.ImageToStream(bm,ImageFormat.Png); } - private Func SayYes() - { - return async e => - { - await NadekoBot.client.SendMessage(e.Channel, "Yes. :)"); - }; - } + private Func SayYes() + => async e => await NadekoBot.client.SendMessage(e.Channel, "Yes. :)"); } }