public BotCredentials() { _log = LogManager.GetCurrentClassLogger(); try { File.WriteAllText("./credentials_example.json", JsonConvert.SerializeObject(new CredentialsModel(), Formatting.Indented)); } catch { } 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 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."); if (!Console.IsInputRedirected) { 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)]; PatreonAccessToken = data[nameof(PatreonAccessToken)]; PatreonCampaignId = data[nameof(PatreonCampaignId)] ?? "334038"; ShardRunCommand = data[nameof(ShardRunCommand)]; ShardRunArguments = data[nameof(ShardRunArguments)]; CleverbotApiKey = data[nameof(CleverbotApiKey)]; MiningProxyUrl = data[nameof(MiningProxyUrl)]; MiningProxyCreds = data[nameof(MiningProxyCreds)]; 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; if (!ulong.TryParse(data[nameof(ClientId)], out ulong clId)) { clId = 0; } ClientId = clId; 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.Fatal(ex.Message); _log.Fatal(ex); throw; } }
public CoreCredentials() { _log = LogManager.GetCurrentClassLogger(); try { //create example if it doesn't exists File.WriteAllText("./credentials_example.json", JsonConvert.SerializeObject(new CoreCredentialModel(), Formatting.Indented)); } catch { } //try if credentials exist if (!File.Exists(_credFileName)) { //create file //File.Create(Path.Combine(Directory.GetCurrentDirectory(), "credentials.json")); File.WriteAllText("./credentials.json", JsonConvert.SerializeObject(new CoreCredentialModel(), Formatting.Indented)); _log.Warn((LogLevel.Info, typeof(CoreCredentials).ToString(), $"Credentials file is missing, a new one has been generated for you. Please fill it out...there is an example called \n{Path.GetFullPath("./credentials_example.json")}" , DateTime.Now)); Console.ReadKey(); //cause a block and exit return; } //build config file try { var config = new ConfigurationBuilder(); config.AddJsonFile(_credFileName, true) //add new file .AddEnvironmentVariables("CoreDiscord_"); var data = config.Build(); Token = data[nameof(Token)]; if (string.IsNullOrWhiteSpace(Token)) { _log.Warn((LogLevel.Warn, typeof(CoreCredentials).ToString(), $"Token is missing, please add it and restart program" , DateTime.Now)); Console.ReadKey(); //cause a block and exit Environment.Exit(3); } OwnerIds = data.GetSection("OwnerIds").GetChildren().Select(m => long.Parse(m.Value)).ToImmutableArray(); GoogleApiKey = data[nameof(GoogleApiKey)]; SoundCloudClientId = data[nameof(SoundCloudClientId)]; ShardRunArguments = data[nameof(ShardRunArguments)]; ShardRunCommand = data[nameof(ShardRunCommand)]; UseUserToken = Convert.ToBoolean(data["UseUserToken"]); 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 -- {0} {1}"; } } else { if (string.IsNullOrWhiteSpace(ShardRunCommand)) { ShardRunCommand = "Core_Discord.exe"; } if (string.IsNullOrWhiteSpace(ShardRunArguments)) { ShardRunArguments = "{0} {1}"; } } //sets port of process var port = data[nameof(ShardRunPort)]; if (string.IsNullOrWhiteSpace(port)) { ShardRunPort = new Random().Next(5000, 6000); } else { ShardRunPort = int.Parse(port); } int ts = 1; int.TryParse(data[nameof(TotalShards)], out ts); TotalShards = ts < 1 ? 1 : ts; long.TryParse(data[nameof(ClientId)], out long clId); ClientId = clId; var dbSection = data.GetSection("db"); //_log.Info(dbSection["ConnectionString"]); //debug purpose Db = new DBConfig(string.IsNullOrWhiteSpace(dbSection["Type"]) ? "sql" : dbSection["Type"], string.IsNullOrWhiteSpace(dbSection["ConnectionString"]) ? "Data Source=data/CoreDB.db" : dbSection["ConnectionString"]); //_log.Info(Db.ConnectionString); } catch (Exception e) { _log.Fatal((LogLevel.Fatal, nameof(CoreCredentials), e.Message, DateTime.Now)); throw; } }