public async Task RunAsync(CancellationToken cancellationToken = default) { // check version var update = new UpdateChecker(); await update.RunAsync(); // retrieve auth token var token = new AuthTokenManager(); // discord login var login = new DiscordLogin(_client, token); await login.RunAsync(); try { // configuration manager var config = new ConfigManager(_client); await config.InitializeAsync(); // state management var state = new MudaeStateManager(_client, config); // module initialization var dependencies = new object[] { _client, update, token, login, config, state }; var modules = EnumerateModules(dependencies).ToArray(); foreach (var module in modules) { module.Initialize(); } Log.Warning("Ready!"); // keep running var tasks = modules.Select(m => (name: m.GetType().Name, task: m.RunAsync(cancellationToken))).ToList(); tasks.Add((state.GetType().Name, state.RunAsync(cancellationToken))); await Task.WhenAll(tasks.Select(async x => { var(name, task) = x; try { await task; } catch (TaskCanceledException) { } catch (Exception e) { Log.Warning($"Module '{name}' failed.", e); } })); } finally { await _client.StopAsync(); } }
public async Task RunAsync(CancellationToken cancellationToken = default) { // retrieve auth token var token = new AuthTokenManager(); // discord login var login = new DiscordLogin(_client, token); await login.RunAsync(); try { // configuration manager var config = new ConfigManager(_client); await config.InitializeAsync(); // state management var state = new MudaeStateManager(_client, config); // module initialization var dependencies = new object[] { _client, token, login, config, state }; var modules = EnumerateModules(dependencies).ToArray(); foreach (var module in modules) { module.Initialize(); } Log.Warning("Ready!"); var tasks = modules.Select(m => new { name = m.GetType().Name, func = (Func <Task>)(() => m.RunAsync(cancellationToken)) }) .ToList(); tasks.Add(new { name = state.GetType().Name, func = (Func <Task>)(() => state.RunAsync(cancellationToken)) }); await Task.WhenAll(tasks.Select(async x => { while (true) { try { await x.func(); return; } catch (TaskCanceledException) { } catch (DummyRestartException) { throw; } catch (Exception e) { Log.Warning($"Restarting failed module '{x.name}'.", e); } } })); } finally { await _client.StopAsync(); } }