From 7ad5c5e02bf5cebf16141247f31e5eacdce0fd75 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 24 Jun 2017 05:24:43 +0200 Subject: [PATCH] Patreon rewards fix finished --- .../Modules/Utility/Commands/PatreonCommands.cs | 5 ++++- src/NadekoBot/NadekoBot.cs | 2 +- src/NadekoBot/Services/Utility/PatreonRewardsService.cs | 9 ++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/NadekoBot/Modules/Utility/Commands/PatreonCommands.cs b/src/NadekoBot/Modules/Utility/Commands/PatreonCommands.cs index 509419fd..293e2c42 100644 --- a/src/NadekoBot/Modules/Utility/Commands/PatreonCommands.cs +++ b/src/NadekoBot/Modules/Utility/Commands/PatreonCommands.cs @@ -34,7 +34,9 @@ namespace NadekoBot.Modules.Utility [OwnerOnly] 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); } @@ -44,6 +46,7 @@ namespace NadekoBot.Modules.Utility { if (string.IsNullOrWhiteSpace(_creds.PatreonAccessToken)) return; + if (DateTime.UtcNow.Day < 5) { await ReplyErrorLocalized("clpa_too_early").ConfigureAwait(false); diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 2ee3d1b3..26cf6d91 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -163,7 +163,7 @@ namespace NadekoBot var repeaterService = new MessageRepeaterService(this, Client, AllGuildConfigs); //var converterService = new ConverterService(Db); 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 pruneService = new PruneService(); #endregion diff --git a/src/NadekoBot/Services/Utility/PatreonRewardsService.cs b/src/NadekoBot/Services/Utility/PatreonRewardsService.cs index f3811c6d..8b463f7e 100644 --- a/src/NadekoBot/Services/Utility/PatreonRewardsService.cs +++ b/src/NadekoBot/Services/Utility/PatreonRewardsService.cs @@ -32,6 +32,8 @@ namespace NadekoBot.Services.Utility private readonly DbService _db; private readonly CurrencyService _currency; + private readonly string cacheFileName = "./patreon-rewards.json"; + public PatreonRewardsService(IBotCredentials creds, DbService db, CurrencyService currency, DiscordSocketClient client) { @@ -41,7 +43,8 @@ namespace NadekoBot.Services.Utility if (string.IsNullOrWhiteSpace(creds.PatreonAccessToken)) return; _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) @@ -93,11 +96,15 @@ namespace NadekoBot.Services.Utility { getPledgesLocker.Release(); } + Console.WriteLine("Pledges loaded from the website"); } else { + if(File.Exists(cacheFileName)) Pledges = JsonConvert.DeserializeObject(File.ReadAllText("./patreon_rewards.json")) .ToImmutableArray(); + + Console.WriteLine("Pledges loaded from the file"); } }