Esempio n. 1
0
        /// <summary>
        /// Main proceedure
        /// </summary>
        private async void _timer_Callback(object state)
        {
            try
            {
                _timer.Change(Timeout.Infinite, Timeout.Infinite);

                var tasks = _timedTasks.Keys.Where(x => x.When <= DateTime.Now).ToArray();
                foreach (var task in tasks)
                {
                    bool tmp;
                    _timedTasks.TryRemove(task, out tmp);

                    await task.Callback();
                }

                if (_config.InfoTopicChannel != null)
                {
                    if (DateTime.Now - _prevTopicUpdate > TimeSpan.FromSeconds(60))
                    {
                        _prevTopicUpdate = DateTime.Now;
                        var topicUpdateTask = Task.Run(async() =>
                        {
                            var sb = new StringBuilder();
                            sb.AppendLine(@"Type !help to get started | ");

                            var n = 0;
                            foreach (var serverContext in _contextManager.Servers)
                            {
                                var lastUpdate = serverContext.LastUpdate;
                                var nextUpdate = serverContext.ApproxTimeUntilNextUpdate;

                                var nextUpdateTmp    = nextUpdate?.ToStringCustom();
                                var nextUpdateString = (nextUpdate.HasValue ? (!string.IsNullOrWhiteSpace(nextUpdateTmp) ? $", Next Update in ~{nextUpdateTmp}" : ", waiting for new update ...") : "");
                                var lastUpdateString = lastUpdate.ToStringWithRelativeDay();
                                sb.Append($@"{serverContext.Config.Key}: Updated {lastUpdateString}{nextUpdateString}");
                                if (++n < _contextManager.Servers.Length)
                                {
                                    sb.AppendLine(" | ");
                                }
                            }

                            try
                            {
                                await _discordManager.EditChannelByNameOnAllServers(_config.InfoTopicChannel, topic: sb.ToString());
                            }
                            catch (Exception ex)
                            {
                                Logging.LogException("Error when attempting to change bot info channel topic", ex, GetType(), LogLevel.ERROR, ExceptionLevel.Ignored);
                            }
                        });
                    }
                }

                if (DateTime.Now - _prevServerStatusUpdate > TimeSpan.FromMinutes(1))
                {
                    _prevServerStatusUpdate = DateTime.Now;
                    var banUpdateTask = Task.Run(async() =>
                    {
                        foreach (var server in _contextManager.Servers)
                        {
                            await server.Steam.GetServerStatus();
                        }
                    });
                }

                if (DateTime.Now - _prevTimedBansUpdate > TimeSpan.FromMinutes(5))
                {
                    _prevTimedBansUpdate = DateTime.Now;
                    var banUpdateTask = Task.Run(async() => await _votingManager.TimerUpdateVotes());
                }
            }
            catch (Exception ex)
            {
                Logging.LogException("Unhandled exception in bot timer method", ex, GetType(), LogLevel.ERROR, ExceptionLevel.Unhandled);
            }
            finally
            {
                _timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
            }
        }