From 4bc7ec9d479f59faa33569b23f1571eadc09043b Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 14 Apr 2016 23:47:02 +0200 Subject: [PATCH] Refactored Music, NFW, Search, Trello, ClashOfClans Modules ... --- NadekoBot/Classes/NadekoStats.cs | 6 +- .../Administration/Commands/PlayingRotate.cs | 7 +- .../ClashOfClans/ClashOfClans.cs | 0 .../ClashOfClansModule.cs} | 5 +- .../Modules/Help/{Help.cs => HelpModule.cs} | 4 +- .../{Music.cs => Music/MusicModule.cs} | 6 +- .../Modules/{NSFW.cs => NSFW/NSFWModule.cs} | 4 +- .../Modules/Searches/Commands/LoLCommands.cs | 2 +- .../{Searches.cs => SearchesModule.cs} | 4 +- .../{Trello.cs => Trello/TrelloModule.cs} | 67 ++++++++++++------- NadekoBot/NadekoBot.cs | 18 ++--- NadekoBot/NadekoBot.csproj | 14 ++-- 12 files changed, 80 insertions(+), 57 deletions(-) rename NadekoBot/{Classes => Modules}/ClashOfClans/ClashOfClans.cs (100%) rename NadekoBot/Modules/{ClashOfClans.cs => ClashOfClans/ClashOfClansModule.cs} (99%) rename NadekoBot/Modules/Help/{Help.cs => HelpModule.cs} (96%) rename NadekoBot/Modules/{Music.cs => Music/MusicModule.cs} (99%) rename NadekoBot/Modules/{NSFW.cs => NSFW/NSFWModule.cs} (98%) rename NadekoBot/Modules/Searches/{Searches.cs => SearchesModule.cs} (99%) rename NadekoBot/Modules/{Trello.cs => Trello/TrelloModule.cs} (83%) diff --git a/NadekoBot/Classes/NadekoStats.cs b/NadekoBot/Classes/NadekoStats.cs index 48c37053..7483cde0 100644 --- a/NadekoBot/Classes/NadekoStats.cs +++ b/NadekoBot/Classes/NadekoStats.cs @@ -1,8 +1,8 @@ using Discord; using Discord.Commands; using NadekoBot.Extensions; -using NadekoBot.Modules; using NadekoBot.Modules.Administration.Commands; +using NadekoBot.Modules.Music; using System; using System.Collections.Generic; using System.Diagnostics; @@ -139,7 +139,7 @@ namespace NadekoBot public Task LoadStats() => Task.Run(() => { - var songs = Music.MusicPlayers.Count(mp => mp.Value.CurrentSong != null); + var songs = MusicModule.MusicPlayers.Count(mp => mp.Value.CurrentSong != null); var sb = new System.Text.StringBuilder(); sb.AppendLine("`Author: Kwoth` `Library: Discord.Net`"); sb.AppendLine($"`Bot Version: {BotVersion}`"); @@ -154,7 +154,7 @@ namespace NadekoBot sb.AppendLine($"`Message queue size: {NadekoBot.Client.MessageQueue.Count}`"); sb.Append($"`Greeted {ServerGreetCommand.Greeted} times.`"); sb.AppendLine($" `| Playing {songs} songs, ".SnPl(songs) + - $"{Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count)} queued.`"); + $"{MusicModule.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count)} queued.`"); sb.AppendLine($"`Heap: {Heap(false)}`"); statsCache = sb.ToString(); }); diff --git a/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs b/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs index 0ee27a63..81a2d5d0 100644 --- a/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs +++ b/NadekoBot/Modules/Administration/Commands/PlayingRotate.cs @@ -2,6 +2,7 @@ using NadekoBot.Classes.JSONModels; using NadekoBot.Commands; using NadekoBot.Modules.Games.Commands; +using NadekoBot.Modules.Music; using System; using System.Collections.Generic; using System.Linq; @@ -20,10 +21,10 @@ namespace NadekoBot.Modules.Administration.Commands {"%servers%", () => NadekoBot.Client.Servers.Count().ToString()}, {"%users%", () => NadekoBot.Client.Servers.SelectMany(s => s.Users).Count().ToString()}, {"%playing%", () => { - var cnt = Music.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null); + var cnt = MusicModule.MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null); if (cnt != 1) return cnt.ToString(); try { - var mp = Music.MusicPlayers.FirstOrDefault(); + var mp = MusicModule.MusicPlayers.FirstOrDefault(); return mp.Value.CurrentSong.SongInfo.Title; } catch { @@ -31,7 +32,7 @@ namespace NadekoBot.Modules.Administration.Commands } } }, - {"%queued%", () => Music.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}, + {"%queued%", () => MusicModule.MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count).ToString()}, {"%trivia%", () => Trivia.RunningTrivias.Count.ToString()} }; diff --git a/NadekoBot/Classes/ClashOfClans/ClashOfClans.cs b/NadekoBot/Modules/ClashOfClans/ClashOfClans.cs similarity index 100% rename from NadekoBot/Classes/ClashOfClans/ClashOfClans.cs rename to NadekoBot/Modules/ClashOfClans/ClashOfClans.cs diff --git a/NadekoBot/Modules/ClashOfClans.cs b/NadekoBot/Modules/ClashOfClans/ClashOfClansModule.cs similarity index 99% rename from NadekoBot/Modules/ClashOfClans.cs rename to NadekoBot/Modules/ClashOfClans/ClashOfClansModule.cs index 2bc9ab42..7abfe559 100644 --- a/NadekoBot/Modules/ClashOfClans.cs +++ b/NadekoBot/Modules/ClashOfClans/ClashOfClansModule.cs @@ -1,15 +1,14 @@ using Discord.Commands; using Discord.Modules; using NadekoBot.Classes.ClashOfClans; -using NadekoBot.Modules; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Text; -namespace NadekoBot.Commands +namespace NadekoBot.Modules.ClashOfClans { - internal class ClashOfClans : DiscordModule + internal class ClashOfClansModule : DiscordModule { public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.ClashOfClans; diff --git a/NadekoBot/Modules/Help/Help.cs b/NadekoBot/Modules/Help/HelpModule.cs similarity index 96% rename from NadekoBot/Modules/Help/Help.cs rename to NadekoBot/Modules/Help/HelpModule.cs index 05f2e206..07b48524 100644 --- a/NadekoBot/Modules/Help/Help.cs +++ b/NadekoBot/Modules/Help/HelpModule.cs @@ -6,10 +6,10 @@ using System.Linq; namespace NadekoBot.Modules.Help { - internal class Help : DiscordModule + internal class HelpModule : DiscordModule { - public Help() + public HelpModule() { commands.Add(new HelpCommand(this)); } diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music/MusicModule.cs similarity index 99% rename from NadekoBot/Modules/Music.cs rename to NadekoBot/Modules/Music/MusicModule.cs index fcf53359..4aabfb05 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music/MusicModule.cs @@ -13,15 +13,15 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -namespace NadekoBot.Modules +namespace NadekoBot.Modules.Music { - internal class Music : DiscordModule + internal class MusicModule : DiscordModule { public static ConcurrentDictionary MusicPlayers = new ConcurrentDictionary(); public static ConcurrentDictionary DefaultMusicVolumes = new ConcurrentDictionary(); - public Music() + public MusicModule() { // ready for 1.0 //NadekoBot.Client.UserUpdated += (s, e) => diff --git a/NadekoBot/Modules/NSFW.cs b/NadekoBot/Modules/NSFW/NSFWModule.cs similarity index 98% rename from NadekoBot/Modules/NSFW.cs rename to NadekoBot/Modules/NSFW/NSFWModule.cs index b1808555..5252be6f 100644 --- a/NadekoBot/Modules/NSFW.cs +++ b/NadekoBot/Modules/NSFW/NSFWModule.cs @@ -4,9 +4,9 @@ using NadekoBot.Classes; using Newtonsoft.Json.Linq; using System; -namespace NadekoBot.Modules +namespace NadekoBot.Modules.NSFW { - internal class NSFW : DiscordModule + internal class NSFWModule : DiscordModule { private readonly Random rng = new Random(); diff --git a/NadekoBot/Modules/Searches/Commands/LoLCommands.cs b/NadekoBot/Modules/Searches/Commands/LoLCommands.cs index 68db5c36..f1b3d10a 100644 --- a/NadekoBot/Modules/Searches/Commands/LoLCommands.cs +++ b/NadekoBot/Modules/Searches/Commands/LoLCommands.cs @@ -306,7 +306,7 @@ Assists: {general["assists"]} Ban: {general["banRate"]}% await e.Channel.SendMessage(sb.ToString()); } - catch (Exception ex) + catch (Exception) { await e.Channel.SendMessage($":anger: Fail: Champion.gg didsabled ban data until next patch. Sorry for the inconvenience."); } diff --git a/NadekoBot/Modules/Searches/Searches.cs b/NadekoBot/Modules/Searches/SearchesModule.cs similarity index 99% rename from NadekoBot/Modules/Searches/Searches.cs rename to NadekoBot/Modules/Searches/SearchesModule.cs index 847d5926..4295260c 100644 --- a/NadekoBot/Modules/Searches/Searches.cs +++ b/NadekoBot/Modules/Searches/SearchesModule.cs @@ -16,10 +16,10 @@ using System.Net; namespace NadekoBot.Modules.Searches { - internal class Searches : DiscordModule + internal class SearchesModule : DiscordModule { private readonly Random rng; - public Searches() + public SearchesModule() { commands.Add(new LoLCommands(this)); commands.Add(new StreamNotifications(this)); diff --git a/NadekoBot/Modules/Trello.cs b/NadekoBot/Modules/Trello/TrelloModule.cs similarity index 83% rename from NadekoBot/Modules/Trello.cs rename to NadekoBot/Modules/Trello/TrelloModule.cs index 42b233b3..73433a42 100644 --- a/NadekoBot/Modules/Trello.cs +++ b/NadekoBot/Modules/Trello/TrelloModule.cs @@ -1,19 +1,22 @@ -using System; +using Discord.Modules; +using Manatee.Trello; +using Manatee.Trello.ManateeJson; +using NadekoBot.Extensions; +using System; using System.Collections.Generic; using System.Linq; -using Discord.Modules; -using Manatee.Trello.ManateeJson; -using Manatee.Trello; using System.Timers; -using NadekoBot.Extensions; using Action = Manatee.Trello.Action; -namespace NadekoBot.Modules { - internal class Trello : DiscordModule { +namespace NadekoBot.Modules.Trello +{ + internal class TrelloModule : DiscordModule + { private readonly Timer t = new Timer { Interval = 2000 }; public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Trello; - public override void Install(ModuleManager manager) { + public override void Install(ModuleManager manager) + { var client = manager.Client; @@ -29,8 +32,10 @@ namespace NadekoBot.Modules { Board board = null; List last5ActionIDs = null; - t.Elapsed += async (s, e) => { - try { + t.Elapsed += async (s, e) => + { + try + { if (board == null || bound == null) return; //do nothing if there is no bound board @@ -38,22 +43,27 @@ namespace NadekoBot.Modules { var cur5Actions = board.Actions.Take(board.Actions.Count() < 5 ? board.Actions.Count() : 5); var cur5ActionsArray = cur5Actions as Action[] ?? cur5Actions.ToArray(); - if (last5ActionIDs == null) { + if (last5ActionIDs == null) + { last5ActionIDs = cur5ActionsArray.Select(a => a.Id).ToList(); return; } - foreach (var a in cur5ActionsArray.Where(ca => !last5ActionIDs.Contains(ca.Id))) { + foreach (var a in cur5ActionsArray.Where(ca => !last5ActionIDs.Contains(ca.Id))) + { await bound.Send("**--TRELLO NOTIFICATION--**\n" + a.ToString()); } last5ActionIDs.Clear(); last5ActionIDs.AddRange(cur5ActionsArray.Select(a => a.Id)); - } catch (Exception ex) { + } + catch (Exception ex) + { Console.WriteLine("Timer failed " + ex.ToString()); } }; - manager.CreateCommands("trello ", cgb => { + manager.CreateCommands("trello ", cgb => + { cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance); @@ -61,11 +71,15 @@ namespace NadekoBot.Modules { .Alias("j") .Description("Joins a server") .Parameter("code", Discord.Commands.ParameterType.Required) - .Do(async e => { + .Do(async e => + { if (!NadekoBot.IsOwner(e.User.Id)) return; - try { + try + { await (await client.GetInvite(e.GetArg("code"))).Accept(); - } catch (Exception ex) { + } + catch (Exception ex) + { Console.WriteLine(ex.ToString()); } }); @@ -75,23 +89,28 @@ namespace NadekoBot.Modules { "You will receive notifications from your board when something is added or edited." + "\n**Usage**: bind [board_id]") .Parameter("board_id", Discord.Commands.ParameterType.Required) - .Do(async e => { + .Do(async e => + { if (!NadekoBot.IsOwner(e.User.Id)) return; if (bound != null) return; - try { + try + { bound = e.Channel; board = new Board(e.GetArg("board_id").Trim()); board.Refresh(); await e.Channel.SendMessage("Successfully bound to this channel and board " + board.Name); t.Start(); - } catch (Exception ex) { + } + catch (Exception ex) + { Console.WriteLine("Failed to join the board. " + ex.ToString()); } }); cgb.CreateCommand("unbind") .Description("Unbinds a bot from the channel and board.") - .Do(async e => { + .Do(async e => + { if (!NadekoBot.IsOwner(e.User.Id)) return; if (bound == null || bound != e.Channel) return; t.Stop(); @@ -104,7 +123,8 @@ namespace NadekoBot.Modules { cgb.CreateCommand("lists") .Alias("list") .Description("Lists all lists yo ;)") - .Do(async e => { + .Do(async e => + { if (!NadekoBot.IsOwner(e.User.Id)) return; if (bound == null || board == null || bound != e.Channel) return; await e.Channel.SendMessage("Lists for a board '" + board.Name + "'\n" + string.Join("\n", board.Lists.Select(l => "**• " + l.ToString() + "**"))); @@ -113,7 +133,8 @@ namespace NadekoBot.Modules { cgb.CreateCommand("cards") .Description("Lists all cards from the supplied list. You can supply either a name or an index.") .Parameter("list_name", Discord.Commands.ParameterType.Unparsed) - .Do(async e => { + .Do(async e => + { if (!NadekoBot.IsOwner(e.User.Id)) return; if (bound == null || board == null || bound != e.Channel || e.GetArg("list_name") == null) return; diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 3b77114a..7ca85d8f 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -3,19 +3,21 @@ using Discord.Audio; using Discord.Commands; using Discord.Modules; using NadekoBot.Classes.JSONModels; -using NadekoBot.Commands; using NadekoBot.Commands.Help.Commands; -using NadekoBot.Modules; using NadekoBot.Modules.Administration; +using NadekoBot.Modules.ClashOfClans; using NadekoBot.Modules.Conversations; using NadekoBot.Modules.Gambling; using NadekoBot.Modules.Games; using NadekoBot.Modules.Games.Commands; using NadekoBot.Modules.Help; +using NadekoBot.Modules.Music; +using NadekoBot.Modules.NSFW; using NadekoBot.Modules.Permissions; using NadekoBot.Modules.Pokemon; using NadekoBot.Modules.Searches; using NadekoBot.Modules.Translator; +using NadekoBot.Modules.Trello; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -170,19 +172,19 @@ namespace NadekoBot //install modules modules.Add(new AdministrationModule(), "Administration", ModuleFilter.None); - modules.Add(new Help(), "Help", ModuleFilter.None); + modules.Add(new HelpModule(), "Help", ModuleFilter.None); modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None); modules.Add(new Conversations(), "Conversations", ModuleFilter.None); modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None); modules.Add(new GamesModule(), "Games", ModuleFilter.None); - modules.Add(new Music(), "Music", ModuleFilter.None); - modules.Add(new Searches(), "Searches", ModuleFilter.None); - modules.Add(new NSFW(), "NSFW", ModuleFilter.None); - modules.Add(new ClashOfClans(), "ClashOfClans", ModuleFilter.None); + modules.Add(new MusicModule(), "Music", ModuleFilter.None); + modules.Add(new SearchesModule(), "Searches", ModuleFilter.None); + modules.Add(new NSFWModule(), "NSFW", ModuleFilter.None); + modules.Add(new ClashOfClansModule(), "ClashOfClans", ModuleFilter.None); modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None); modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None); if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey)) - modules.Add(new Trello(), "Trello", ModuleFilter.None); + modules.Add(new TrelloModule(), "Trello", ModuleFilter.None); //run the bot Client.ExecuteAndWait(async () => diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 85904a90..eaea0f52 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -120,7 +120,7 @@ - + @@ -167,7 +167,7 @@ - + @@ -197,22 +197,22 @@ - - + + - + - + - +