Started adding trello support. Maybe will rewrite.

This commit is contained in:
Kwoth 2016-01-17 22:24:51 +01:00
parent 7454f5611d
commit 9e870cfc87
4 changed files with 155 additions and 0 deletions

View File

@ -10,6 +10,7 @@
public bool Crawl; public bool Crawl;
public string ParseID; public string ParseID;
public string ParseKey; public string ParseKey;
public string TrelloAppKey;
} }
public class AnimeResult public class AnimeResult
{ {

136
NadekoBot/Modules/Trello.cs Normal file
View File

@ -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<string> 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<Manatee.Trello.Action> 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<string>();
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.");
});
});
}
}
}

View File

@ -20,11 +20,13 @@ namespace NadekoBot
public static string GoogleAPIKey = null; public static string GoogleAPIKey = null;
public static ulong OwnerID; public static ulong OwnerID;
public static string password; public static string password;
public static string TrelloAppKey;
static void Main() static void Main()
{ {
//load credentials from credentials.json //load credentials from credentials.json
Credentials c; Credentials c;
bool trelloLoaded = false;
try try
{ {
c = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText("credentials.json")); c = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText("credentials.json"));
@ -34,6 +36,12 @@ namespace NadekoBot
} else { } else {
GoogleAPIKey = c.GoogleAPIKey; 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; OwnerID = c.OwnerID;
password = c.Password; password = c.Password;
} }
@ -86,6 +94,8 @@ namespace NadekoBot
modules.Install(new Games(), "Games", FilterType.Unrestricted); modules.Install(new Games(), "Games", FilterType.Unrestricted);
modules.Install(new Music(), "Music", FilterType.Unrestricted); modules.Install(new Music(), "Music", FilterType.Unrestricted);
modules.Install(new Searches(), "Searches", FilterType.Unrestricted); modules.Install(new Searches(), "Searches", FilterType.Unrestricted);
if(trelloLoaded)
modules.Install(new Trello(), "Trello", FilterType.Unrestricted);
//run the bot //run the bot
client.Run(async () => client.Run(async () =>
@ -97,6 +107,7 @@ namespace NadekoBot
Console.ReadKey(); Console.ReadKey();
} }
static bool repliedRecently = false; static bool repliedRecently = false;
private static async void Client_MessageReceived(object sender, MessageEventArgs e) { private static async void Client_MessageReceived(object sender, MessageEventArgs e) {
if (e.Server != null) return; if (e.Server != null) return;
try { try {

View File

@ -1,5 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Manatee.Json" version="3.2.1" targetFramework="net452" />
<package id="Manatee.StateMachine" version="1.1.2" targetFramework="net452" />
<package id="Manatee.Trello" version="1.8.2" targetFramework="net452" />
<package id="Manatee.Trello.ManateeJson" version="1.4.0" targetFramework="net452" />
<package id="Manatee.Trello.WebApi" version="1.0.1" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" /> <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="Parse" version="1.6.2" targetFramework="net452" /> <package id="Parse" version="1.6.2" targetFramework="net452" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" /> <package id="RestSharp" version="105.2.3" targetFramework="net452" />