예제 #1
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);
        }
예제 #2
0
 public void DequeueAllVerification()
 {
     SystemUtil.StopAndDestroyTimer(ref _linkVerificationTimeoutTimer);
     SystemUtil.StopAndDestroyTimer(ref _staticVerificationOutputDelayTimer);
     SystemUtil.StopAndDestroyTimer(ref _guildVerificationOutputTimer);
 }