public static async void DownloadPlaytestDemo(PlaytestCommandInfo playtestCommandInfo)
        {
            var server = DatabaseHandler.GetTestServer(playtestCommandInfo.ServerAddress);

            if (server == null)
            {
                return;
            }

            string localPath = $"{_data.RSettings.ProgramSettings.PlaytestDemoPath}\\{playtestCommandInfo.StartDateTime:yyyy}" +
                               $"\\{playtestCommandInfo.StartDateTime:MM} - {playtestCommandInfo.StartDateTime:MMMM}\\{playtestCommandInfo.DemoName}";

            switch (server.FtpType.ToLower())
            {
            case "ftps":
            case "ftp":
                using (var client = new FtpClient(server.Address, server.FtpUser, server.FtpPassword))
                {
                    if (server.FtpType == "ftps")
                    {
                        client.EncryptionMode       = FtpEncryptionMode.Explicit;
                        client.SslProtocols         = SslProtocols.Tls;
                        client.ValidateCertificate += (control, e) => { e.Accept = true; };
                    }

                    try
                    {
                        client.Connect();
                    }
                    catch (Exception e)
                    {
                        await _log.LogMessage($"Failed to connect to FTP server. {server.Address}\n {e.Message}", alert : true);

                        return;
                    }

                    try
                    {
                        //Download Demo
                        client.DownloadFile($"{localPath}\\{playtestCommandInfo.DemoName}.dem",
                                            GetFile(client, server.FtpPath, playtestCommandInfo.DemoName));

                        //Download BSP
                        string bspFile = GetFile(client,
                                                 $"{server.FtpPath}/maps/workshop/{playtestCommandInfo.WorkshopId}", ".bsp");
                        client.DownloadFile($"{localPath}\\{Path.GetFileName(bspFile)}", bspFile);
                    }
                    catch (Exception e)
                    {
                        await _log.LogMessage($"Failed to download file from playtest server. {server.Address}\n{e.Message}");
                    }

                    client.Disconnect();

                    await _log.LogMessage($"```Listing of test download directory:\n{string.Join("\n", Directory.GetFiles(localPath))}```");
                }

                break;

            case "sftp":
                using (var client = new SftpClient(server.Address, server.FtpUser, server.FtpPassword))
                {
                    try
                    {
                        client.Connect();
                    }
                    catch (Exception e)
                    {
                        await _log.LogMessage($"Failed to connect to SFTP server. {server.Address}\n {e.Message}", alert : true);

                        return;
                    }

                    Directory.CreateDirectory(localPath);

                    try
                    {
                        var remoteDemoFile = GetFile(client, server.FtpPath, playtestCommandInfo.DemoName);
                        using (Stream stream = File.OpenWrite($"{localPath}\\{remoteDemoFile.Name}"))
                        {
                            client.DownloadFile(remoteDemoFile.FullName, stream);
                        }

                        var remoteBspFile = GetFile(client, $"{server.FtpPath}/maps/workshop/{playtestCommandInfo.WorkshopId}", ".bsp");
                        using (Stream stream = File.OpenWrite($"{localPath}\\{remoteBspFile.Name}"))
                        {
                            client.DownloadFile(remoteBspFile.FullName, stream);
                        }
                    }
                    catch (Exception e)
                    {
                        await _log.LogMessage($"Failed to download file from playtest server. {server.Address}\n{e.Message}");
                    }

                    client.Disconnect();

                    await _log.LogMessage($"```Listing of test download directory:\n{string.Join("\n", Directory.GetFiles(localPath))}```");
                }

                break;

            default:
                await _log.LogMessage($"The FTP type on the server is incorrectly set. {server.Address} is using {server.FtpType}", alert : true);

                break;
            }
        }
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);
            }
        }