- Updated creds_example.yml

- Added credentials.json -> creds.yml migration
- Migrated credentials.json fields are:
  - Token
  - OwnerIds
  - GoogleApiKey
  - OsuApiKey
  - CleverbotApiKey
  - TotalShards (although you should set this from Coordinator's coord.yml TotalShards, so this setting will usually have no effect)
  - PatreonAccessToken and PatreonCampaignId
  - VotesUrl and VotesToken
  - BotListToken
  - RedisOptions
  - LocationIqApiKey and TimezoneDbApiKey
  - CoinmarketcapApiKey
This commit is contained in:
Kwoth
2021-07-12 21:03:43 +02:00
parent 0b71e9c28f
commit 8fb7239100
3 changed files with 66 additions and 10 deletions

View File

@@ -1,6 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using NadekoBot.Common.Yml; using NadekoBot.Common.Yml;
using NadekoBot.Services;
using YamlDotNet.Serialization; using YamlDotNet.Serialization;
namespace NadekoBot.Common namespace NadekoBot.Common
@@ -79,8 +78,10 @@ Change only if you've changed the coordinator address or port.")]
[YamlIgnore] [YamlIgnore]
public string PatreonAccessToken => Patreon?.AccessToken; public string PatreonAccessToken => Patreon?.AccessToken;
public string VotesUrl { get; set; } [YamlIgnore]
public string VotesToken { get; set; } public string VotesUrl => Votes?.Url;
[YamlIgnore]
public string VotesToken => Votes.Key;
[Comment(@"Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key)")] [Comment(@"Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key)")]
public string RapidApiKey { get; set; } public string RapidApiKey { get; set; }
@@ -185,6 +186,9 @@ Windows default
public string VotesToken { get; set; } = string.Empty; public string VotesToken { get; set; } = string.Empty;
public string VotesUrl { get; set; } = string.Empty; public string VotesUrl { get; set; } = string.Empty;
public string RedisOptions { get; set; } = string.Empty; public string RedisOptions { get; set; } = string.Empty;
public string LocationIqApiKey { get; set; } = string.Empty;
public string TimezoneDbApiKey { get; set; } = string.Empty;
public string CoinmarketcapApiKey { get; set; } = string.Empty;
public class RestartConfig public class RestartConfig
{ {

View File

@@ -1,9 +1,11 @@
using System; using System;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System.IO; using System.IO;
using System.Linq;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using NadekoBot.Common; using NadekoBot.Common;
using NadekoBot.Common.Yml; using NadekoBot.Common.Yml;
using Newtonsoft.Json;
using Serilog; using Serilog;
namespace NadekoBot.Services namespace NadekoBot.Services
@@ -13,15 +15,18 @@ namespace NadekoBot.Services
{ {
private readonly int? _totalShards; private readonly int? _totalShards;
private const string _credsFileName = "creds.yml"; private const string _credsFileName = "creds.yml";
private string CredsPath => Path.Combine(Directory.GetCurrentDirectory(), _credsFileName);
private const string _credsExampleFileName = "creds_example.yml"; private const string _credsExampleFileName = "creds_example.yml";
private string CredsExamplePath => Path.Combine(Directory.GetCurrentDirectory(), _credsExampleFileName);
private string _oldCredsJsonFilename = Path.Combine(Directory.GetCurrentDirectory(), "credentials.json"); private string CredsPath => Path.Combine(Directory.GetCurrentDirectory(), _credsFileName);
private string CredsExamplePath => Path.Combine(Directory.GetCurrentDirectory(), _credsExampleFileName);
private string OldCredsJsonPath => Path.Combine(Directory.GetCurrentDirectory(), "credentials.json");
private string OldCredsJsonBackupPath => Path.Combine(Directory.GetCurrentDirectory(), "credentials.json.bak");
private Creds _creds = new Creds(); private Creds _creds = new Creds();
private IConfigurationRoot _config; private IConfigurationRoot _config;
private readonly object reloadLock = new object(); private readonly object reloadLock = new object();
private void Reload() private void Reload()
{ {
@@ -59,6 +64,12 @@ namespace NadekoBot.Services
} }
} }
if (string.IsNullOrWhiteSpace(_creds.RedisOptions))
_creds.RedisOptions = "127.0.0.1,syncTimeout=3000";
if (string.IsNullOrWhiteSpace(_creds.CoinmarketcapApiKey))
_creds.CoinmarketcapApiKey = "e79ec505-0913-439d-ae07-069e296a6079";
_creds.TotalShards = _totalShards ?? _creds.TotalShards; _creds.TotalShards = _totalShards ?? _creds.TotalShards;
} }
} }
@@ -71,6 +82,8 @@ namespace NadekoBot.Services
File.WriteAllText(CredsExamplePath, Yaml.Serializer.Serialize(_creds)); File.WriteAllText(CredsExamplePath, Yaml.Serializer.Serialize(_creds));
} }
MigrateCredentials();
if (!File.Exists(CredsPath)) if (!File.Exists(CredsPath))
{ {
Log.Warning($"{CredsPath} is missing. " + Log.Warning($"{CredsPath} is missing. " +
@@ -90,6 +103,47 @@ namespace NadekoBot.Services
Reload(); 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
/// </summary>
private void MigrateCredentials()
{
if (File.Exists(OldCredsJsonPath))
{
Log.Information("Migrating old creds...");
var jsonCredentialsFileText = File.ReadAllText(OldCredsJsonPath);
var oldCreds = JsonConvert.DeserializeObject<Creds.Old>(jsonCredentialsFileText);
var creds = new Creds
{
Version = 1,
Token = oldCreds.Token,
OwnerIds = oldCreds.OwnerIds.Distinct().ToHashSet(),
GoogleApiKey = oldCreds.GoogleApiKey,
RapidApiKey = oldCreds.MashapeKey,
OsuApiKey = oldCreds.OsuApiKey,
CleverbotApiKey = oldCreds.CleverbotApiKey,
TotalShards = oldCreds.TotalShards <= 1 ? 1 : oldCreds.TotalShards,
Patreon = new Creds.PatreonSettings(oldCreds.PatreonAccessToken,
null,
null,
oldCreds.PatreonCampaignId),
Votes = new Creds.VotesSettings(oldCreds.VotesUrl, oldCreds.VotesToken),
BotListToken = oldCreds.BotListToken,
RedisOptions = oldCreds.RedisOptions,
LocationIqApiKey = oldCreds.LocationIqApiKey,
TimezoneDbApiKey = oldCreds.TimezoneDbApiKey,
CoinmarketcapApiKey = oldCreds.CoinmarketcapApiKey,
};
File.Move(OldCredsJsonPath, OldCredsJsonBackupPath, true);
File.WriteAllText(CredsPath, Yaml.Serializer.Serialize(creds));
Log.Warning("Data from credentials.json has been moved to creds.yml\nPlease inspect your creds.yml for correctness");
}
}
public Creds GetCreds() => _creds; public Creds GetCreds() => _creds;
} }
} }

View File

@@ -42,8 +42,6 @@ db:
# Address and port of the coordinator endpoint. Leave empty for default. # Address and port of the coordinator endpoint. Leave empty for default.
# Change only if you've changed the coordinator address or port. # Change only if you've changed the coordinator address or port.
coordinatorUrl: http://localhost:3442 coordinatorUrl: http://localhost:3442
votesUrl:
votesToken:
# Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key) # Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key)
rapidApiKey: rapidApiKey:
# https://locationiq.com api key (register and you will receive the token in the email). # https://locationiq.com api key (register and you will receive the token in the email).