public async Task <CommandExecuteResult> StartDiscordAppAsync(ulong clientId) { await StopDiscordAppAsync(clientId).ConfigureAwait(false); using (var context = SqliteDatabaseService.GetContext(true)) { var discordApp = await context.DiscordAppTable.Where(a => a.ClientId == clientId).FirstOrDefaultAsync(); if (discordApp == null) { return(CommandExecuteResult.FromError("Discord App is not registered in the database.")); } var discordAppInstance = new DiscordAppInstance(discordApp.ClientId, discordApp.BotToken); AddListener(discordAppInstance); try { await discordAppInstance.DiscordAppLoginAsync().ConfigureAwait(false); } catch (Exception ex) { await discordAppInstance.DiscordAppLogoutAsync().ConfigureAwait(false); await LoggerService.LogErrorMessageAsync(ex).ConfigureAwait(false); } if (!discordAppInstance.IsLoggedIn) { return(CommandExecuteResult.FromError("Discord App encountered an error while trying to authenticate.")); } try { await discordAppInstance.DiscordAppStartAsync().ConfigureAwait(false); } catch (Exception ex) { await LoggerService.LogErrorMessageAsync(ex).ConfigureAwait(false); } if (!discordAppInstance.IsStarted) { RemoveListener(discordAppInstance); return(CommandExecuteResult.FromError("Discord App encountered an error while trying to connect to the discord gateway server.")); } DiscordAppInstances.Add(discordAppInstance); return(CommandExecuteResult.FromSuccess("Discord App is now starting!")); } }
public async Task <CommandExecuteResult> StopDiscordAppAsync(ulong clientId) { foreach (var discordAppInstance in DiscordAppInstances) { if (discordAppInstance.ClientId != clientId) { continue; } var instanceName = discordAppInstance.DiscordShardedClient?.CurrentUser?.ToString(); try { await discordAppInstance.DiscordAppStopAsync().ConfigureAwait(false); } catch (Exception ex) { await LoggerService.LogErrorMessageAsync(ex).ConfigureAwait(false); } try { await discordAppInstance.DiscordAppLogoutAsync().ConfigureAwait(false); } catch (Exception ex) { await LoggerService.LogErrorMessageAsync(ex).ConfigureAwait(false); } RemoveListener(discordAppInstance); await discordAppInstance.DisposeAsync().ConfigureAwait(false); _discordAppInstances.Remove(discordAppInstance); return(CommandExecuteResult.FromSuccess($"Discord App {instanceName} has stopped.")); } return(CommandExecuteResult.FromError("Discord App is not running!")); }