Пример #1
0
        void StopClient()
        {
            _status = "Shutting down";

            // Stop various timers that may have been set up so they do not trigger while the reset is ongoing
            DLConfig.Instance.DequeueAllVerification();
            SystemUtil.StopAndDestroyTimer(ref _discordDataMaybeAvailable);

            ShutdownModules();

            if (DiscordClient != null)
            {
                StopRelaying();

                // If DisconnectAsync() is called in the GUI thread, it will cause a deadlock
                SystemUtil.SynchronousThreadExecute(() =>
                {
                    DiscordClient.DisconnectAsync().Wait();
                });
                DiscordClient.Dispose();
                DiscordClient = null;

                OnClientStopped?.Invoke(this, EventArgs.Empty);
            }
        }
Пример #2
0
        public void Stop()
        {
            if (!_initialized)
            {
                return;
            }

            SystemUtil.StopAndDestroyTimer(ref _flushTimer);
            try
            {
                _writer.Close();
            }
            catch (Exception e)
            {
                Logger.Error("Error occurred while attempting to close the chatlog file writer. Error message: " + e);
            }

            _writer      = null;
            _initialized = false;
        }
Пример #3
0
 public void DequeueAllVerification()
 {
     SystemUtil.StopAndDestroyTimer(ref _linkVerificationTimeoutTimer);
     SystemUtil.StopAndDestroyTimer(ref _staticVerificationOutputDelayTimer);
     SystemUtil.StopAndDestroyTimer(ref _guildVerificationOutputTimer);
 }
Пример #4
0
        private bool SetUpClient()
        {
            _status = "Setting up client";

            bool BotTokenIsNull = string.IsNullOrWhiteSpace(DLConfig.Data.BotToken);

            if (BotTokenIsNull)
            {
                DLConfig.Instance.VerifyConfig(DLConfig.VerificationFlags.Static); // Make the user aware of the empty bot token
            }

            if (BotTokenIsNull)
            {
                return(false);                // Do not attempt to initialize if the bot token is empty
            }
            try
            {
                // Create the new client
                DiscordClient = new DiscordClient(new DiscordConfiguration
                {
                    AutoReconnect   = true,
                    Token           = DLConfig.Data.BotToken,
                    TokenType       = TokenType.Bot,
                    MinimumLogLevel = DLConfig.Data.BackendLogLevel
                });

                DiscordClient.ClientErrored += async(client, args) => { Logger.Debug("A Discord client error occurred. Error messages was: " + args.EventName + " " + args.Exception.ToString()); };
                DiscordClient.SocketErrored += async(client, args) => { Logger.Debug("A socket error occurred. Error message was: " + args.Exception.ToString()); };
                DiscordClient.SocketClosed  += async(client, args) => { Logger.DebugVerbose("Socket Closed: " + args.CloseMessage + " " + args.CloseCode); };
                DiscordClient.Resumed       += async(client, args) => { Logger.Debug("Resumed connection"); };
                DiscordClient.Ready         += async(client, args) =>
                {
                    _status = "Awaiting Discord Caching...";
                    DLConfig.Instance.EnqueueFullVerification();

                    _discordDataMaybeAvailable = new Timer(innerArgs =>
                    {
                        OnDiscordMaybeReady?.Invoke(this, EventArgs.Empty);
                        SystemUtil.StopAndDestroyTimer(ref _discordDataMaybeAvailable);
                        _status = "Connected and running";
                    }, null, FIRST_DISPLAY_UPDATE_DELAY_MS, Timeout.Infinite);
                };

                DiscordClient.GuildAvailable += async(client, args) =>
                {
                    DLConfig.Instance.EnqueueGuildVerification();
                };

                DiscordClient.MessageDeleted += async(client, args) =>
                {
                    Modules.ForEach(async module => await module.OnMessageDeleted(args.Message));
                };

                // Set up the client to use CommandsNext
                _commands = DiscordClient.UseCommandsNext(new CommandsNextConfiguration
                {
                    StringPrefixes = DLConfig.Data.DiscordCommandPrefix.SingleItemAsEnumerable()
                });
                _commands.RegisterCommands <DiscordCommands>();

                OnClientStarted?.Invoke(this, EventArgs.Empty);
                return(true);
            }
            catch (Exception e)
            {
                Logger.Error("Error occurred while creating the Discord client. Error message: " + e);
            }

            return(false);
        }