public SongBot(SongConfig config) { _instance = this; this._config = config; this._discord = new DiscordShardedClient(this._config.Discord.Build()); this._discord.DebugLogger.LogMessageReceived += (_, e) => { this._log.Log(e.Level.ToNLog(), e.Exception, " ({0}) -> {1}", e.Application, e.Message); }; }
static async Task MainAsync() { Console.CancelKeyPress += (sender, e) => { if (!_cts.IsCancellationRequested) { _cts.Cancel(); } e.Cancel = true; }; var configFile = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), "Config.json")); if (!configFile.Exists) { using (var fs = configFile.CreateText()) { fs.Write(JsonConvert.SerializeObject(SongConfig.Default, Formatting.Indented)); fs.Flush(); } _log.Warn("Default configuration file created."); Environment.Exit(1); } SongConfig config = default; using (var fs = configFile.OpenText()) config = JsonConvert.DeserializeObject <SongConfig>(fs.ReadToEnd()); if (string.IsNullOrEmpty(config.Discord.Token)) { throw new InvalidConfigurationException("Token must be valid."); } var bot = new SongBot(config); await bot.InitializeAsync(); while (!_cts.IsCancellationRequested) { await Task.Delay(1); } await bot.ShutdownAsync(); }