From 16dd398aa0e0c8016a296a3bf70ff13db9ce6b27 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 23 Jun 2021 18:11:52 +0200 Subject: [PATCH] - 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 --- CHANGELOG.md | 4 + README.md | 18 +- src/NadekoBot/Bot.cs | 21 +- src/NadekoBot/Common/Creds.cs | 144 +++++---- src/NadekoBot/Db/NadekoContext.cs | 3 +- .../Administration/Services/SelfService.cs | 2 +- src/NadekoBot/Modules/Searches/Searches.cs | 2 +- .../Searches/Services/SearchesService.cs | 2 +- src/NadekoBot/NadekoBot.csproj | 10 +- src/NadekoBot/Services/DbService.cs | 4 +- src/NadekoBot/Services/IBotCredentials.cs | 35 +- src/NadekoBot/Services/Impl/BotCredentials.cs | 299 +++++++----------- 12 files changed, 254 insertions(+), 290 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc961b31c..9f8de705c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o +## [3.0.0] - 01.07.2021 + +WIP + ## [2.46.0] - 17.06.2021 ### Added diff --git a/README.md b/README.md index 02ba6ce14..9b1637dec 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Experimental Branch. Things will break. +# :warning: Experimental Branch. Things will break. :warning: ## Migration from 2.x @@ -6,6 +6,16 @@ ## Changes -- Command attributes cleaned up -- Database migrations cleaned up/squashed - +- explain properly: Command attributes cleaned up +- explain properly: Database migrations cleaned up/squashed +- explain properly: coord and coord.yml +- wip: credentials.json moved to `creds.yml`, example is in `creds_example.json` +- todo: from source run location is nadekobot/output +- todo: votes functionality changed +- todo: code cleanup tasks + - todo: remove colors from bot.cs + - todo: creds + - todo: +- todo?: maybe use https://github.com/Humanizr/Humanizer for trimto, time printing, date printing, etc +- todo?: use guild locale more in the code (from guild settings) (for dates, currency, etc?) +- todo?: Write a sourcegen for response strings and use const/static fields (maybe even typed to enforce correct number of arguments) \ No newline at end of file diff --git a/src/NadekoBot/Bot.cs b/src/NadekoBot/Bot.cs index 07adfeb88..9f5617c0d 100644 --- a/src/NadekoBot/Bot.cs +++ b/src/NadekoBot/Bot.cs @@ -18,7 +18,6 @@ using System.Net.Http; using System.Reflection; using System.Threading.Tasks; using Discord.Net; -using LinqToDB.EntityFrameworkCore; using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Common.Configs; using NadekoBot.Db; @@ -32,14 +31,14 @@ namespace NadekoBot { public class Bot { - public BotCredentials Credentials { get; } + private readonly IBotCredentials _creds; public DiscordSocketClient Client { get; } public CommandService CommandService { get; } private readonly DbService _db; public ImmutableArray AllGuildConfigs { get; private set; } - /* Will have to be removed soon, it's been way too long */ + // todo remove colors from here public static Color OkColor { get; set; } public static Color ErrorColor { get; set; } public static Color PendingColor { get; set; } @@ -60,10 +59,12 @@ namespace NadekoBot TerribleElevatedPermissionCheck(); - Credentials = new BotCredentials(); - Cache = new RedisCache(Credentials, shardId); - LinqToDBForEFTools.Initialize(); - _db = new DbService(Credentials); + _creds = BotCredentialsProvider.CreateBotCredentials(); + + // todo no need for cache prop + Cache = new RedisCache(_creds, shardId); + + _db = new DbService(_creds); if (shardId == 0) { @@ -75,7 +76,7 @@ namespace NadekoBot MessageCacheSize = 50, LogLevel = LogSeverity.Warning, ConnectionTimeout = int.MaxValue, - TotalShards = Credentials.TotalShards, + TotalShards = _creds.TotalShards, ShardId = shardId, AlwaysDownloadUsers = false, ExclusiveBulkDelete = true, @@ -116,7 +117,7 @@ namespace NadekoBot } var svcs = new ServiceCollection() - .AddSingleton(Credentials) + .AddSingleton(_creds) .AddSingleton(_db) .AddSingleton(Client) .AddSingleton(CommandService) @@ -300,7 +301,7 @@ namespace NadekoBot { var sw = Stopwatch.StartNew(); - await LoginAsync(Credentials.Token).ConfigureAwait(false); + await LoginAsync(_creds.Token).ConfigureAwait(false); Mention = Client.CurrentUser.Mention; Log.Information("Shard {ShardId} loading services...", Client.ShardId); diff --git a/src/NadekoBot/Common/Creds.cs b/src/NadekoBot/Common/Creds.cs index f7c10e685..3a63f9e7c 100644 --- a/src/NadekoBot/Common/Creds.cs +++ b/src/NadekoBot/Common/Creds.cs @@ -1,86 +1,120 @@ using System.Collections.Generic; using NadekoBot.Common.Yml; - +using NadekoBot.Services; +using YamlDotNet.Serialization; namespace Nadeko.Common { - public sealed record Creds + public sealed class Creds : IBotCredentials { public Creds() { Token = string.Empty; - OwnerIds = new() - { - 105635576866156544 - }; + OwnerIds = new(); TotalShards = 1; GoogleApiKey = string.Empty; Votes = new(string.Empty, string.Empty); Patreon = new(string.Empty, string.Empty, string.Empty, string.Empty); BotListToken = string.Empty; CleverbotApiKey = string.Empty; - RedisOptions = "redis:6379,syncTimeout=30000,responseTimeout=30000,allowAdmin=true,password="; - Db = new DbOptions(); + RedisOptions = "localhost:6379,syncTimeout=30000,responseTimeout=30000,allowAdmin=true,password="; + Db = new() + { + Type = "sqlite", + ConnectionString = "Data Source=data/NadekoBot.db" + }; Version = 1; } [Comment(@"Bot token. Do not share with anyone ever -> https://discordapp.com/developers/applications/")] - public string Token { get; } + public string Token { get; set; } [Comment(@"List of Ids of the users who have bot owner permissions **DO NOT ADD PEOPLE YOU DON'T TRUST**")] - public HashSet OwnerIds { get; } + public List OwnerIds { get; set; } + // todo update total shards on startup [Comment(@"The number of shards that the bot will running on. Leave at 1 if you don't know what you're doing.")] - public int TotalShards { get; } + public int TotalShards { get; set; } [Comment(@"Login to https://console.cloud.google.com, create a new project, go to APIs & Services -> Library -> YouTube Data API and enable it. Then, go to APIs and Services -> Credentials and click Create credentials -> API key. Used only for Youtube Data Api (at the moment).")] - public string GoogleApiKey { get; } + public string GoogleApiKey { get; set; } [Comment(@"Settings for voting system for discordbots. Meant for use on global Nadeko.")] - public VotesSettings Votes { get; } + public VotesSettings Votes { get; set; } [Comment(@"Patreon auto reward system settings. go to https://www.patreon.com/portal -> my clients -> create client")] - public PatreonSettings Patreon { get; } + public PatreonSettings Patreon { get; set; } [Comment(@"Api key for sending stats to DiscordBotList.")] - public string BotListToken { get; } + public string BotListToken { get; set; } [Comment(@"Official cleverbot api key.")] - public string CleverbotApiKey { get; } + public string CleverbotApiKey { get; set; } [Comment(@"Redis connection string. Don't change if you don't know what you're doing.")] - public string RedisOptions { get; } + public string RedisOptions { get; set; } [Comment(@"Database options. Don't change if you don't know what you're doing. Leave null for default values")] - public DbOptions Db { get; } + public DbOptions Db { get; set; } [Comment(@"DO NOT CHANGE")] - public int Version { get; } + public int Version { get; set; } + + + public RestartConfig RestartCommand { get; set; } + + [YamlIgnore] + public string PatreonCampaignId => Patreon?.CampaignId; + [YamlIgnore] + public string PatreonAccessToken => Patreon?.AccessToken; + + public string VotesUrl { get; set; } + public string VotesToken { get; set; } + + [Comment(@"Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key)")] + public string RapidApiKey { get; set; } + + [Comment(@"https://locationiq.com api key (register and you will receive the token in the email). +Used only for .time command.")] + public string LocationIqApiKey { get; set; } + + [Comment(@"https://timezonedb.com api key (register and you will receive the token in the email). +Used only for .time command")] + public string TimezoneDbApiKey { get; set; } + + [Comment(@"https://pro.coinmarketcap.com/account/ api key. There is a free plan for personal use. +Used for cryptocurrency related commands.")] + public string CoinmarketcapApiKey { get; set; } + + [Comment(@"Api key used for Osu related commands. Obtain this key at https://osu.ppy.sh/p/api")] + public string OsuApiKey { get; set; } + public class DbOptions { [Comment(@"Database type. Only sqlite supported atm")] - public string Type { get; } = ""; + public string Type { get; set; } [Comment(@"Connection string. Will default to ""Data Source=data/NadekoBot.db""")] - public string ConnectionString { get; } = string.Empty; + public string ConnectionString { get; set; } } + // todo fixup patreon public sealed record PatreonSettings { - [Comment(@"")] - public string AccessToken { get; } - [Comment(@"")] - public string RefreshToken { get; } - [Comment(@"")] - public string ClientSecret { get; } + [Comment(@"Access token. You have to manually update this 1st of each month by refreshing the token on https://patreon.com/portal")] + public string AccessToken { get; set; } + [Comment(@"Unused atm")] + public string RefreshToken { get; set; } + [Comment(@"Unused atm")] + public string ClientSecret { get; set; } [Comment(@"Campaign ID of your patreon page. Go to your patreon page (make sure you're logged in) and type ""prompt('Campaign ID', window.patreon.bootstrap.creator.data.id);"" in the console. (ctrl + shift + i)")] - public string CampaignId { get; } + public string CampaignId { get; set; } public PatreonSettings(string accessToken, string refreshToken, string clientSecret, string campaignId) { @@ -94,9 +128,9 @@ go to https://www.patreon.com/portal -> my clients -> create client")] public sealed record VotesSettings { [Comment(@"")] - public string Url { get; } + public string Url { get; set; } [Comment(@"")] - public string Key { get; } + public string Key { get; set; } public VotesSettings(string url, string key) { @@ -107,31 +141,31 @@ go to https://www.patreon.com/portal -> my clients -> create client")] public class Old { - public string Token { get; } = string.Empty; - public ulong[] OwnerIds { get; } = new ulong[1]; - public string LoLApiKey { get; } = string.Empty; - public string GoogleApiKey { get; } = string.Empty; - public string MashapeKey { get; } = string.Empty; - public string OsuApiKey { get; } = string.Empty; - public string SoundCloudClientId { get; } = string.Empty; - public string CleverbotApiKey { get; } = string.Empty; - public string CarbonKey { get; } = string.Empty; - public int TotalShards { get; } = 1; - public string PatreonAccessToken { get; } = string.Empty; - public string PatreonCampaignId { get; } = "334038"; - public RestartConfig? RestartCommand { get; } = null; + public string Token { get; set; } = string.Empty; + public ulong[] OwnerIds { get; set; } = new ulong[1]; + public string LoLApiKey { get; set; } = string.Empty; + public string GoogleApiKey { get; set; } = string.Empty; + public string MashapeKey { get; set; } = string.Empty; + public string OsuApiKey { get; set; } = string.Empty; + public string SoundCloudClientId { get; set; } = string.Empty; + public string CleverbotApiKey { get; set; } = string.Empty; + public string CarbonKey { get; set; } = string.Empty; + public int TotalShards { get; set; } = 1; + public string PatreonAccessToken { get; set; } = string.Empty; + public string PatreonCampaignId { get; set; } = "334038"; + public RestartConfig? RestartCommand { get; set; } = null; - public string ShardRunCommand { get; } = string.Empty; - public string ShardRunArguments { get; } = string.Empty; - public int? ShardRunPort { get; } = null; - public string MiningProxyUrl { get; } = string.Empty; - public string MiningProxyCreds { get; } = string.Empty; + public string ShardRunCommand { get; set; } = string.Empty; + public string ShardRunArguments { get; set; } = string.Empty; + public int? ShardRunPort { get; set; } = null; + public string MiningProxyUrl { get; set; } = string.Empty; + public string MiningProxyCreds { get; set; } = string.Empty; - public string BotListToken { get; } = string.Empty; - public string TwitchClientId { get; } = string.Empty; - public string VotesToken { get; } = string.Empty; - public string VotesUrl { get; } = string.Empty; - public string RedisOptions { get; } = string.Empty; + public string BotListToken { get; set; } = string.Empty; + public string TwitchClientId { get; set; } = string.Empty; + public string VotesToken { get; set; } = string.Empty; + public string VotesUrl { get; set; } = string.Empty; + public string RedisOptions { get; set; } = string.Empty; public class RestartConfig { @@ -141,8 +175,8 @@ go to https://www.patreon.com/portal -> my clients -> create client")] this.Args = args; } - public string Cmd { get; } - public string Args { get; } + public string Cmd { get; set; } + public string Args { get; set; } } } } diff --git a/src/NadekoBot/Db/NadekoContext.cs b/src/NadekoBot/Db/NadekoContext.cs index 594f7c053..be6d644a0 100644 --- a/src/NadekoBot/Db/NadekoContext.cs +++ b/src/NadekoBot/Db/NadekoContext.cs @@ -6,6 +6,7 @@ using NadekoBot.Services; using System; using System.IO; using Microsoft.Extensions.Logging; +using Nadeko.Common; using NadekoBot.Db.Models; namespace NadekoBot.Services.Database @@ -16,7 +17,7 @@ namespace NadekoBot.Services.Database { LogSetup.SetupLogger(-2); var optionsBuilder = new DbContextOptionsBuilder(); - IBotCredentials creds = new BotCredentials(); + IBotCredentials creds = BotCredentialsProvider.CreateBotCredentials(); var builder = new SqliteConnectionStringBuilder(creds.Db.ConnectionString); builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource); optionsBuilder.UseSqlite(builder.ToString()); diff --git a/src/NadekoBot/Modules/Administration/Services/SelfService.cs b/src/NadekoBot/Modules/Administration/Services/SelfService.cs index 025e735ca..613d85100 100644 --- a/src/NadekoBot/Modules/Administration/Services/SelfService.cs +++ b/src/NadekoBot/Modules/Administration/Services/SelfService.cs @@ -218,7 +218,7 @@ namespace NadekoBot.Modules.Administration.Services Log.Warning( "No owner channels created! Make sure you've specified the correct OwnerId in the credentials.json file and invited the bot to a Discord server."); else - Log.Information($"Created {ownerChannels.Count} out of {_creds.OwnerIds.Length} owner message channels."); + Log.Information($"Created {ownerChannels.Count} out of {_creds.OwnerIds.Count} owner message channels."); } public Task LeaveGuild(string guildStr) diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index 220aeb0cc..d2dad9496 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -430,7 +430,7 @@ namespace NadekoBot.Modules.Searches if (!await ValidateQuery(ctx.Channel, name).ConfigureAwait(false)) return; - if (string.IsNullOrWhiteSpace(_creds.MashapeKey)) + if (string.IsNullOrWhiteSpace(_creds.RapidApiKey)) { await ReplyErrorLocalizedAsync("mashape_api_missing").ConfigureAwait(false); return; diff --git a/src/NadekoBot/Modules/Searches/Services/SearchesService.cs b/src/NadekoBot/Modules/Searches/Services/SearchesService.cs index f91065078..ddedd7729 100644 --- a/src/NadekoBot/Modules/Searches/Services/SearchesService.cs +++ b/src/NadekoBot/Modules/Searches/Services/SearchesService.cs @@ -575,7 +575,7 @@ namespace NadekoBot.Modules.Searches.Services using (var http = _httpFactory.CreateClient()) { http.DefaultRequestHeaders.Clear(); - http.DefaultRequestHeaders.Add("x-rapidapi-key", _creds.MashapeKey); + http.DefaultRequestHeaders.Add("x-rapidapi-key", _creds.RapidApiKey); try { var response = await http.GetStringAsync($"https://omgvamp-hearthstone-v1.p.rapidapi.com/" + diff --git a/src/NadekoBot/NadekoBot.csproj b/src/NadekoBot/NadekoBot.csproj index 59a755b77..90f0c666e 100644 --- a/src/NadekoBot/NadekoBot.csproj +++ b/src/NadekoBot/NadekoBot.csproj @@ -34,6 +34,7 @@ + @@ -56,8 +57,13 @@ + + + Always + + + - Protos\coordinator.proto @@ -73,7 +79,7 @@ Always - + Always diff --git a/src/NadekoBot/Services/DbService.cs b/src/NadekoBot/Services/DbService.cs index f6c911a85..7aaabdd99 100644 --- a/src/NadekoBot/Services/DbService.cs +++ b/src/NadekoBot/Services/DbService.cs @@ -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); diff --git a/src/NadekoBot/Services/IBotCredentials.cs b/src/NadekoBot/Services/IBotCredentials.cs index cda36be94..c8370d65f 100644 --- a/src/NadekoBot/Services/IBotCredentials.cs +++ b/src/NadekoBot/Services/IBotCredentials.cs @@ -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 OwnerIds { get; } - string MashapeKey { get; } + List 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; } - } } diff --git a/src/NadekoBot/Services/Impl/BotCredentials.cs b/src/NadekoBot/Services/Impl/BotCredentials.cs index ed623b7e1..e74d3416b 100644 --- a/src/NadekoBot/Services/Impl/BotCredentials.cs +++ b/src/NadekoBot/Services/Impl/BotCredentials.cs @@ -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 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(); - 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); + // } } - - /// - /// No idea why this thing exists - /// - 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 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); } } \ No newline at end of file