/// <summary>
        ///     Adds required jobs on startup
        /// </summary>
        public void AddRequiredJobs()
        {
            //Read in the last playtest event from the DB
            _calendar.BootStorePreviousPlaytestEvent();

            _ = _log.LogMessage("Adding required scheduled jobs...", false, color: LOG_COLOR);

            //Ask Google API for new tests every 60 seconds.
            JobManager.AddJob(async() => await _calendar.UpdateTestEventCache(), s => s
                              .WithName("[UpdatePlaytestEventCacheNow]").ToRunNow());

            JobManager.AddJob(async() => await _calendar.UpdateTestEventCache(), s => s
                              .WithName("[UpdatePlaytestEventCache]").ToRunEvery(60).Seconds());

            //Delay the announcement updates so the calendar refreshes first.
            Task.Run(() =>
            {
                Thread.Sleep(10000);
                //Add schedule for playtest information
                JobManager.AddJob(async() => await _playtestService.PostOrUpdateAnnouncement("csgo"), s => s
                                  .WithName("[PostOrUpdateAnnouncement_CSGO]").ToRunEvery(60).Seconds());


                JobManager.AddJob(async() => await _playtestService.PostOrUpdateAnnouncement("tf2"), s => s
                                  .WithName("[PostOrUpdateAnnouncement_TF2]").ToRunEvery(60).Seconds());
            });

            //Early refresh on playtest announcements.
            JobManager.AddJob(async() => await _playtestService.PostOrUpdateAnnouncement("csgo"), s => s
                              .WithName("[PostOrUpdateAnnouncementNow_CSGO]").ToRunOnceIn(15).Seconds());

            JobManager.AddJob(async() => await _playtestService.PostOrUpdateAnnouncement("tf2"), s => s
                              .WithName("[PostOrUpdateAnnouncementNow_TF2]").ToRunOnceIn(15).Seconds());

            //Reattach to the old announcement message quickly
            JobManager.AddJob(async() => await _playtestService.TryAttachPreviousAnnounceMessage(), s => s
                              .WithName("[TryAttachPreviousAnnounceMessage]").ToRunOnceIn(5).Seconds());

            //On start up schedule of playtest announcements
            JobManager.AddJob(() => _playtestService.ScheduleAllPlaytestAnnouncements(), s => s
                              .WithName("[SchedulePlaytestAnnouncementsBoot]").ToRunOnceIn(10).Seconds());

            //Add schedule for playing information
            JobManager.AddJob(async() => await UpdatePlaying(), s => s
                              .WithName("[PlayingUpdate]").ToRunEvery(20).Seconds());

            //Add schedule for playtest count update, will do every few hours, and now to seed the value.
            JobManager.AddJob(UpdatePlayTestCount, s => s
                              .WithName("[PlaytestCountUpdate]").ToRunEvery(2).Hours());
            JobManager.AddJob(UpdatePlayTestCount, s => s
                              .WithName("[PlaytestCountUpdateNow]").ToRunNow());

            //Update playerbase
            JobManager.AddJob(async() => await UpdatePlayerbase(), s => s
                              .WithName("[PlayerbaseUpdate]").ToRunEvery(1).Days().At(0, 00));

            //Daily Faceit Demo Fetching
            JobManager.AddJob(async() => await DailyDemoRequests(), s => s
                              .WithName("[FaceitDemoRequest]").ToRunEvery(1).Days().At(0, 00));

            //Re-add joined users so they get welcome message and playtester role.
            //This would only happen if the bot restarts after someone joins, but didn't get the welcome message.
            foreach (var user in DatabaseUtil.GetAllUserJoins())
            {
                try
                {
                    //Test getting user in a try catch, if we can't they left the server.
                    var validUser = _dataService.Guild.GetUser(user.UserId);

                    //Send welcome message right away, or wait?
                    if (DateTime.Now > user.JoinTime.AddMinutes(10))
                    {
                        //Timer expired, schedule now
                        JobManager.AddJob(async() => await _userHandler.UserWelcomeMessage(validUser), s => s
                                          .WithName($"[UserJoin_{validUser.Id}]").ToRunOnceIn(10).Seconds());
                    }
                    else
                    {
                        //Not passed, scheduled ahead
                        JobManager.AddJob(async() => await _userHandler.UserWelcomeMessage(validUser), s => s
                                          .WithName($"[UserJoin_{validUser.Id}]").ToRunOnceAt(user.JoinTime.AddMinutes(10)));
                    }
                }
                catch
                {
                    //If we cannot get a user, that means that user left the server. So remove them.
                    if (_dataService.RSettings.ProgramSettings.Debug)
                    {
                        _ = _log.LogMessage($"Cannot re-add user join for ID {user.UserId}" +
                                            "because they left the server.", false, color: LOG_COLOR);
                    }

                    DatabaseUtil.RemoveJoinedUser(user.UserId);
                }
            }

            //Re-add user mutes
            foreach (var user in DatabaseUtil.GetAllActiveUserMutes())
            {
                //Send welcome message right away, or wait?
                if (DateTime.Now > user.MuteTime.AddMinutes(user.Duration))
                {
                    //Timer expired, schedule now
                    JobManager.AddJob(async() => await _dataService.UnmuteUser(user.UserId), s => s
                                      .WithName($"[UnmuteUser_{user.UserId}]").ToRunOnceIn(20).Seconds());
                }
                else
                {
                    //Not passed, scheduled ahead
                    JobManager.AddJob(async() => await _dataService.UnmuteUser(user.UserId), s => s
                                      .WithName($"[UnmuteUser_{user.UserId}]").ToRunOnceAt(user.MuteTime.AddMinutes(user.Duration)));
                }
            }

            //Re-add server reservations.
            foreach (var reservation in DatabaseUtil.GetAllServerReservation())
            {
                string mention = null;

                try
                {
                    mention = _dataService.Guild.GetUser(reservation.UserId).Mention;
                }
                catch
                {
                    //Can't get user don't do a mention
                }

                //Send welcome message right away, or wait?
                if (DateTime.Now > reservation.StartTime.AddHours(3))
                {
                    //Timer expired, schedule now
                    JobManager.AddJob(async() => await _dataService.BotChannel.SendMessageAsync($"{mention}",
                                                                                                embed: _reservationService.ReleaseServer(reservation.UserId,
                                                                                                                                         "The reservation has expired.", _dataService.BotChannel)),
                                      s => s.WithName(
                                          $"[TSRelease_{GeneralUtil.GetServerCode(reservation.ServerId)}_{reservation.UserId}]")
                                      .ToRunOnceIn(15).Seconds());
                }
                else
                {
                    //Not passed, scheduled ahead
                    JobManager.AddJob(async() => await _dataService.BotChannel.SendMessageAsync($"{mention}",
                                                                                                embed: _reservationService.ReleaseServer(reservation.UserId,
                                                                                                                                         "The reservation has expired.", _dataService.BotChannel)),
                                      s => s.WithName(
                                          $"[TSRelease_{GeneralUtil.GetServerCode(reservation.ServerId)}_{reservation.UserId}]")
                                      .ToRunOnceAt(reservation.StartTime.AddHours(3)));
                }
            }

            DisplayScheduledJobs();
        }
Exemple #2
0
        /// <summary>
        /// Adds required jobs on startup
        /// </summary>
        public void AddRequiredJobs()
        {
            _ = _log.LogMessage("Adding required scheduled jobs...", false, color: LOG_COLOR);

            //Add schedule for playtest information
            JobManager.AddJob(async() => await _playtestService.PostOrUpdateAnnouncement(), s => s
                              .WithName("[PostOrUpdateAnnouncement]").ToRunEvery(60).Seconds());

            //Reattach to the old announcement message quickly
            JobManager.AddJob(async() => await _playtestService.TryAttachPreviousAnnounceMessage(), s => s
                              .WithName("[TryAttachPreviousAnnounceMessage]").ToRunOnceIn(3).Seconds());

            //On start up schedule of playtest announcements
            JobManager.AddJob(() => _playtestService.SchedulePlaytestAnnouncements(), s => s
                              .WithName("[SchedulePlaytestAnnouncementsBoot]").ToRunOnceIn(6).Seconds());

            //Add schedule for playing information
            JobManager.AddJob(async() => await UpdatePlaying(), s => s
                              .WithName("[PlayingUpdate]").ToRunEvery(20).Seconds());

            //Add schedule for playtest count update, will do every few hours, and now to seed the value.
            JobManager.AddJob(UpdatePlayTestCount, s => s
                              .WithName("[PlayingUpdate]").ToRunEvery(2).Hours());
            JobManager.AddJob(UpdatePlayTestCount, s => s
                              .WithName("[PlayingUpdate]").ToRunNow());

            //Re-add joined users so they get welcome message and playtester role.
            //This would only happen if the bot restarts after someone joins, but didn't get the welcome message.
            foreach (var user in DatabaseHandler.GetAllUserJoins())
            {
                try
                {
                    //Test getting user in a try catch, if we can't they left the server.
                    var validUser = _data.Guild.GetUser(user.UserId);

                    //Send welcome message right away, or wait?
                    if (DateTime.Now > user.JoinTime.AddMinutes(10))
                    {
                        //Timer expired, schedule now
                        JobManager.AddJob(async() => await _userHandler.UserWelcomeMessage(validUser), s => s
                                          .WithName($"[UserJoin_{validUser.Id}]").ToRunOnceIn(10).Seconds());
                    }
                    else
                    {
                        //Not passed, scheduled ahead
                        JobManager.AddJob(async() => await _userHandler.UserWelcomeMessage(validUser), s => s
                                          .WithName($"[UserJoin_{validUser.Id}]").ToRunOnceAt(user.JoinTime.AddMinutes(10)));
                    }
                }
                catch
                {
                    //If we cannot get a user, that means that user left the server. So remove them.
                    if (_data.RSettings.ProgramSettings.Debug)
                    {
                        _ = _log.LogMessage($"Cannot re-add user join for ID {user.UserId}" +
                                            $"because they left the server.", false, color: LOG_COLOR);
                    }

                    DatabaseHandler.RemoveJoinedUser(user.UserId);
                }
            }

            //Re-add user mutes
            foreach (var user in DatabaseHandler.GetAllActiveUserMutes())
            {
                //Send welcome message right away, or wait?
                if (DateTime.Now > user.MuteTime.AddMinutes(user.Duration))
                {
                    //Timer expired, schedule now
                    JobManager.AddJob(async() => await _data.UnmuteUser(user.UserId), s => s
                                      .WithName($"[UnmuteUser_{user.UserId}]").ToRunOnceIn(20).Seconds());
                }
                else
                {
                    //Not passed, scheduled ahead
                    JobManager.AddJob(async() => await _data.UnmuteUser(user.UserId), s => s
                                      .WithName($"[UnmuteUser_{user.UserId}]").ToRunOnceAt(user.MuteTime.AddMinutes(user.Duration)));
                }
            }

            //Display what jobs we have scheduled
            foreach (var allSchedule in JobManager.AllSchedules)
            {
                _ = _log.LogMessage($"{allSchedule.Name} runs at: {allSchedule.NextRun}",
                                    false, color: LOG_COLOR);
            }
        }