NadekoBot/src/NadekoBot/Services/Impl/BotCredentials.cs

130 lines
5.1 KiB
C#
Raw Normal View History

2016-08-18 23:28:26 +00:00
using Newtonsoft.Json;
using System;
using System.IO;
2016-08-20 11:05:57 +00:00
using Discord;
using System.Linq;
using NLog;
using Microsoft.Extensions.Configuration;
2017-02-13 09:28:49 +00:00
using System.Collections.Immutable;
2016-08-15 23:38:28 +00:00
namespace NadekoBot.Services.Impl
{
public class BotCredentials : IBotCredentials
{
private Logger _log;
public ulong ClientId { get; }
2016-08-16 14:53:38 +00:00
2016-08-18 23:28:26 +00:00
public string GoogleApiKey { get; }
2016-08-15 23:38:28 +00:00
2016-08-20 23:01:50 +00:00
public string MashapeKey { get; }
2016-08-18 23:28:26 +00:00
public string Token { get; }
public ImmutableArray<ulong> OwnerIds { get; }
2016-08-18 23:28:26 +00:00
2016-08-20 17:13:29 +00:00
public string LoLApiKey { get; }
public string OsuApiKey { get; }
2016-08-20 17:13:29 +00:00
2016-11-15 09:54:56 +00:00
public DBConfig Db { get; }
public int TotalShards { get; }
public string CarbonKey { get; }
private readonly string _credsFileName = Path.Combine(Directory.GetCurrentDirectory(), "credentials.json");
2017-03-05 15:09:54 +00:00
public string PatreonAccessToken { get; }
public string ShardRunCommand { get; }
public string ShardRunArguments { get; }
2016-08-18 23:28:26 +00:00
public BotCredentials()
{
_log = LogManager.GetCurrentClassLogger();
try { File.WriteAllText("./credentials_example.json", JsonConvert.SerializeObject(new CredentialsModel(), Formatting.Indented)); } catch { }
if(!File.Exists(_credsFileName))
2016-11-15 08:55:42 +00:00
_log.Warn($"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_");
var data = configBuilder.Build();
Token = data[nameof(Token)];
if (string.IsNullOrWhiteSpace(Token))
{
_log.Error("Token is missing from credentials.json or Environment varibles. Add it and restart the program.");
Console.ReadKey();
Environment.Exit(3);
}
OwnerIds = data.GetSection("OwnerIds").GetChildren().Select(c => ulong.Parse(c.Value)).ToImmutableArray();
LoLApiKey = data[nameof(LoLApiKey)];
GoogleApiKey = data[nameof(GoogleApiKey)];
MashapeKey = data[nameof(MashapeKey)];
OsuApiKey = data[nameof(OsuApiKey)];
2017-03-05 15:09:54 +00:00
PatreonAccessToken = data[nameof(PatreonAccessToken)];
ShardRunCommand = data[nameof(ShardRunCommand)];
ShardRunArguments = data[nameof(ShardRunArguments)];
if (string.IsNullOrWhiteSpace(ShardRunCommand))
ShardRunCommand = "dotnet";
if (string.IsNullOrWhiteSpace(ShardRunArguments))
ShardRunArguments = "run -c Release -- {0} {1}";
int ts = 1;
int.TryParse(data[nameof(TotalShards)], out ts);
TotalShards = ts < 1 ? 1 : ts;
ulong.TryParse(data[nameof(ClientId)], out ulong clId);
2016-11-15 11:02:23 +00:00
ClientId = clId;
2017-06-20 02:24:24 +00:00
//var scId = data[nameof(SoundCloudClientId)];
//SoundCloudClientId = scId;
2017-06-15 13:29:41 +00:00
//SoundCloudClientId = string.IsNullOrWhiteSpace(scId)
// ?
// : scId;
CarbonKey = data[nameof(CarbonKey)];
var dbSection = data.GetSection("db");
2016-11-15 09:54:56 +00:00
Db = new DBConfig(string.IsNullOrWhiteSpace(dbSection["Type"])
? "sqlite"
: dbSection["Type"],
string.IsNullOrWhiteSpace(dbSection["ConnectionString"])
? "Filename=./data/NadekoBot.db"
: dbSection["ConnectionString"]);
}
catch (Exception ex)
{
2016-11-15 10:06:11 +00:00
_log.Fatal(ex.Message);
_log.Fatal(ex);
throw;
}
2016-08-18 23:28:26 +00:00
}
private class CredentialsModel
{
public ulong ClientId { get; set; } = 123123123;
2016-10-14 06:21:45 +00:00
public string Token { get; set; } = "";
public ulong[] OwnerIds { get; set; } = new ulong[1];
public string LoLApiKey { get; set; } = "";
public string GoogleApiKey { get; set; } = "";
public string MashapeKey { get; set; } = "";
public string OsuApiKey { get; set; } = "";
public string SoundCloudClientId { get; set; } = "";
public string CarbonKey { get; set; } = "";
2016-11-15 09:54:56 +00:00
public DBConfig Db { get; set; } = new DBConfig("sqlite", "Filename=./data/NadekoBot.db");
public int TotalShards { get; set; } = 1;
2017-03-05 15:09:54 +00:00
public string PatreonAccessToken { get; set; } = "";
public string ShardRunCommand { get; set; } = "";
}
private class DbModel
{
public string Type { get; set; }
public string ConnectionString { get; set; }
2016-08-15 23:38:28 +00:00
}
2016-08-20 11:05:57 +00:00
public bool IsOwner(IUser u) => OwnerIds.Contains(u.Id);
2016-08-15 23:38:28 +00:00
}
}