Added a big part of basic patreon stuff, but can't activate it yet, waiting for their email reply to check some things

This commit is contained in:
Kwoth 2017-03-28 08:34:35 +02:00
parent 2f69535565
commit ec24ac3886
5 changed files with 271 additions and 95 deletions

View File

@ -1,78 +1,110 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Discord.Commands;
using Discord;
using NadekoBot.Attributes;
using NadekoBot.Modules.Utility.Models;
using Newtonsoft.Json;
//using System.Collections.Generic;
//using System.Linq;
//using System.Net.Http;
//using System.Threading.Tasks;
//using Discord.Commands;
//using NadekoBot.Attributes;
//using NadekoBot.Modules.Utility.Models;
//using Newtonsoft.Json;
//using System.Threading;
//using System;
//using System.Collections.Immutable;
namespace NadekoBot.Modules.Utility
{
public partial class Utility
{
//[Group]
//public class PatreonCommands : NadekoSubmodule
//{
// [NadekoCommand, Usage, Description, Aliases]
// [RequireContext(ContextType.Guild)]
// public async Task ClaimPatreonRewards([Remainder] string arg)
// {
// var pledges = await GetPledges2();
// }
//namespace NadekoBot.Modules.Utility
//{
// public partial class Utility
// {
// [Group]
// public class PatreonCommands : NadekoSubmodule
// {
// [NadekoCommand, Usage, Description, Aliases]
// public async Task ClaimPatreonRewards()
// {
// var patreon = PatreonThingy.Instance;
// private static async Task<Pledge[]> GetPledges()
// {
// var pledges = new List<Pledge>();
// using (var http = new HttpClient())
// {
// http.DefaultRequestHeaders.Clear();
// http.DefaultRequestHeaders.Add("Authorization", "Bearer " + NadekoBot.Credentials.PatreonAccessToken);
// var data = new PatreonData()
// {
// Links = new Links()
// {
// Next = "https://api.patreon.com/oauth2/api/campaigns/334038/pledges"
// }
// };
// do
// {
// var res =
// await http.GetStringAsync(data.Links.Next)
// .ConfigureAwait(false);
// data = JsonConvert.DeserializeObject<PatreonData>(res);
// pledges.AddRange(data.Data);
// } while (!string.IsNullOrWhiteSpace(data.Links.Next));
// }
// return pledges.Where(x => string.IsNullOrWhiteSpace(x.Attributes.declined_since)).ToArray();
// }
// var pledges = (await patreon.GetPledges().ConfigureAwait(false))
// .OrderByDescending(x => x.Reward.attributes.amount_cents);
// private static async Task<Pledge[]> GetPledges2()
// {
// var pledges = new List<Pledge>();
// using (var http = new HttpClient())
// {
// http.DefaultRequestHeaders.Clear();
// http.DefaultRequestHeaders.Add("Authorization", "Bearer " + NadekoBot.Credentials.PatreonAccessToken);
// var data = new PatreonData()
// {
// Links = new Links()
// {
// Next = "https://api.patreon.com/oauth2/api/current_user/campaigns?include=pledges"
// }
// };
// do
// {
// var res =
// await http.GetStringAsync(data.Links.Next)
// .ConfigureAwait(false);
// data = JsonConvert.DeserializeObject<PatreonData>(res);
// pledges.AddRange(data.Data);
// } while (!string.IsNullOrWhiteSpace(data.Links.Next));
// }
// return pledges.Where(x => string.IsNullOrWhiteSpace(x.Attributes.declined_since)).ToArray();
// }
//}
}
}
// if (pledges == null)
// {
// await ReplyErrorLocalized("pledges_loading").ConfigureAwait(false);
// return;
// }
// }
// }
// public class PatreonThingy
// {
// public static PatreonThingy _instance = new PatreonThingy();
// public static PatreonThingy Instance => _instance;
// private readonly SemaphoreSlim getPledgesLocker = new SemaphoreSlim(1, 1);
// private ImmutableArray<PatreonUserAndReward> pledges;
// static PatreonThingy() { }
// public async Task<ImmutableArray<PatreonUserAndReward>> GetPledges()
// {
// try
// {
// await LoadPledges().ConfigureAwait(false);
// return pledges;
// }
// catch (OperationCanceledException)
// {
// return pledges;
// }
// }
// public async Task LoadPledges()
// {
// await getPledgesLocker.WaitAsync(1000).ConfigureAwait(false);
// try
// {
// var rewards = new List<PatreonPledge>();
// var users = new List<PatreonUser>();
// using (var http = new HttpClient())
// {
// http.DefaultRequestHeaders.Clear();
// http.DefaultRequestHeaders.Add("Authorization", "Bearer " + NadekoBot.Credentials.PatreonAccessToken);
// var data = new PatreonData()
// {
// Links = new PatreonDataLinks()
// {
// next = "https://api.patreon.com/oauth2/api/campaigns/334038/pledges"
// }
// };
// do
// {
// var res = await http.GetStringAsync(data.Links.next)
// .ConfigureAwait(false);
// data = JsonConvert.DeserializeObject<PatreonData>(res);
// var pledgers = data.Data.Where(x => x["type"].ToString() == "pledge");
// rewards.AddRange(pledgers.Select(x => JsonConvert.DeserializeObject<PatreonPledge>(x.ToString()))
// .Where(x => x.attributes.declined_since == null));
// users.AddRange(data.Included
// .Where(x => x["type"].ToString() == "user")
// .Select(x => JsonConvert.DeserializeObject<PatreonUser>(x.ToString())));
// } while (!string.IsNullOrWhiteSpace(data.Links.next));
// }
// pledges = rewards.Join(users, (r) => r.relationships?.patron?.data?.id, (u) => u.id, (x, y) => new PatreonUserAndReward()
// {
// User = y,
// Reward = x,
// }).ToImmutableArray();
// }
// finally
// {
// var _ = Task.Run(async () =>
// {
// await Task.Delay(TimeSpan.FromMinutes(5)).ConfigureAwait(false);
// getPledgesLocker.Release();
// });
// }
// }
// }
// }
//}

