Patreon Access and Refresh Tokens should now be automatically updated

This commit is contained in:
Kwoth
2021-11-17 18:45:49 +00:00
parent d55ce7accc
commit 06c20c6fa4
14 changed files with 375 additions and 267 deletions

View File

@@ -10,7 +10,14 @@ using Serilog;
namespace NadekoBot.Services
{
public sealed class BotCredsProvider
public interface IBotCredsProvider
{
public void Reload();
public IBotCredentials GetCreds();
public void ModifyCredsFile(Action<Creds> func);
}
public sealed class BotCredsProvider : IBotCredsProvider
{
private readonly int? _totalShards;
private const string _credsFileName = "creds.yml";
@@ -27,7 +34,7 @@ namespace NadekoBot.Services
private readonly object reloadLock = new object();
private void Reload()
public void Reload()
{
lock (reloadLock)
{
@@ -102,6 +109,19 @@ namespace NadekoBot.Services
Reload();
}
public void ModifyCredsFile(Action<Creds> func)
{
var ymlData = File.ReadAllText(_credsFileName);
var creds = Yaml.Deserializer.Deserialize<Creds>(ymlData);
func(creds);
ymlData = Yaml.Serializer.Serialize(creds);
File.WriteAllText(_credsFileName, ymlData);
Reload();
}
/// <summary>
/// Checks if there's a V2 credentials file present, loads it if it exists,
/// converts it to new model, and saves it to YAML. Also backs up old credentials to credentials.json.bak
@@ -157,6 +177,6 @@ namespace NadekoBot.Services
}
public Creds GetCreds() => _creds;
public IBotCredentials GetCreds() => _creds;
}
}