Possible fix for patreon auto-creds update

This commit is contained in:
Kwoth
2022-01-07 06:28:58 +01:00
parent c0ce22a6b7
commit c3ba805acf

View File

@@ -4,7 +4,9 @@ using NadekoBot.Services.Database.Models;
using NadekoBot.Modules.Utility.Common.Patreon;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
@@ -146,15 +148,22 @@ namespace NadekoBot.Modules.Utility.Services
if (DateTime.UtcNow.Day < 5)
return;
// if the user has the necessary patreon creds
// and the access token expired or doesn't exist
// -> update access token
if (!HasPatreonCreds(creds))
if (string.IsNullOrWhiteSpace(creds.Patreon.CampaignId))
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))
{
// 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);
if (!success)
return;
@@ -210,11 +219,18 @@ namespace NadekoBot.Modules.Utility.Services
})
.ToList();
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)
{
Log.Warning(ex, "Error refreshing patreon pledges");
@@ -223,7 +239,6 @@ namespace NadekoBot.Modules.Utility.Services
{
getPledgesLocker.Release();
}
}
public async Task<int> ClaimReward(ulong userId, string patreonUserId, int cents)