/// <summary> /// Add new quote to DBfile with specified Quote. /// Does not check if quote exists, make sure it doesn't prior calling. /// </summary> /// <param name="newQuote">The new quote to be added to the database</param> public static void AddQuote(Quote newQuote) { SQLiteConnection dbConnection = new SQLiteConnection(string.Format("Data Source={0}; Version=3", fileQuotes)); dbConnection.Open(); SQLiteCommand sqlCmd = new SQLiteCommand( string.Format("INSERT INTO `Quotes` VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')", newQuote.Id, newQuote.QuoteString, newQuote.Quoter, newQuote.Date.ToString("o"), newQuote.DisplayDate, newQuote.Game, newQuote.DisplayGame, newQuote.LastDisplayed.ToString("o")), dbConnection); sqlCmd.ExecuteNonQuery(); dbConnection.Close(); }
public static void RunBotCommandDiscord(string command, Message message) { Viewer sender = new Viewer(message.User.Name); if (command == "!quote") { string[] splitMessage = message.Text.Split(new char[] { ' ' }, 3); if (splitMessage.Count() >= 2 && splitMessage[1].ToLower() == "add") { try { // Split quote and quoter on - string[] splitEntry = splitMessage[2].Split('-'); // Create new quote with game that the streamer on channel is/was playing Quote newQuote = new Quote(splitEntry[0].Trim(), splitEntry[1].Trim(), Utils.GetClient().GetMyChannel().Game); // Add new quote to collection App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colQuotes.Add(newQuote); })); // Save to database DatabaseUtils.AddQuote(newQuote); // Send response SendMessageDiscord(string.Format("Quote has been added with ID of: {0}", newQuote.Id), message.Server.Id, message.Channel.Id); } catch (Exception) { SendMessageDiscord("To add a quote use: !quote add <quote> - <quoter> No need to use \" as it will be added on display.", message.Server.Id, message.Channel.Id); } } else if (splitMessage.Count() >= 2 && splitMessage[1].ToLower() == "remove") { try { int idToRemove = int.Parse(splitMessage[2]); if (idToRemove < MainWindow.colQuotes.Count()) { // Remove quote from collection App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colQuotes.RemoveAt(idToRemove); })); // Update whole database file (dynamic id) DatabaseUtils.SaveAllQuotes(); // Send response SendMessageDiscord("Quote removed with id: " + splitMessage[2], message.Server.Id, message.Channel.Id); } else { // Send response SendMessageDiscord("The quote with the given id does not exist.", message.Server.Id, message.Channel.Id); } } catch { SendMessageDiscord("Given id is not a valid id number", message.Server.Id, message.Channel.Id); } } else { Quote q; try { // Try to get quote from given id q = MainWindow.colQuotes[int.Parse(splitMessage[1])]; } catch { // Get a random quote if arg is not parsable or out of range Random rnd = new Random((int)DateTime.Now.Ticks); q = MainWindow.colQuotes[rnd.Next(0, MainWindow.colQuotes.Count)]; } // Send response SendMessageDiscord(string.Format("Quote #{0}: \"{1}\" - {2} {3} {4}", q.Id, q.QuoteString, q.Quoter, q.DisplayDate ? "[" + q.DateString + "]" : "", q.DisplayGame ? "while playing " + q.Game : "") , message.Server.Id, message.Channel.Id); } } else if (command == "!songrequest") { string[] splitMessage = message.Text.Split(new char[] { ' ' }, 2); if (splitMessage.Count() > 1) { Song requestedSong = new Song(splitMessage[1]); if (requestedSong.Type != SongType.INVALID) { // Add to colSongs App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colSongs.Add(requestedSong); })); // Display response SendMessageDiscord("The following song has been added: " + Utils.getTitleFromYouTube(splitMessage[1]), message.Server.Id, message.Channel.Id); } else { // Display response SendMessageDiscord("Invalid song link or id.", message.Server.Id, message.Channel.Id); } } } else if (command == "!currentsong") { if (MainWindow.playState) { SendMessageDiscord("Current song playing: " + MainWindow.colSongs[MainWindow.indexSong].SongName, message.Server.Id, message.Channel.Id); } } else if (command == "!nextsong") { if (MainWindow.playState) { MainWindow.instance.nextSong(); } } else if (command == "!prevsong") { if (MainWindow.playState) { MainWindow.instance.prevSong(); } } else if(command == "!id") { SendMessageDiscord(string.Format("{0} Your ID is {1}", message.User.Mention, message.User.Id), message.Server.Id, message.Channel.Id); } else if(command == "!twitchname") { Viewer vwr = MainWindow.colDatabase.FirstOrDefault(x => x.DiscordID == message.User.Id.ToString()); if(vwr != null) { SendMessageDiscord(string.Format("{0}, your Twitch name is **{1}**!", message.User.Mention, vwr.UserName), message.Server.Id, message.Channel.Id); }else { SendMessageDiscord(string.Format("{0}, you haven't linked your Twitch account yet!"), message.Server.Id, message.Channel.Id); } } }
/// <summary> /// Load viewers from DBfile to colDatabase. /// </summary> public static void LoadAllQuotes() { SQLiteConnection dbConnection; // Create new database-file and table if not exists if (!File.Exists(fileQuotes)) { SQLiteConnection.CreateFile(fileQuotes); dbConnection = new SQLiteConnection(string.Format("Data Source={0}; Version=3;", fileQuotes)); dbConnection.Open(); SQLiteCommand sqlCmd = new SQLiteCommand("CREATE TABLE `Quotes` (`Id` INTERGER NOT NULL, `Quote` TEXT, `Quoter` TEXT, `Date` TEXT, `DisplayDate` BOOLEAN, `Game` TEXT, `DisplayGame` BOOLEAN, `LastDisplayed` TEXT, PRIMARY KEY(Id))", dbConnection); sqlCmd.ExecuteNonQuery(); dbConnection.Close(); } // Load database-file otherwise else { dbConnection = new SQLiteConnection(string.Format("Data Source={0}; Version=3; Read Only=True;", fileQuotes)); dbConnection.Open(); SQLiteCommand read = new SQLiteCommand("SELECT * FROM `Quotes` ORDER BY `Id` ASC", dbConnection); SQLiteDataReader reader = read.ExecuteReader(); while (reader.Read()) { Quote loadedQuote = new Quote( (string)reader["Quote"], (string)reader["Quoter"], DateTime.Parse((string)reader["Date"], CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind), (bool)reader["DisplayDate"], (string)reader["Game"], (bool)reader["DisplayGame"], DateTime.Parse((string)reader["Date"], CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind) ); MainWindow.colQuotes.Add(loadedQuote); } dbConnection.Close(); } }
public async static void RunBotCommand(string command, IrcMessage message) { // !quote > display random quote // !quote # > display quote of given id // !quote add quote - quoter > adds quote // !quote remove # > removes quote by given id // If given Id is int and not found, display not exists // If given Id is not int, dont display Viewer sender = MainWindow.colViewers.First(x => x.UserName.ToLower() == message.Author.ToLower()); if (command == "!quote") { string[] splitMessage = message.Message.Split(new char[] { ' ' }, 3); if (splitMessage.Count() >= 2 && splitMessage[1].ToLower() == "add") { try { // Split quote and quoter on - string[] splitEntry = splitMessage[2].Split('-'); // Create new quote with game that the streamer on channel is/was playing Quote newQuote = new Quote(splitEntry[0].Trim(), splitEntry[1].Trim(), Utils.GetClient().GetMyChannel().Game); // Add new quote to collection App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colQuotes.Add(newQuote); })); // Save to database DatabaseUtils.AddQuote(newQuote); // Send response SendAndShowMessage(string.Format("Quote has been added with ID of: {0}", newQuote.Id)); } catch (Exception) { SendAndShowMessage("To add a quote use: !quote add <quote> - <quoter> No need to use \" as it will be added on display."); } } else if (splitMessage.Count() >= 2 && splitMessage[1].ToLower() == "remove") { try { int idToRemove = int.Parse(splitMessage[2]); if (idToRemove < MainWindow.colQuotes.Count()) { // Remove quote from collection App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colQuotes.RemoveAt(idToRemove); })); // Update whole database file (dynamic id) DatabaseUtils.SaveAllQuotes(); // Send response SendAndShowMessage("Quote removed with id: " + splitMessage[2]); } else { // Send response SendAndShowMessage("The quote with the given id does not exist."); } } catch { SendAndShowMessage("Given id is not a valid id number"); } } else { Quote q; try { // Try to get quote from given id q = MainWindow.colQuotes[int.Parse(splitMessage[1])]; } catch { // Get a random quote if arg is not parsable or out of range Random rnd = new Random((int)DateTime.Now.Ticks); q = MainWindow.colQuotes[rnd.Next(0, MainWindow.colQuotes.Count)]; } // Send response SendAndShowMessage(string.Format("Quote #{0}: \"{1}\" - {2} {3} {4}", q.Id, q.QuoteString, q.Quoter, q.DisplayDate ? "[" + q.DateString + "]" : "", q.DisplayGame ? "while playing " + q.Game : "") ); } } else if (command == "!songrequest") { string[] splitMessage = message.Message.Split(new char[] { ' ' }, 2); if (splitMessage.Count() > 1) { Song requestedSong = new Song(splitMessage[1]); if (requestedSong.Type != SongType.INVALID) { // Add to colSongs App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colSongs.Add(requestedSong); })); // Display response SendAndShowMessage("The following song has been added: " + Utils.getTitleFromYouTube(splitMessage[1])); } else { // Display response SendAndShowMessage("Invalid song link or id."); } } } else if (command == "!currentsong") { if (MainWindow.playState) { SendAndShowMessage("Current song playing: " + MainWindow.colSongs[MainWindow.indexSong].SongName); } } else if (command == "!nextsong") { if (MainWindow.playState) { MainWindow.instance.nextSong(); } } else if (command == "!prevsong") { if (MainWindow.playState) { MainWindow.instance.prevSong(); } } else if (command == "!link") { string[] splitMessage = message.Message.Split(new char[] { ' ' }, 2); if (splitMessage.Count() != 2) { SendAndShowMessage("In order to link your Twitch name to this bot, you have to get your Discord User ID. You can get that by using !id in Discord. Then, proceed to use !link [id] here in chat. Further instructions are following in a Discord message!"); } else { Channel priv = MainWindow.discord.CreatePrivateChannel(ulong.Parse(splitMessage[1])).Result; if (priv.IsPrivate && priv != null) { string msg = string.Format(@"Hello! The user {0} wants to link his Twitch account with your Discord name! **If you didn't use !link {1} in the Twitch chat, please ignore this message!** Else, please do the following steps: **1.** Change your Twitch game to the ID (**{1}**) you used with !link **2.** Reply with `confirm {0}` (case sensitive) **3.** Test a user specific command (like !followdate) in the Discord chat", sender.UserName, splitMessage[1]); priv.SendMessage(msg); } } } }