public void DBConnector(int idIns, string[] reqIns) { Console.WriteLine("{0} [INFO] Connecting to database", DateTime.Now.ToString("H:mm:ss")); var conf = new Config.IniFile("config.ini"); var host = ""; var user = ""; var pass = ""; var name = ""; try { host = conf.Read("DBHost", "SQL"); user = conf.Read("DBUser", "SQL"); pass = conf.Read("DBPass", "SQL"); name = conf.Read("DBName", "SQL"); } catch { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} [ERROR] Cannot read variables (SQL) from config. Did you fill in the config?", DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); } string cs = @"server=" + host + ";userid=" + user + ";password="******";database=" + name; MySqlConnection conn = null; try { conn = new MySqlConnection(cs); conn.Open(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = "INSERT INTO `requests` (session, name, author, state) VALUES (@Session, @Name, @Author, 'active')"; cmd.Prepare(); cmd.Parameters.AddWithValue("@Name", reqIns[1]); cmd.Parameters.AddWithValue("@Author", reqIns[0]); cmd.Parameters.AddWithValue("@Session", idIns); cmd.ExecuteNonQuery(); Console.WriteLine("{0} [INFO] Successfully added request to database", DateTime.Now.ToString("H:mm:ss")); } catch (MySqlException ex) { switch (ex.Number) { case 1042: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{1} [ERROR] Cannot connect to database server:\n{0}", ex.ToString(), DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); MessageBox.Show("Cannot connect to database server!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Program.open = false; break; case 0: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{1} [ERROR] Invalid username/password:\n{0}", ex.ToString(), DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); MessageBox.Show("Authentication to host failed: Access denied.\nPlease, check the config.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); Program.open = false; break; case 1366: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{1} [ERROR] Cannot add request to the database\n", ex.Number, DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); Program.open = true; break; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{2} [ERROR] An error {1} occurred:\n{0}", ex.ToString(), ex.Number, DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); MessageBox.Show("An error occurred!\nMore info in console.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Program.open = false; break; } } finally { if (conn != null) { conn.Close(); } } }
static void Main(string[] args) { if (File.Exists("temp/RequestYT-Setup.exe")) { try { File.Delete("temp/RequestYT-Setup.exe"); } catch { } } DBConnect db = new DBConnect(); var conf = new Config.IniFile("config.ini"); var chid = ""; var api = ""; int refresh = 500; if (!File.Exists("usernameCache.sqlite")) { UserCache.CreateCache(); } if (!File.Exists("config.ini")) { conf.Write("RefreshTime", "", "Settings"); conf.Write("DBHost", "", "SQL"); conf.Write("DBUser", "", "SQL"); conf.Write("DBPass", "", "SQL"); conf.Write("DBName", "", "SQL"); conf.Write("ChatID", "", "YTChat"); conf.Write("APIKey", "", "YTChat"); conf.Write("AutoUpdate", "true", "Updater"); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} [ERROR] Cannot find config.ini. Creating new one. Please fill the config in.", DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); open = false; goto skip; } try { chid = conf.Read("ChatID", "YTChat"); api = conf.Read("APIKey", "YTChat"); refresh = Convert.ToInt32(conf.Read("RefreshTime", "Settings")); try { autoupdate = conf.Read("AutoUpdate", "Updater"); } catch { autoupdate = "false"; try { conf.Write("AutoUpdate", "false", "Updater"); } catch { } } } catch { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} [ERROR] Cannot read variables from config. Did you fill in the config?", DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); open = false; goto skip; } Console.WriteLine("{1} [INFO] Request system v{0}\n{1} [INFO] Programmed by Creeperman007", v, DateTime.Now.ToString("H:mm:ss")); Thread.Sleep(250); Version.Get(v); Thread.Sleep(500); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("{1} [INFO] Session key: {0}\n{1} [INFO] Key is used for accessing the web interface.", session, DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); request[0] = "Test"; request[1] = "Testing connection."; Console.WriteLine("{0} [TEST] Testing connection with database", DateTime.Now.ToString("H:mm:ss")); db.DBConnector(session, request); Console.WriteLine("{0} [TEST] Ending testing session", DateTime.Now.ToString("H:mm:ss")); skip: while (open == true) { request = ChatRequest.GetChat(chid, api); if (Compare(request[1]) && request[1] != requestLast && !request[1].Contains("SongName") && request[1].Contains(' ')) { Console.WriteLine("{0} [INFO] Adding new request", DateTime.Now.ToString("H:mm:ss")); try { db.DBConnector(session, request); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{1} [ERROR] An error occurred:\n{0}", ex.ToString(), DateTime.Now.ToString("H:mm:ss")); Console.ResetColor(); MessageBox.Show("An error occurred!\nMore info in console.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); open = false; } } else { Thread.Sleep(refresh); } requestLast = request[1]; } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }