//        private async Task UserLeftEventHandler(SocketGuildUser user)
//        {
//
//        }

        public async Task UserWelcomeMessage(SocketGuildUser user)
        {
            try
            {
                await _log.LogMessage($"Welcomed {user.Username} at {DateTime.Now}, and assigning them the Playtester role!");

                await user.AddRoleAsync(_data.PlayTesterRole);

                await user.SendMessageAsync(embed : WelcomeEmbed(user));
            }
            catch
            {
                await _log.LogMessage($"Attempted to send welcome message to {user.Username}, but failed. " +
                                      $"They either have DMs off, or left the server.");
            }
            DatabaseHandler.RemoveJoinedUser(user.Id);
        }
Exemplo n.º 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);
            }
        }