View File

@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,32 +7,22 @@ using System.Threading.Tasks;
namespace NadekoBot.Modules.Utility.Models
{
public class PatreonData
{
public Pledge[] Data { get; set; }
public Links Links { get; set; }
public JObject[] Included { get; set; }
public JObject[] Data { get; set; }
public PatreonDataLinks Links { get; set; }
}
public class Attributes
public class PatreonDataLinks
{
public int amount_cents { get; set; }
public string created_at { get; set; }
public string declined_since { get; set; }
public bool is_twitch_pledge { get; set; }
public bool patron_pays_fees { get; set; }
public int pledge_cap_cents { get; set; }
public string first { get; set; }
public string next { get; set; }
}
public class Pledge
public class PatreonUserAndReward
{
public Attributes Attributes { get; set; }
public int Id { get; set; }
}
public class Links
{
public string First { get; set; }
public string Next { get; set; }
public PatreonUser User { get; set; }
public PatreonPledge Reward { get; set; }
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Utility.Models
{
public class Attributes
{
public int amount_cents { get; set; }
public string created_at { get; set; }
public object declined_since { get; set; }
public bool is_twitch_pledge { get; set; }
public bool patron_pays_fees { get; set; }
public int pledge_cap_cents { get; set; }
}
public class Address
{
public object data { get; set; }
}
public class Data
{
public string id { get; set; }
public string type { get; set; }
}
public class Links
{
public string related { get; set; }
}
public class Creator
{
public Data data { get; set; }
public Links links { get; set; }
}
public class Patron
{
public Data data { get; set; }
public Links links { get; set; }
}
public class Reward
{
public Data data { get; set; }
public Links links { get; set; }
}
public class Relationships
{
public Address address { get; set; }
public Creator creator { get; set; }
public Patron patron { get; set; }
public Reward reward { get; set; }
}
public class PatreonPledge
{
public Attributes attributes { get; set; }
public string id { get; set; }
public Relationships relationships { get; set; }
public string type { get; set; }
}
}

View File

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Utility.Models
{
public class DiscordConnection
{
public string user_id { get; set; }
}
public class SocialConnections
{
public object deviantart { get; set; }
public DiscordConnection discord { get; set; }
public object facebook { get; set; }
public object spotify { get; set; }
public object twitch { get; set; }
public object twitter { get; set; }
public object youtube { get; set; }
}
public class UserAttributes
{
public string about { get; set; }
public string created { get; set; }
public object discord_id { get; set; }
public string email { get; set; }
public object facebook { get; set; }
public object facebook_id { get; set; }
public string first_name { get; set; }
public string full_name { get; set; }
public int gender { get; set; }
public bool has_password { get; set; }
public string image_url { get; set; }
public bool is_deleted { get; set; }
public bool is_nuked { get; set; }
public bool is_suspended { get; set; }
public string last_name { get; set; }
public SocialConnections social_connections { get; set; }
public int status { get; set; }
public string thumb_url { get; set; }
public object twitch { get; set; }
public string twitter { get; set; }
public string url { get; set; }
public string vanity { get; set; }
public object youtube { get; set; }
}
public class Campaign
{
public Data data { get; set; }
public Links links { get; set; }
}
public class UserRelationships
{
public Campaign campaign { get; set; }
}
public class PatreonUser
{
public UserAttributes attributes { get; set; }
public string id { get; set; }
public UserRelationships relationships { get; set; }
public string type { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Models
{
public class PatreonRewards : DbEntity
{
public ulong UserId { get; set; }
public ulong PledgeCents { get; set; }
public ulong Awarded { get; set; }
}
}