Possible fix for monthly patreon creds auto update

This commit is contained in:
Kwoth
2022-01-07 06:39:49 +01:00
parent 73de20b615
commit a6330119e8

View File

@@ -4,6 +4,7 @@ using NadekoBot.Modules.Gambling.Services;
using NadekoBot.Modules.Utility.Common.Patreon; using NadekoBot.Modules.Utility.Common.Patreon;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using StackExchange.Redis; using StackExchange.Redis;
using System.Net;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text.Json; using System.Text.Json;
@@ -112,15 +113,22 @@ public class PatreonRewardsService : INService
if (DateTime.UtcNow.Day < 5) if (DateTime.UtcNow.Day < 5)
return; return;
// if the user has the necessary patreon creds if (string.IsNullOrWhiteSpace(creds.Patreon.CampaignId))
// and the access token expired or doesn't exist
// -> update access token
if (!HasPatreonCreds(creds))
return; return;
if (LastAccessTokenUpdate(creds).Month < DateTime.UtcNow.Month var lastUpdate = LastAccessTokenUpdate(creds);
var now = DateTime.UtcNow;
if (lastUpdate.Year != now.Year
|| lastUpdate.Month != now.Month
|| string.IsNullOrWhiteSpace(creds.Patreon.AccessToken)) || string.IsNullOrWhiteSpace(creds.Patreon.AccessToken))
{ {
// if the user has the necessary patreon creds
// and the access token expired or doesn't exist
// -> update access token
if (!HasPatreonCreds(creds))
return;
var success = await UpdateAccessToken(creds); var success = await UpdateAccessToken(creds);
if (!success) if (!success)
return; return;
@@ -178,6 +186,12 @@ public class PatreonRewardsService : INService
foreach (var pledge in userData) await ClaimReward(pledge.UserId, pledge.PatreonUserId, pledge.EntitledTo); foreach (var pledge in userData) await ClaimReward(pledge.UserId, pledge.PatreonUserId, pledge.EntitledTo);
} }
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)
{
Log.Warning("Patreon credentials invalid or expired. I will try to refresh them during the next run");
var db = _redis.GetDatabase();
await db.KeyDeleteAsync($"{creds.RedisKey()}_patreon_update");
}
catch (Exception ex) catch (Exception ex)
{ {
Log.Warning(ex, "Error refreshing patreon pledges"); Log.Warning(ex, "Error refreshing patreon pledges");