diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 7da1e109..5f1f4e79 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -20,6 +20,7 @@ using NadekoBot.TypeReaders; using System.Collections.Concurrent; using NadekoBot.Modules.Music; using NadekoBot.Services.Database.Models; +using Microsoft.Extensions.Configuration; namespace NadekoBot { @@ -51,6 +52,8 @@ namespace NadekoBot public async Task RunAsync(params string[] args) { + + SetupLogger(); _log = LogManager.GetCurrentClassLogger(); diff --git a/src/NadekoBot/Services/Impl/BotCredentials.cs b/src/NadekoBot/Services/Impl/BotCredentials.cs index d52e8ad7..8faf74b7 100644 --- a/src/NadekoBot/Services/Impl/BotCredentials.cs +++ b/src/NadekoBot/Services/Impl/BotCredentials.cs @@ -5,6 +5,7 @@ using System.IO; using Discord; using System.Linq; using NLog; +using Microsoft.Extensions.Configuration; namespace NadekoBot.Services.Impl { @@ -31,34 +32,54 @@ namespace NadekoBot.Services.Impl public int TotalShards { get; } public string CarbonKey { get; } + public string credsFileName { get; } = Path.Combine(Directory.GetCurrentDirectory(), "credentials.json"); + public BotCredentials() { _log = LogManager.GetCurrentClassLogger(); try { File.WriteAllText("./credentials_example.json", JsonConvert.SerializeObject(new CredentialsModel(), Formatting.Indented)); } catch { } - if (File.Exists("./credentials.json")) + if(!File.Exists(credsFileName)) + _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 cm = JsonConvert.DeserializeObject(File.ReadAllText("./credentials.json")); - Token = cm.Token; - OwnerIds = cm.OwnerIds; - LoLApiKey = cm.LoLApiKey; - GoogleApiKey = cm.GoogleApiKey; - MashapeKey = cm.MashapeKey; - OsuApiKey = cm.OsuApiKey; - TotalShards = cm.TotalShards < 1 ? 1 : cm.TotalShards; - BotId = cm.BotId ?? cm.ClientId; - ClientId = cm.ClientId; - SoundCloudClientId = cm.SoundCloudClientId; - CarbonKey = cm.CarbonKey; - if (cm.Db == null) - Db = new DB("sqlite", ""); - else - Db = new DB(cm.Db.Type, cm.Db.ConnectionString); + var configBuilder = new ConfigurationBuilder(); + configBuilder.AddJsonFile(credsFileName) + .AddEnvironmentVariables("NadekoBot:"); + + var data = configBuilder.Build(); + + Token = data[nameof(Token)]; + if (string.IsNullOrWhiteSpace(Token)) + throw new ArgumentNullException(nameof(Token), "Token is missing from credentials.json or Environment varibles."); + OwnerIds = data.GetSection("OwnerIds").GetChildren().Select(c => ulong.Parse(c.Value)).ToArray(); + LoLApiKey = data[nameof(LoLApiKey)]; + GoogleApiKey = data[nameof(GoogleApiKey)]; + MashapeKey = data[nameof(MashapeKey)]; + OsuApiKey = data[nameof(OsuApiKey)]; + + int ts = 1; + int.TryParse(data[nameof(TotalShards)], out ts); + TotalShards = ts < 1 ? 1 : ts; + + ulong clId = 0; + ulong.TryParse(data[nameof(ClientId)], out clId); + ClientId = ulong.Parse(data[nameof(ClientId)]); + + SoundCloudClientId = data[nameof(SoundCloudClientId)]; + CarbonKey = data[nameof(CarbonKey)]; + var dbSection = data.GetSection("db"); + Db = new DB(string.IsNullOrWhiteSpace(dbSection["Type"]) + ? "sqlite" + : dbSection["Type"], + string.IsNullOrWhiteSpace(dbSection["ConnectionString"]) + ? "Filename=./data/NadekoBot.db" + : dbSection["ConnectionString"]); } - else + catch (Exception ex) { - _log.Fatal($"credentials.json is missing. Failed to start. Example is in {Path.GetFullPath("./credentials_example.json")}"); - throw new FileNotFoundException(); + _log.Warn(ex); + throw; } } @@ -66,7 +87,6 @@ namespace NadekoBot.Services.Impl private class CredentialsModel { public ulong ClientId { get; set; } = 123123123; - public ulong? BotId { get; set; } public string Token { get; set; } = ""; public ulong[] OwnerIds { get; set; } = new ulong[1]; public string LoLApiKey { get; set; } = ""; @@ -75,7 +95,7 @@ namespace NadekoBot.Services.Impl public string OsuApiKey { get; set; } = ""; public string SoundCloudClientId { get; set; } = ""; public string CarbonKey { get; set; } = ""; - public DB Db { get; set; } + public DB Db { get; set; } = new DB("sqlite", "Filename=./data/NadekoBot.db"); public int TotalShards { get; set; } = 1; } diff --git a/src/NadekoBot/credentials_example.json b/src/NadekoBot/credentials_example.json index 34925301..240f9029 100644 --- a/src/NadekoBot/credentials_example.json +++ b/src/NadekoBot/credentials_example.json @@ -1,6 +1,5 @@ { "ClientId": 123123123, - "BotId": null, "Token": "", "OwnerIds": [ 0 @@ -11,6 +10,9 @@ "OsuApiKey": "", "SoundCloudClientId": "", "CarbonKey": "", - "Db": null, + "Db": { + "Type": "sqlite", + "ConnectionString": "Filename=./data/NadekoBot.db" + }, "TotalShards": 1 } \ No newline at end of file diff --git a/src/NadekoBot/project.json b/src/NadekoBot/project.json index 7b6efe1c..1b2c1995 100644 --- a/src/NadekoBot/project.json +++ b/src/NadekoBot/project.json @@ -42,7 +42,10 @@ "Discord.Net": { "target": "project" }, - "System.Xml.XPath": "4.0.1" + "System.Xml.XPath": "4.0.1", + "Microsoft.Extensions.Configuration": "1.0.0", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", + "Microsoft.Extensions.Configuration.Json": "1.0.0" }, "tools": { "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"