Patreon rewards fix finished

This commit is contained in:
Master Kwoth 2017-06-24 05:24:43 +02:00
parent a8f2ca60c2
commit 7ad5c5e02b
3 changed files with 13 additions and 3 deletions

View File

@ -34,7 +34,9 @@ namespace NadekoBot.Modules.Utility
[OwnerOnly] [OwnerOnly]
public async Task PatreonRewardsReload() public async Task PatreonRewardsReload()
{ {
await _patreon.LoadPledges().ConfigureAwait(false); if (string.IsNullOrWhiteSpace(_creds.PatreonAccessToken))
return;
await _patreon.RefreshPledges(true).ConfigureAwait(false);
await Context.Channel.SendConfirmAsync("👌").ConfigureAwait(false); await Context.Channel.SendConfirmAsync("👌").ConfigureAwait(false);
} }
@ -44,6 +46,7 @@ namespace NadekoBot.Modules.Utility
{ {
if (string.IsNullOrWhiteSpace(_creds.PatreonAccessToken)) if (string.IsNullOrWhiteSpace(_creds.PatreonAccessToken))
return; return;
if (DateTime.UtcNow.Day < 5) if (DateTime.UtcNow.Day < 5)
{ {
await ReplyErrorLocalized("clpa_too_early").ConfigureAwait(false); await ReplyErrorLocalized("clpa_too_early").ConfigureAwait(false);

View File

@ -163,7 +163,7 @@ namespace NadekoBot
var repeaterService = new MessageRepeaterService(this, Client, AllGuildConfigs); var repeaterService = new MessageRepeaterService(this, Client, AllGuildConfigs);
//var converterService = new ConverterService(Db); //var converterService = new ConverterService(Db);
var commandMapService = new CommandMapService(AllGuildConfigs); var commandMapService = new CommandMapService(AllGuildConfigs);
var patreonRewardsService = new PatreonRewardsService(Credentials, Db, Currency); var patreonRewardsService = new PatreonRewardsService(Credentials, Db, Currency, Client);
var verboseErrorsService = new VerboseErrorsService(AllGuildConfigs, Db, CommandHandler, helpService); var verboseErrorsService = new VerboseErrorsService(AllGuildConfigs, Db, CommandHandler, helpService);
var pruneService = new PruneService(); var pruneService = new PruneService();
#endregion #endregion

View File

@ -32,6 +32,8 @@ namespace NadekoBot.Services.Utility
private readonly DbService _db; private readonly DbService _db;
private readonly CurrencyService _currency; private readonly CurrencyService _currency;
private readonly string cacheFileName = "./patreon-rewards.json";
public PatreonRewardsService(IBotCredentials creds, DbService db, CurrencyService currency, public PatreonRewardsService(IBotCredentials creds, DbService db, CurrencyService currency,
DiscordSocketClient client) DiscordSocketClient client)
{ {
@ -41,7 +43,8 @@ namespace NadekoBot.Services.Utility
if (string.IsNullOrWhiteSpace(creds.PatreonAccessToken)) if (string.IsNullOrWhiteSpace(creds.PatreonAccessToken))
return; return;
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
Updater = new Timer(async (load) => await RefreshPledges((bool)load), client.ShardId == 0, TimeSpan.Zero, Interval); Updater = new Timer(async (load) => await RefreshPledges((bool)load),
client.ShardId == 0, client.ShardId == 0 ? TimeSpan.Zero : TimeSpan.FromMinutes(2), Interval);
} }
public async Task RefreshPledges(bool shouldLoad) public async Task RefreshPledges(bool shouldLoad)
@ -93,11 +96,15 @@ namespace NadekoBot.Services.Utility
{ {
getPledgesLocker.Release(); getPledgesLocker.Release();
} }
Console.WriteLine("Pledges loaded from the website");
} }
else else
{ {
if(File.Exists(cacheFileName))
Pledges = JsonConvert.DeserializeObject<PatreonUserAndReward[]>(File.ReadAllText("./patreon_rewards.json")) Pledges = JsonConvert.DeserializeObject<PatreonUserAndReward[]>(File.ReadAllText("./patreon_rewards.json"))
.ToImmutableArray(); .ToImmutableArray();
Console.WriteLine("Pledges loaded from the file");
} }
} }