From 9e870cfc87ac421189ee3bb807d6f5f51a40991a Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 17 Jan 2016 22:24:51 +0100 Subject: [PATCH] Started adding trello support. Maybe will rewrite. --- NadekoBot/Classes/_JSONModels.cs | 1 + NadekoBot/Modules/Trello.cs | 136 +++++++++++++++++++++++++++++++ NadekoBot/NadekoBot.cs | 11 +++ NadekoBot/packages.config | 7 ++ 4 files changed, 155 insertions(+) create mode 100644 NadekoBot/Modules/Trello.cs diff --git a/NadekoBot/Classes/_JSONModels.cs b/NadekoBot/Classes/_JSONModels.cs index 8a5b8467..53b9881c 100644 --- a/NadekoBot/Classes/_JSONModels.cs +++ b/NadekoBot/Classes/_JSONModels.cs @@ -10,6 +10,7 @@ public bool Crawl; public string ParseID; public string ParseKey; + public string TrelloAppKey; } public class AnimeResult { diff --git a/NadekoBot/Modules/Trello.cs b/NadekoBot/Modules/Trello.cs new file mode 100644 index 00000000..a922877d --- /dev/null +++ b/NadekoBot/Modules/Trello.cs @@ -0,0 +1,136 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Modules; +using Manatee.Trello.ManateeJson; +using Manatee.Trello; +using System.Timers; + +namespace NadekoBot.Modules { + class Trello : DiscordModule { + public override void Install(ModuleManager manager) { + + var client = manager.Client; + + var serializer = new ManateeSerializer(); + TrelloConfiguration.Serializer = serializer; + TrelloConfiguration.Deserializer = serializer; + TrelloConfiguration.JsonFactory = new ManateeFactory(); + TrelloConfiguration.RestClientProvider = new Manatee.Trello.WebApi.WebApiClientProvider(); + TrelloAuthorization.Default.AppKey = NadekoBot.TrelloAppKey; + //TrelloAuthorization.Default.UserToken = "[your user token]"; + + Discord.Channel bound = null; + Board board = null; + + Timer t = new Timer(); + t.Interval = 2000; + List last5ActionIDs = null; + t.Elapsed += async (s, e) => { + try { + if (board == null || bound == null) + return; //do nothing if there is no bound board + + board.Refresh(); + IEnumerable cur5Actions; + if (board.Actions.Count() < 5) + cur5Actions = board.Actions.Take(board.Actions.Count()); + else + cur5Actions = board.Actions.Take(5); + + if (last5ActionIDs == null) { + last5ActionIDs = new List(); + foreach (var a in cur5Actions) + last5ActionIDs.Add(a.Id); + return; + } + foreach (var a in cur5Actions.Where(ca => !last5ActionIDs.Contains(ca.Id))) { + await bound.Send("**--TRELLO NOTIFICATION--**\n" + a.ToString()); + } + last5ActionIDs.Clear(); + foreach (var a in cur5Actions) + last5ActionIDs.Add(a.Id); + } catch (Exception ex) { + Console.WriteLine("Timer failed " + ex.ToString()); + } + }; + + manager.CreateCommands("", cgb => { + cgb.CreateCommand("join") + .Alias("j") + .Description("Joins a server") + .Parameter("code", Discord.Commands.ParameterType.Required) + .Do(async e => { + if (e.User.Id != NadekoBot.OwnerID) return; + try { + await (await client.GetInvite(e.GetArg("code"))).Accept(); + } catch (Exception ex) { + Console.WriteLine(ex.ToString()); + } + }); + + cgb.CreateCommand("bind") + .Description("Bind a trello bot to a certain channel. Bot will operate only in 1 channel at a time") + .Parameter("board_id", Discord.Commands.ParameterType.Required) + .Do(async e => { + if (e.User.Id != NadekoBot.OwnerID) return; + if (bound != null) return; + try { + bound = e.Channel; + board = new Board(e.GetArg("board_id").Trim()); + board.Refresh(); + await e.Send("Successfully bound to this channel and board " + board.Name); + t.Start(); + } 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 => { + if (e.User.Id != NadekoBot.OwnerID) return; + if (bound == null || bound != e.Channel) return; + t.Stop(); + bound = null; + board = null; + await e.Send("Successfully unbound trello from this channel."); + + }); + + cgb.CreateCommand("lists") + .Alias("list") + .Description("Lists all lists yo ;)") + .Do(async e => { + if (e.User.Id != NadekoBot.OwnerID) return; + if (bound == null || board == null || bound != e.Channel) return; + await e.Send("Lists for a board '" + board.Name + "'\n" + string.Join("\n", board.Lists.Select(l => "**• " + l.ToString() + "**"))); + }); + + 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 => { + if (e.User.Id != NadekoBot.OwnerID) return; + if (bound == null || board == null || bound != e.Channel || e.GetArg("list_name") == null) return; + + int num; + var success = int.TryParse(e.GetArg("list_name"), out num); + List list = null; + if (success && num <= board.Lists.Count() && num > 0) + list = board.Lists[num - 1]; + else + list = board.Lists.Where(l => l.Name == e.GetArg("list_name")).FirstOrDefault(); + + + if (list != null) + await e.Send("There are " + list.Cards.Count() + " cards in a **" + list.Name + "** list\n" + string.Join("\n", list.Cards.Select(c => "**• " + c.ToString() + "**"))); + else + await e.Send("No such list."); + }); + }); + } + } +} diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index 2031bd5f..8444b4c5 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -20,11 +20,13 @@ namespace NadekoBot public static string GoogleAPIKey = null; public static ulong OwnerID; public static string password; + public static string TrelloAppKey; static void Main() { //load credentials from credentials.json Credentials c; + bool trelloLoaded = false; try { c = JsonConvert.DeserializeObject(File.ReadAllText("credentials.json")); @@ -34,6 +36,12 @@ namespace NadekoBot } else { GoogleAPIKey = c.GoogleAPIKey; } + if (c.TrelloAppKey == null || c.TrelloAppKey == "") { + Console.WriteLine("No trello appkey found. You will not be able to use trello commands."); + } else { + TrelloAppKey = c.TrelloAppKey; + trelloLoaded = true; + } OwnerID = c.OwnerID; password = c.Password; } @@ -86,6 +94,8 @@ namespace NadekoBot modules.Install(new Games(), "Games", FilterType.Unrestricted); modules.Install(new Music(), "Music", FilterType.Unrestricted); modules.Install(new Searches(), "Searches", FilterType.Unrestricted); + if(trelloLoaded) + modules.Install(new Trello(), "Trello", FilterType.Unrestricted); //run the bot client.Run(async () => @@ -97,6 +107,7 @@ namespace NadekoBot Console.ReadKey(); } static bool repliedRecently = false; + private static async void Client_MessageReceived(object sender, MessageEventArgs e) { if (e.Server != null) return; try { diff --git a/NadekoBot/packages.config b/NadekoBot/packages.config index 3ba870e9..61a5c710 100644 --- a/NadekoBot/packages.config +++ b/NadekoBot/packages.config @@ -1,5 +1,12 @@  + + + + + + +