using System; using System.IO; using System.Windows.Forms; namespace DiscordBotController { [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044")] public partial class MainForm : Form { private SSHManager sshManager = null!; #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. private TextBox txtConsoleOutput; // Add TextBox control private Button btnCopy; // Add Button control #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. public MainForm() { InitializeComponent(); InitializeSSHManager(); } private void InitializeSSHManager() { string botIp = Environment.GetEnvironmentVariable("RHPC_IP") ?? string.Empty; string botUsername = Environment.GetEnvironmentVariable("RHPC_USERNAME") ?? string.Empty; string password = Environment.GetEnvironmentVariable("RHPC_PASSWORD") ?? string.Empty; if (string.IsNullOrEmpty(botIp) || string.IsNullOrEmpty(botUsername) || string.IsNullOrEmpty(password)) { ShowErrorMessage("Missing environment variables. Please make sure RHPC_IP, RHPC_USERNAME, and RHPC_PASSWORD are set."); Environment.Exit(1); } try { sshManager = new SSHManager(botIp, botUsername, password); // Pass password } catch (Exception ex) { ShowErrorMessage("An error occurred: " + ex.Message); Environment.Exit(1); } } private void btnStartBot_Click(object sender, EventArgs e) { sshManager.StartBotService(output => AppendConsoleOutput(output)); } private void btnStopBot_Click(object sender, EventArgs e) { try { if (sshManager != null) { sshManager.StopBotService(output => AppendConsoleOutput(output)); } else { ShowErrorMessage("SSHManager is not properly initialized."); } } catch (Exception ex) { ShowErrorMessage("An error occurred: " + ex.Message); } } private void ShowErrorMessage(string message) { using (var errorDialog = new ErrorDialog(message)) { errorDialog.ShowDialog(); } } private void AppendConsoleOutput(string output) { if (txtConsoleOutput != null) { txtConsoleOutput.AppendText(output + Environment.NewLine); } } } public class ErrorDialog : Form { private TextBox txtErrorMessage; private Button? btnCopy; public ErrorDialog(string errorMessage) { InitializeComponent(); txtErrorMessage.Text = errorMessage; } private void InitializeComponent() { txtErrorMessage = new TextBox(); btnCopy = new Button(); // Set properties for txtErrorMessage txtErrorMessage.Multiline = true; txtErrorMessage.Dock = DockStyle.Fill; txtErrorMessage.ReadOnly = true; // Set properties for btnCopy btnCopy.Text = "Copy"; btnCopy.Dock = DockStyle.Bottom; btnCopy.Click += BtnCopy_Click; // Add controls to the form Controls.Add(txtErrorMessage); Controls.Add(btnCopy); // Arrange controls using layout logic (e.g., FlowLayoutPanel) } private void BtnCopy_Click(object? sender, EventArgs e) { if (txtErrorMessage != null) { Clipboard.SetText(txtErrorMessage.Text); MessageBox.Show("Error message copied to clipboard."); } } } }