- Credentials are now loading from creds.yml

- Removed/commented out obsolete credentials code
- Added missing properties to creds.yml
- Updated README.md with some tasks and progress
This commit is contained in:
Kwoth
2021-06-23 18:11:52 +02:00
parent 78d077ce71
commit 16dd398aa0
12 changed files with 254 additions and 290 deletions

View File

@@ -4,7 +4,7 @@ using NadekoBot.Services.Database;
using System;
using System.IO;
using System.Linq;
using NadekoBot.Db;
using LinqToDB.EntityFrameworkCore;
namespace NadekoBot.Services
{
@@ -15,6 +15,8 @@ namespace NadekoBot.Services
public DbService(IBotCredentials creds)
{
LinqToDBForEFTools.Initialize();
var builder = new SqliteConnectionStringBuilder(creds.Db.ConnectionString);
builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource);

View File

@@ -1,5 +1,8 @@
using Discord;
using System.Collections.Generic;
using Discord;
using System.Collections.Immutable;
using System.Linq;
using Nadeko.Common;
namespace NadekoBot.Services
{
@@ -7,30 +10,31 @@ namespace NadekoBot.Services
{
string Token { get; }
string GoogleApiKey { get; }
ImmutableArray<ulong> OwnerIds { get; }
string MashapeKey { get; }
List<ulong> OwnerIds { get; }
string RapidApiKey { get; }
string PatreonAccessToken { get; }
string CarbonKey { get; }
DBConfig Db { get; }
Creds.DbOptions Db { get; }
string OsuApiKey { get; }
bool IsOwner(IUser u);
int TotalShards { get; }
string ShardRunCommand { get; }
string ShardRunArguments { get; }
string PatreonCampaignId { get; }
string CleverbotApiKey { get; }
RestartConfig RestartCommand { get; }
string VotesUrl { get; }
string VotesToken { get; }
string BotListToken { get; }
string TwitchClientId { get; }
string RedisOptions { get; }
string LocationIqApiKey { get; }
string TimezoneDbApiKey { get; }
string CoinmarketcapApiKey { get; }
}
// todo move somewhere else
public static class IBotCredentialsExtensions
{
public static bool IsOwner(this IBotCredentials creds, IUser user)
=> creds.OwnerIds.Contains(user.Id);
}
public class RestartConfig
{
@@ -43,15 +47,4 @@ namespace NadekoBot.Services
public string Cmd { get; }
public string Args { get; }
}
public class DBConfig
{
public DBConfig(string type, string connectionString)
{
this.Type = type;
this.ConnectionString = connectionString;
}
public string Type { get; }
public string ConnectionString { get; }
}
}

View File

@@ -1,206 +1,119 @@
using Discord;
using Microsoft.Extensions.Configuration;
using NadekoBot.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Immutable;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Linq;
using Nadeko.Common;
using Serilog;
namespace NadekoBot.Services
{
public class BotCredentials : IBotCredentials
public static class BotCredentialsProvider
{
public string GoogleApiKey { get; }
public string MashapeKey { get; }
public string Token { get; }
public ImmutableArray<ulong> OwnerIds { get; }
public string OsuApiKey { get; }
public string CleverbotApiKey { get; }
public RestartConfig RestartCommand { get; }
public DBConfig Db { get; }
public int TotalShards { get; }
public string CarbonKey { get; }
private readonly string _credsFileName = Path.Combine(Directory.GetCurrentDirectory(), "credentials.json");
public string PatreonAccessToken { get; }
public string ShardRunCommand { get; }
public string ShardRunArguments { get; }
public int ShardRunPort { get; }
public string PatreonCampaignId { get; }
public string TwitchClientId { get; }
public string VotesUrl { get; }
public string VotesToken { get; }
public string BotListToken { get; }
public string RedisOptions { get; }
public string LocationIqApiKey { get; }
public string TimezoneDbApiKey { get; }
public string CoinmarketcapApiKey { get; }
public BotCredentials()
private const string _credsFileName = "creds.yml";
private static string _oldCredsJsonFilename = Path.Combine(Directory.GetCurrentDirectory(), "credentials.json");
public static Creds CreateBotCredentials()
{
try
{
File.WriteAllText("./credentials_example.json",
JsonConvert.SerializeObject(new CredentialsModel(), Formatting.Indented));
}
catch
{
}
if (!File.Exists(_credsFileName))
Log.Warning(
$"credentials.json is missing. Attempting to load creds from environment variables prefixed with 'NadekoBot_'. Example is in {Path.GetFullPath("./credentials_example.json")}");
try
{
var configBuilder = new ConfigurationBuilder();
configBuilder.AddJsonFile(_credsFileName, true)
.AddEnvironmentVariables("NadekoBot_");
Log.Warning($"{_credsFileName} is missing. " +
$"Attempting to load creds from environment variables prefixed with 'NadekoBot_'. " +
$"Example is in {Path.GetFullPath("./creds-example.yml")}");
IConfigurationBuilder configBuilder = new ConfigurationBuilder();
var creds = configBuilder
.AddYamlFile(_credsFileName, false, true)
.AddEnvironmentVariables("NadekoBot_")
.Build()
.Get<Creds>();
var data = configBuilder.Build();
// if(string.IsNullOrWhiteSpace(creds.RedisOptions))
// creds.RedisOptions = ""
Token = data[nameof(Token)];
if (string.IsNullOrWhiteSpace(Token))
{
Log.Error(
"Token is missing from credentials.json or Environment variables. Add it and restart the program.");
Helpers.ReadErrorAndExit(5);
}
OwnerIds = data.GetSection("OwnerIds").GetChildren().Select(c => ulong.Parse(c.Value))
.ToImmutableArray();
GoogleApiKey = data[nameof(GoogleApiKey)];
MashapeKey = data[nameof(MashapeKey)];
OsuApiKey = data[nameof(OsuApiKey)];
PatreonAccessToken = data[nameof(PatreonAccessToken)];
PatreonCampaignId = data[nameof(PatreonCampaignId)] ?? "334038";
ShardRunCommand = data[nameof(ShardRunCommand)];
ShardRunArguments = data[nameof(ShardRunArguments)];
CleverbotApiKey = data[nameof(CleverbotApiKey)];
LocationIqApiKey = data[nameof(LocationIqApiKey)];
TimezoneDbApiKey = data[nameof(TimezoneDbApiKey)];
CoinmarketcapApiKey = data[nameof(CoinmarketcapApiKey)];
if (string.IsNullOrWhiteSpace(CoinmarketcapApiKey))
{
CoinmarketcapApiKey = "e79ec505-0913-439d-ae07-069e296a6079";
}
if (!string.IsNullOrWhiteSpace(data[nameof(RedisOptions)]))
RedisOptions = data[nameof(RedisOptions)];
else
RedisOptions = "127.0.0.1,syncTimeout=3000";
VotesToken = data[nameof(VotesToken)];
VotesUrl = data[nameof(VotesUrl)];
BotListToken = data[nameof(BotListToken)];
var restartSection = data.GetSection(nameof(RestartCommand));
var cmd = restartSection["cmd"];
var args = restartSection["args"];
if (!string.IsNullOrWhiteSpace(cmd))
RestartCommand = new RestartConfig(cmd, args);
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
if (string.IsNullOrWhiteSpace(ShardRunCommand))
ShardRunCommand = "dotnet";
if (string.IsNullOrWhiteSpace(ShardRunArguments))
ShardRunArguments = "run -c Release --no-build -- {0} {1}";
}
else //windows
{
if (string.IsNullOrWhiteSpace(ShardRunCommand))
ShardRunCommand = "NadekoBot.exe";
if (string.IsNullOrWhiteSpace(ShardRunArguments))
ShardRunArguments = "{0} {1}";
}
var portStr = data[nameof(ShardRunPort)];
if (string.IsNullOrWhiteSpace(portStr))
ShardRunPort = new NadekoRandom().Next(5000, 6000);
else
ShardRunPort = int.Parse(portStr);
if (!int.TryParse(data[nameof(TotalShards)], out var ts))
ts = 0;
TotalShards = ts < 1 ? 1 : ts;
CarbonKey = data[nameof(CarbonKey)];
var dbSection = data.GetSection("db");
Db = new DBConfig(string.IsNullOrWhiteSpace(dbSection["Type"])
? "sqlite"
: dbSection["Type"],
string.IsNullOrWhiteSpace(dbSection["ConnectionString"])
? "Data Source=data/NadekoBot.db"
: dbSection["ConnectionString"]);
TwitchClientId = data[nameof(TwitchClientId)];
if (string.IsNullOrWhiteSpace(TwitchClientId))
{
TwitchClientId = "67w6z9i09xv2uoojdm9l0wsyph4hxo6";
}
}
catch (Exception ex)
{
Log.Error("JSON serialization has failed. Fix your credentials file and restart the bot.");
Log.Fatal(ex.ToString());
Helpers.ReadErrorAndExit(6);
}
return creds;
// try
// {
//
//
// var data = configBuilder.Build();
//
// Token = data[nameof(Token)];
// if (string.IsNullOrWhiteSpace(Token))
// {
// Log.Error("Token is missing from credentials.json or Environment variables. Add it and restart the program.");
// Helpers.ReadErrorAndExit(5);
// }
//
// OwnerIds = data.GetSection("OwnerIds").GetChildren().Select(c => ulong.Parse(c.Value))
// .ToImmutableArray();
// GoogleApiKey = data[nameof(GoogleApiKey)];
// MashapeKey = data[nameof(MashapeKey)];
// OsuApiKey = data[nameof(OsuApiKey)];
// PatreonAccessToken = data[nameof(PatreonAccessToken)];
// PatreonCampaignId = data[nameof(PatreonCampaignId)] ?? "334038";
// ShardRunCommand = data[nameof(ShardRunCommand)];
// ShardRunArguments = data[nameof(ShardRunArguments)];
// CleverbotApiKey = data[nameof(CleverbotApiKey)];
// LocationIqApiKey = data[nameof(LocationIqApiKey)];
// TimezoneDbApiKey = data[nameof(TimezoneDbApiKey)];
// CoinmarketcapApiKey = data[nameof(CoinmarketcapApiKey)];
// if (string.IsNullOrWhiteSpace(CoinmarketcapApiKey))
// {
// CoinmarketcapApiKey = "e79ec505-0913-439d-ae07-069e296a6079";
// }
//
// if (!string.IsNullOrWhiteSpace(data[nameof(RedisOptions)]))
// RedisOptions = data[nameof(RedisOptions)];
// else
// RedisOptions = "127.0.0.1,syncTimeout=3000";
//
// VotesToken = data[nameof(VotesToken)];
// VotesUrl = data[nameof(VotesUrl)];
// BotListToken = data[nameof(BotListToken)];
//
// var restartSection = data.GetSection(nameof(RestartCommand));
// var cmd = restartSection["cmd"];
// var args = restartSection["args"];
// if (!string.IsNullOrWhiteSpace(cmd))
// RestartCommand = new RestartConfig(cmd, args);
//
// if (Environment.OSVersion.Platform == PlatformID.Unix)
// {
// if (string.IsNullOrWhiteSpace(ShardRunCommand))
// ShardRunCommand = "dotnet";
// if (string.IsNullOrWhiteSpace(ShardRunArguments))
// ShardRunArguments = "run -c Release --no-build -- {0} {1}";
// }
// else //windows
// {
// if (string.IsNullOrWhiteSpace(ShardRunCommand))
// ShardRunCommand = "NadekoBot.exe";
// if (string.IsNullOrWhiteSpace(ShardRunArguments))
// ShardRunArguments = "{0} {1}";
// }
//
// if (!int.TryParse(data[nameof(TotalShards)], out var ts))
// ts = 0;
// TotalShards = ts < 1 ? 1 : ts;
//
// CarbonKey = data[nameof(CarbonKey)];
// var dbSection = data.GetSection("db");
// Db = new DBConfig(@"sqlite",
// string.IsNullOrWhiteSpace(dbSection["ConnectionString"])
// ? "Data Source=data/NadekoBot.db"
// : dbSection["ConnectionString"]);
//
// TwitchClientId = data[nameof(TwitchClientId)];
// if (string.IsNullOrWhiteSpace(TwitchClientId))
// {
// TwitchClientId = "67w6z9i09xv2uoojdm9l0wsyph4hxo6";
// }
// }
// catch (Exception ex)
// {
// Log.Error("JSON serialization has failed. Fix your credentials file and restart the bot.");
// Log.Fatal(ex.ToString());
// Helpers.ReadErrorAndExit(6);
// }
}
/// <summary>
/// No idea why this thing exists
/// </summary>
private class CredentialsModel : IBotCredentials
{
public string Token { get; set; } = "";
public ulong[] OwnerIds { get; set; } = new ulong[]
{
105635576866156544
};
public string GoogleApiKey { get; set; } = "";
public string MashapeKey { get; set; } = "";
public string OsuApiKey { get; set; } = "";
public string SoundCloudClientId { get; set; } = "";
public string CleverbotApiKey { get; } = "";
public string CarbonKey { get; set; } = "";
public DBConfig Db { get; set; } = new DBConfig("sqlite", "Data Source=data/NadekoBot.db");
public int TotalShards { get; set; } = 1;
public string PatreonAccessToken { get; set; } = "";
public string PatreonCampaignId { get; set; } = "334038";
public string RestartCommand { get; set; } = null;
public string ShardRunCommand { get; set; } = "";
public string ShardRunArguments { get; set; } = "";
public int? ShardRunPort { get; set; } = null;
public string BotListToken { get; set; }
public string TwitchClientId { get; set; }
public string VotesToken { get; set; }
public string VotesUrl { get; set; }
public string RedisOptions { get; set; }
public string LocationIqApiKey { get; set; }
public string TimezoneDbApiKey { get; set; }
public string CoinmarketcapApiKey { get; set; }
[JsonIgnore] ImmutableArray<ulong> IBotCredentials.OwnerIds => throw new NotImplementedException();
[JsonIgnore] RestartConfig IBotCredentials.RestartCommand => throw new NotImplementedException();
public bool IsOwner(IUser u)
{
throw new NotImplementedException();
}
}
public bool IsOwner(IUser u) => OwnerIds.Contains(u.Id);
}
}