Example #1
0
        public async Task ConnectedAsync()
        {
            await CommandWindowLogger.WriteMessageAsync("Client successfully reconnected");

            _cts.Cancel();
            _cts = new CancellationTokenSource();
        }
Example #2
0
 public BotReady(DiscordSocketClient client)
 {
     try
     {
         _client = client;
     }
     catch (Exception ex)
     {
         _ = CommandWindowLogger.LogMessageAsync(
             new LogMessage(LogSeverity.Critical, MethodBase.GetCurrentMethod().Name, ex.Message, ex));
     }
 }
Example #3
0
 public async static Task InitializeDomain(DiscordSocketClient client)
 {
     try
     {
         await Retrier.Attempt(() => AssignDomainFields(client), TimeSpan.FromSeconds(3), 5);
     }
     catch (Exception ex)
     {
         await CommandWindowLogger.LogMessageAsync(
             new LogMessage(LogSeverity.Critical, MethodBase.GetCurrentMethod().Name, ex.Message, ex));
     }
 }
Example #4
0
 public static async Task RestartBot()
 {
     try
     {
         _tokenSource.Cancel();
         _tokenSource = null;
         await Main();
     }
     catch (Exception ex)
     {
         await CommandWindowLogger.LogMessageAsync(
             new LogMessage(LogSeverity.Critical, DiscordLogger.GetAsyncMethodName(), ex.Message, ex));
     }
 }
Example #5
0
        internal async Task ReadyProcedures()
        {
            try
            {
                await Domain.InitializeDomain(_client);

                await Domain.GetLogChannel().SendMessageAsync("Bot initialized");
            }
            catch (Exception ex)
            {
                await CommandWindowLogger.LogMessageAsync(
                    new LogMessage(LogSeverity.Critical, DiscordLogger.GetAsyncMethodName(), ex.Message, ex));
            }
        }
Example #6
0
        public async Task DisconnectedAsync(Exception _e)
        {
            await CommandWindowLogger.WriteMessageAsync("Client disconnected, checking to reconnect");

            if (_attemptReset)
            {
                _ = Task.Delay(_timeout, _cts.Token).ContinueWith(async _ =>
                {
                    await CommandWindowLogger.WriteMessageAsync("Timeout expired, continuing to check client state...");
                    bool success = await CheckStateAsync();

                    if (success)
                    {
                        await CommandWindowLogger.WriteMessageAsync("Client reconnected succesfully!");
                    }
                });
            }
        }
Example #7
0
        public static void InitializeConfig()
        {
            try
            {
                var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var folder           = "Config";
                var fileName         = "Config.Json";

                var path = Path.Combine(assemblyLocation, folder, fileName);


                using (StreamReader file = File.OpenText(path))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    BotSettings = (BotSettings)serializer.Deserialize(file, typeof(BotSettings));
                }
            }
            catch (Exception ex)
            {
                CommandWindowLogger.LogMessageAsync(
                    new LogMessage(LogSeverity.Critical, MethodBase.GetCurrentMethod().Name, ex.Message, ex));
            }
        }
Example #8
0
        private async Task <bool> CheckStateAsync()
        {
            if (_client.ConnectionState == ConnectionState.Connected)
            {
                return(true);
            }

            if (_attemptReset)
            {
                for (int i = 0; i < _attemptRetries; i++)
                {
                    await CommandWindowLogger.WriteMessageAsync("Attempting to restart DarkAge bot");

                    var timeout = Task.Delay(_timeout);
                    var connect = Program.RestartBot();

                    var task = await Task.WhenAny(timeout, connect);

                    if (task != timeout)
                    {
                        if (connect.IsFaulted)
                        {
                            await CommandWindowLogger.WriteMessageAsync("CRITICAL ERROR - " + connect.Exception);
                        }
                        else if (connect.IsCompletedSuccessfully)
                        {
                            return(true);
                        }
                    }
                }
            }

            await CommandWindowLogger.WriteMessageAsync("Client has been disconnected from Discord");

            _cts.Cancel();
            return(false);
        }