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 ;
2016-08-20 20:35:27 +00:00
using NLog ;
2016-11-15 08:42:06 +00:00
using Microsoft.Extensions.Configuration ;
2017-02-13 09:28:49 +00:00
using System.Collections.Immutable ;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common ;
2016-08-15 23:38:28 +00:00
2017-10-13 04:14:54 +00:00
namespace NadekoBot.Core.Services.Impl
2016-08-15 23:38:28 +00:00
{
public class BotCredentials : IBotCredentials
{
2016-08-20 20:35:27 +00:00
private Logger _log ;
2016-10-09 02:26:14 +00:00
public ulong ClientId { get ; }
2016-08-18 23:28:26 +00:00
public string GoogleApiKey { get ; }
2016-08-20 23:01:50 +00:00
public string MashapeKey { get ; }
2016-08-18 23:28:26 +00:00
public string Token { get ; }
2017-04-10 01:14:52 +00:00
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 ; }
2016-08-21 22:37:39 +00:00
public string OsuApiKey { get ; }
2017-07-20 20:58:19 +00:00
public string CleverbotApiKey { get ; }
2017-09-27 06:32:33 +00:00
public RestartConfig RestartCommand { get ; }
2016-11-15 09:54:56 +00:00
public DBConfig Db { get ; }
2016-10-01 03:49:05 +00:00
public int TotalShards { get ; }
2016-10-25 11:06:37 +00:00
public string CarbonKey { get ; }
2016-08-24 01:32:48 +00:00
2017-06-21 21:12:24 +00:00
private readonly string _credsFileName = Path . Combine ( Directory . GetCurrentDirectory ( ) , "credentials.json" ) ;
2017-03-05 15:09:54 +00:00
public string PatreonAccessToken { get ; }
2017-06-21 21:12:24 +00:00
public string ShardRunCommand { get ; }
public string ShardRunArguments { get ; }
2017-06-25 04:09:23 +00:00
public int ShardRunPort { get ; }
2016-11-15 08:42:06 +00:00
2017-07-05 09:43:36 +00:00
public string PatreonCampaignId { get ; }
2016-08-18 23:28:26 +00:00
public BotCredentials ( )
{
2016-08-20 20:35:27 +00:00
_log = LogManager . GetCurrentClassLogger ( ) ;
2016-10-14 19:54:26 +00:00
try { File . WriteAllText ( "./credentials_example.json" , JsonConvert . SerializeObject ( new CredentialsModel ( ) , Formatting . Indented ) ) ; } catch { }
2017-06-21 21:12:24 +00:00
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 ")}" ) ;
2016-11-15 08:42:06 +00:00
try
2016-08-20 20:35:27 +00:00
{
2016-11-15 08:42:06 +00:00
var configBuilder = new ConfigurationBuilder ( ) ;
2017-06-21 21:12:24 +00:00
configBuilder . AddJsonFile ( _credsFileName , true )
2016-11-15 08:55:10 +00:00
. AddEnvironmentVariables ( "NadekoBot_" ) ;
2016-11-15 08:42:06 +00:00
var data = configBuilder . Build ( ) ;
Token = data [ nameof ( Token ) ] ;
if ( string . IsNullOrWhiteSpace ( Token ) )
2017-06-05 05:42:00 +00:00
{
_log . Error ( "Token is missing from credentials.json or Environment varibles. Add it and restart the program." ) ;
Console . ReadKey ( ) ;
Environment . Exit ( 3 ) ;
}
2017-04-10 01:14:52 +00:00
OwnerIds = data . GetSection ( "OwnerIds" ) . GetChildren ( ) . Select ( c = > ulong . Parse ( c . Value ) ) . ToImmutableArray ( ) ;
2016-11-15 08:42:06 +00:00
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 ) ] ;
2017-07-05 09:43:36 +00:00
PatreonCampaignId = data [ nameof ( PatreonCampaignId ) ] ? ? "334038" ;
2017-06-21 21:12:24 +00:00
ShardRunCommand = data [ nameof ( ShardRunCommand ) ] ;
ShardRunArguments = data [ nameof ( ShardRunArguments ) ] ;
2017-07-20 21:48:25 +00:00
CleverbotApiKey = data [ nameof ( CleverbotApiKey ) ] ;
2017-09-27 06:32:33 +00:00
var restartSection = data . GetSection ( nameof ( RestartCommand ) ) ;
var cmd = restartSection [ "cmd" ] ;
var args = restartSection [ "args" ] ;
if ( ! string . IsNullOrWhiteSpace ( cmd ) )
RestartCommand = new RestartConfig ( cmd , args ) ;
2017-06-21 21:12:24 +00:00
if ( string . IsNullOrWhiteSpace ( ShardRunCommand ) )
ShardRunCommand = "dotnet" ;
if ( string . IsNullOrWhiteSpace ( ShardRunArguments ) )
2017-10-23 17:46:59 +00:00
ShardRunArguments = "run -c Release -- {0} {1}" ;
2017-06-25 04:09:23 +00:00
var portStr = data [ nameof ( ShardRunPort ) ] ;
if ( string . IsNullOrWhiteSpace ( portStr ) )
ShardRunPort = new NadekoRandom ( ) . Next ( 5000 , 6000 ) ;
else
ShardRunPort = int . Parse ( portStr ) ;
2016-11-15 08:42:06 +00:00
int ts = 1 ;
int . TryParse ( data [ nameof ( TotalShards ) ] , out ts ) ;
TotalShards = ts < 1 ? 1 : ts ;
2017-06-21 21:12:24 +00:00
ulong . TryParse ( data [ nameof ( ClientId ) ] , out ulong clId ) ;
2016-11-15 11:02:23 +00:00
ClientId = clId ;
2016-11-15 08:42:06 +00:00
CarbonKey = data [ nameof ( CarbonKey ) ] ;
var dbSection = data . GetSection ( "db" ) ;
2016-11-15 09:54:56 +00:00
Db = new DBConfig ( string . IsNullOrWhiteSpace ( dbSection [ "Type" ] )
2016-11-15 08:42:06 +00:00
? "sqlite"
: dbSection [ "Type" ] ,
string . IsNullOrWhiteSpace ( dbSection [ "ConnectionString" ] )
2017-09-17 05:28:48 +00:00
? "Data Source=data/NadekoBot.db"
2016-11-15 08:42:06 +00:00
: dbSection [ "ConnectionString" ] ) ;
2016-08-20 20:35:27 +00:00
}
2016-11-15 08:42:06 +00:00
catch ( Exception ex )
2016-10-08 02:25:12 +00:00
{
2016-11-15 10:06:11 +00:00
_log . Fatal ( ex . Message ) ;
_log . Fatal ( ex ) ;
2016-11-15 08:42:06 +00:00
throw ;
2016-10-08 02:25:12 +00:00
}
2016-10-14 19:54:26 +00:00
2016-08-18 23:28:26 +00:00
}
2016-08-21 22:37:39 +00:00
private class CredentialsModel
{
2016-10-14 19:54:26 +00:00
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 ] ;
2016-10-14 19:54:26 +00:00
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 ; } = "" ;
2017-07-20 20:58:19 +00:00
public string CleverbotApiKey { get ; } = "" ;
2016-10-25 11:06:37 +00:00
public string CarbonKey { get ; set ; } = "" ;
2017-09-17 05:28:48 +00:00
public DBConfig Db { get ; set ; } = new DBConfig ( "sqlite" , "Data Source=data/NadekoBot.db" ) ;
2016-10-01 03:49:05 +00:00
public int TotalShards { get ; set ; } = 1 ;
2017-03-05 15:09:54 +00:00
public string PatreonAccessToken { get ; set ; } = "" ;
2017-07-05 09:43:36 +00:00
public string PatreonCampaignId { get ; set ; } = "334038" ;
2017-09-27 06:32:33 +00:00
public string RestartCommand { get ; set ; } = null ;
2017-06-25 04:09:23 +00:00
2017-06-21 21:12:24 +00:00
public string ShardRunCommand { get ; set ; } = "" ;
2017-06-25 04:09:23 +00:00
public string ShardRunArguments { get ; set ; } = "" ;
public int? ShardRunPort { get ; set ; } = null ;
2016-08-24 01:32:48 +00:00
}
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
}
}