public async Task SetupAsync(ObjectLock <DiscordClientEx> discordClient) { _discordClient = discordClient; await _discordClient.DoAsync((client) => { client.ReactionAdded += DiscordClient_ReactionAdded; client.ReactionRemoved += DiscordClient_ReactionRemoved; client.ReactionsCleared += DiscordClient_ReactionsCleared; }).ConfigureAwait(false); }
public async Task StartAsync() { if (!Running) { Running = true; var time = DateTime.Now; // reload all our modules await ReloadAsync(null).ConfigureAwait(false); await _discordClient.DoAsync((client) => { client.MessageReceived += MessageReceivedHandler; client.JoinedGuild += JoinedGuildHandler; client.LeftGuild += LeftGuildHandler; }).ConfigureAwait(false); Log.Info("Started after {0:0}ms", (DateTime.Now - time).TotalMilliseconds); } }
public async Task <bool> RunAsync() { if (_firstStart) { Log.Setup(true, false); Log.Info("Starting Ditto..."); } // Try to load the settings from the XML file. // Should this not exist, it will automatically create one. try { Settings = await SettingsConfiguration.ReadAsync("data/settings.xml"); // Write the settings again to update it with any new properties that might have been added. await Settings.WriteAsync("data/settings.xml").ConfigureAwait(false); } catch (Exception ex) { Log.Fatal(ex); return(false); } // Try to initialize the database try { (Database = new DatabaseHandler()).Setup(Settings.Credentials.Sql.Type, Settings.Credentials.Sql.ConnectionString); } catch (Exception ex) { Log.Fatal("Unable to create a connection with the database, please check the file \"/data/settings.xml\"", ex); return(false); } // Try to initialise the service 'Google' try { await(Google = new GoogleService()).SetupAsync(Settings.Credentials.GoogleApiKey).ConfigureAwait(false); } catch (Exception ex) { Log.Error("Could not initialize Google {0}\n", ex.ToString()); } // Try to initialise 'Giphy' try { Giphy = new Giphy(Settings.Credentials.GiphyApiKey); } catch (ApiException ex) { Log.Warn("Could not initialize Giphy {0}\n", ex.ToString()); } // Try to initialise 'Twitch' try { Twitch = new TwitchLib.Api.TwitchAPI(); Twitch.Settings.ClientId = Settings.Credentials.TwitchApiClientId; Twitch.Settings.Secret = Settings.Credentials.TwitchApiSecret; var twitchResult = await Twitch.V5.Auth.CheckCredentialsAsync().ConfigureAwait(false); if (Twitch.Settings.ClientId == null || !twitchResult.Result) { Twitch = null; throw new ApiException("Twitch credentials check failed."); } } catch (Exception ex) { Log.Warn("Could not initialize Twitch {0}\n", ex.ToString()); } // Create our discord client Client?.Dispose(); Client = new ObjectLock <DiscordClientEx>(new DiscordClientEx(new DiscordSocketConfig() { MessageCacheSize = Settings.Cache.AmountOfCachedMessages, LogLevel = LogSeverity.Warning, //TotalShards = 1, ConnectionTimeout = (int)(Settings.Timeout * 60), HandlerTimeout = (int)(Settings.Timeout * 60), DefaultRetryMode = RetryMode.AlwaysRetry, //AlwaysDownloadUsers = true, }), 1, 1); // Various services if (Cache == null) { (Cache = new CacheHandler()).Setup(TimeSpan.FromSeconds(Settings.Cache.CacheTime)); } CommandHandler?.Dispose(); await(CommandHandler = new CommandHandler(Client)).SetupAsync().ConfigureAwait(false); await Client.DoAsync((client) => client.Connected += async() => { // Setup services await CommandHandler.SetupAsync().ConfigureAwait(false); ReactionHandler?.Dispose(); await(ReactionHandler = new ReactionHandler()).SetupAsync(Client).ConfigureAwait(false); PlayingStatusHandler.Setup(TimeSpan.FromMinutes(1)); } ).ConfigureAwait(false); if (_firstStart) { Connected += async() => { // Start services await CommandHandler.StartAsync().ConfigureAwait(false); PlayingStatusHandler.Start(); Connected -= Initialised; }; // Call this once after a successful connection. Connected += Initialised; } await Client.DoAsync((client) => client.Ready += async() => { if (!Running) { Running = true; await Connected().ConfigureAwait(false); } Log.Info("Connected"); if (Type == BotType.Bot) { await client.SetGameAsync(null); await client.SetStatusAsync(UserStatusEx.Online); } } ).ConfigureAwait(false); await Client.DoAsync((client) => client.Disconnected += (e) => { if (Reconnecting) { //_reconnecting = false; } else { Log.Warn("Bot has been disconnected. {0}", (object)(Exiting ? null : $"| {e}")); if (!Exiting && Settings.AutoReconnect && !Reconnecting) { var _ = Task.Run(() => ReconnectAsync()); } } return(Task.CompletedTask); } ).ConfigureAwait(false); await Client.DoAsync((client) => client.LoggedOut += () => { Log.Warn("Bot has logged out."); if (!Exiting && Settings.AutoReconnect && !Reconnecting) { var _ = Task.Run(() => ReconnectAsync()); } return(Task.CompletedTask); } ).ConfigureAwait(false); var autoReconnect = Settings.AutoReconnect; Settings.AutoReconnect = false; try { await LoginAsync().ConfigureAwait(false); Settings.AutoReconnect = autoReconnect; } catch (Exception ex) { Log.Fatal("Failed to login, please check your internet connection and bot token.", ex); return(false); } await Client.DoAsync((c) => c.StartAsync()).ConfigureAwait(false); _firstStart = false; return(true); }