From 537595694a3e4d8737b9f17bc8a1499244b9d40f Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 13 Dec 2015 03:54:21 +0100 Subject: [PATCH] More refactor, more extension methods. Preparing to ditch this project... I just want to reimplement image searches and then i will probably stop working on this project. --- NadekoBot/Classes/Extensions.cs | 44 ++++++++++++++++++++++++++++-- NadekoBot/Classes/Trivia.cs | 6 ++-- NadekoBot/Commands/HelpCommand.cs | 4 +-- NadekoBot/Modules/Conversations.cs | 10 +++---- NadekoBot/Modules/Music.cs | 2 +- NadekoBot/Modules/Searches.cs | 6 ++-- NadekoBot/StatsCollector.cs | 4 +-- 7 files changed, 58 insertions(+), 18 deletions(-) diff --git a/NadekoBot/Classes/Extensions.cs b/NadekoBot/Classes/Extensions.cs index af4580db..6c5087c2 100644 --- a/NadekoBot/Classes/Extensions.cs +++ b/NadekoBot/Classes/Extensions.cs @@ -34,6 +34,7 @@ namespace NadekoBot /// 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. /// @@ -45,16 +46,55 @@ namespace NadekoBot await NadekoBot.client.SendMessage(e.Channel, message); } + /// + /// Sends a message to this channel. + /// + /// + /// + /// + public static async Task Send(this Channel c, string message) + { + await NadekoBot.client.SendMessage(c, message); + } + + /// + /// Sends a private message to this user. + /// + /// + /// + /// + public static async Task Send(this User u, string message) + { + await NadekoBot.client.SendMessage(u, message); + } + + /// + /// Replies to a user who invoked this command, message start with that user's mention. + /// + /// + /// + /// public static async Task Reply(this CommandEventArgs e, string message) { - await NadekoBot.client.SendMessage(e.Channel, e.User.Mention + " " + message); + await e.Send(e.User.Mention + " " + message); } + /// + /// Replies to a user who invoked this command, message start with that user's mention. + /// + /// + /// + /// public static async Task Reply(this MessageEventArgs e, string message) { - await NadekoBot.client.SendMessage(e.Channel, e.User.Mention + " " + message); + await e.Send(e.User.Mention + " " + message); } + /// + /// Randomizes element order in a list + /// + /// + /// public static void Shuffle(this IList list) { RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider(); diff --git a/NadekoBot/Classes/Trivia.cs b/NadekoBot/Classes/Trivia.cs index 1b162cdf..4309a442 100644 --- a/NadekoBot/Classes/Trivia.cs +++ b/NadekoBot/Classes/Trivia.cs @@ -196,7 +196,7 @@ namespace NadekoBot if (currentQuestion == null || isQuit) { - client.SendMessage(ch, "Trivia bot stopping. :\\\n" + GetLeaderboard()); + await ch.Send("Trivia bot stopping. :\\\n" + GetLeaderboard()); FinishGame(); return; } @@ -205,7 +205,7 @@ namespace NadekoBot t.Enabled = true; t.Elapsed += async (s, ev) => { active = true; - await client.SendMessage(ch, currentQuestion.ToString()); + await ch.Send(currentQuestion.ToString()); t.Enabled = false; timeout.Enabled = true;//starting countdown of the next question stopwatch.Reset(); @@ -215,7 +215,7 @@ namespace NadekoBot } private async void TimeUp() { - await client.SendMessage(client.GetChannel(_channellId), "**Time's up.**\nCorrect answer was: **" + currentQuestion.Answer+"**\n\n*[tq quits trivia][tl shows leaderboard]["+NadekoBot.botMention+" clr clears my messages]*"); + await client.GetChannel(_channellId)?.Send("**Time's up.**\nCorrect answer was: **" + currentQuestion.Answer+"**\n\n*[tq quits trivia][tl shows leaderboard]["+NadekoBot.botMention+" clr clears my messages]*"); LoadNextRound(); } diff --git a/NadekoBot/Commands/HelpCommand.cs b/NadekoBot/Commands/HelpCommand.cs index ba64f244..b208183c 100644 --- a/NadekoBot/Commands/HelpCommand.cs +++ b/NadekoBot/Commands/HelpCommand.cs @@ -24,10 +24,10 @@ namespace NadekoBot while (helpstr.Length > 2000) { var curstr = helpstr.Substring(0, 2000); - await client.SendMessage(e.User, curstr.Substring(0, curstr.LastIndexOf("\n") + 1)); + await e.User.Send(curstr.Substring(0, curstr.LastIndexOf("\n") + 1)); helpstr = curstr.Substring(curstr.LastIndexOf("\n") + 1) + helpstr.Substring(2000); } - await client.SendMessage(e.User, helpstr); + await e.User.Send(helpstr); }; public override void Init(CommandGroupBuilder cgb) diff --git a/NadekoBot/Modules/Conversations.cs b/NadekoBot/Modules/Conversations.cs index 3a85709a..1c530bf9 100644 --- a/NadekoBot/Modules/Conversations.cs +++ b/NadekoBot/Modules/Conversations.cs @@ -351,7 +351,7 @@ namespace NadekoBot.Modules f.Read(b, 0, b.Length); f.Close(); string str = Encoding.ASCII.GetString(b); - await client.SendMessage(e.User, "```" + (str.Length < 1950 ? str : str.Substring(0, 1950)) + "```"); + await e.User.Send("```" + (str.Length < 1950 ? str : str.Substring(0, 1950)) + "```"); }); CreateCommand(cgb, "cs") @@ -400,9 +400,9 @@ namespace NadekoBot.Modules { string str = StatsCollector.GetRequests(); if (str.Trim().Length > 110) - await client.SendMessage(e.User, str); + await e.User.Send(str); else - await client.SendMessage(e.User, "No requests atm."); + await e.User.Send("No requests atm."); }); CreateCommand(cgb, "dr") @@ -444,7 +444,7 @@ namespace NadekoBot.Modules if (sc != null) { await e.Send(e.User.Mention + " Request resolved, notice sent."); - await client.SendMessage(client.GetUser(client.GetServer(sc.ServerId), sc.Id), "**This request of yours has been resolved:**\n" + sc.Text); + await client.GetUser(client.GetServer(sc.ServerId), sc.Id).Send("**This request of yours has been resolved:**\n" + sc.Text); } else { @@ -637,6 +637,6 @@ namespace NadekoBot.Modules } private Func SayYes() - => async e => await NadekoBot.client.SendMessage(e.Channel, "Yes. :)"); + => async e => await e.Send("Yes. :)"); } } diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index 71726be9..3be04570 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -20,7 +20,7 @@ namespace NadekoBot.Modules private static bool exit = true; public static bool NextSong = false; - public static Discord.Audio.DiscordAudioClient Voice; + public static DiscordAudioClient Voice; public static Channel VoiceChannel; public static bool Pause = false; public static List SongQueue = new List(); diff --git a/NadekoBot/Modules/Searches.cs b/NadekoBot/Modules/Searches.cs index ba70fbc6..088eb8ea 100644 --- a/NadekoBot/Modules/Searches.cs +++ b/NadekoBot/Modules/Searches.cs @@ -36,7 +36,7 @@ namespace NadekoBot.Modules if (!(await ValidateQuery(e.Channel, e.GetArg("query")))) return; var str = ShortenUrl(FindYoutubeUrlByKeywords(e.GetArg("query"))); - if (str == null || str.Trim().Length < 5) + if (string.IsNullOrEmpty(str.Trim())) { await e.Send( "Query failed"); return; @@ -140,9 +140,9 @@ namespace NadekoBot.Modules } private static async Task ValidateQuery(Discord.Channel ch,string query) { - if (query == null || query.Trim().Length == 0) + if (string.IsNullOrEmpty(query.Trim())) { - await NadekoBot.client.SendMessage(ch, "Please specify search parameters."); + await ch.Send("Please specify search parameters."); return false; } return true; diff --git a/NadekoBot/StatsCollector.cs b/NadekoBot/StatsCollector.cs index f174492d..7255a1c3 100644 --- a/NadekoBot/StatsCollector.cs +++ b/NadekoBot/StatsCollector.cs @@ -69,7 +69,7 @@ namespace NadekoBot try { await NadekoBot.client.AcceptInvite(await NadekoBot.client.GetInvite(code)); - await NadekoBot.client.SendMessage(e.Channel, e.User.Mention + " I joined it, thanks :)"); + await e.Send(e.User.Mention + " I joined it, thanks :)"); DEBUG_LOG("Sucessfuly joined server with code " + code); DEBUG_LOG("Here is a link for you: discord.gg/" + code); } @@ -80,7 +80,7 @@ namespace NadekoBot } public static void DEBUG_LOG(string text) { - NadekoBot.client.SendMessage(NadekoBot.client.GetChannel(119365591852122112), text); + NadekoBot.client.GetChannel(119365591852122112).Send(text); } private void StartCollecting() {