internal void Run() { while (true) { //IrcMessage ircMessage = new IrcMessage(ircClient.ReadLine(), connectedUser); string rm = ircClient.ReadLine(); IrcMessage ircMessage = new IrcMessage(rm, connectedUser); Trace.WriteLine(rm); // Bot account is the main chat account if (isBot) { switch (ircMessage.Command) { case "PING": // Received PING ircClient.WriteLine("PONG"); break; case "MODE": // Received MODE if (ircMessage.Args[1] == "+o") { // Set throttle for current user as operator if (ircMessage.Args[2] == connectedUser.UserName) { ircClient.throttle = 350; } } else if (ircMessage.Args[1] == "-o") { // Set throttle for current user as member if (ircMessage.Args[2] == connectedUser.UserName) { ircClient.throttle = 1550; } } break; // Received a list of joined viewers case "353": string[] viewers = ircMessage.Message.Split(' '); foreach (string username in viewers) { Utils.AddToViewersCol(username); } break; // JOIN Event case "JOIN": Utils.AddToViewersCol(ircMessage.Author); break; // PART Event case "PART": Utils.RemoveFromViewersCol(ircMessage.Author); break; // PRIVMSG (Chat Message Received) Event case "PRIVMSG": ChatMessageReceived(this, new ChatMessageReceivedEventArgs(ircMessage)); // Seeing that JOIN Message is not that fast ... Utils.AddToViewersCol(ircMessage.Author); // Add the message to the collection MainWindow.colChatMessages.Add(ircMessage); //MainWindow.instance.Dispatcher.BeginInvoke(new Action(delegate //{ // MainWindow.colChatMessages.Add(ircMessage); //})); //App.Current.Dispatcher.BeginInvoke(new Action(delegate //{ // MainWindow.colChatMessages.Add(ircMessage); //})); // Execute command checking and executing in a task to offload receiving new Task(() => { string firstWord = Regex.Match(ircMessage.Message, @"^\S+\b").Value.ToLower(); UserCommand foundCommand = MainWindow.colBotCommands.FirstOrDefault(x => x.Command == firstWord); if (foundCommand != null) { foundCommand.ExecuteCommand(ircMessage); } else { BotCommands.RunBotCommand(firstWord, ircMessage); } }).Start(); break; } } else { switch (ircMessage.Command) { case "PING": // Received PING ircClient.WriteLine("PONG"); break; case "MODE": // Received MODE if (ircMessage.Args[1] == "+o") { // Set throttle for current user as operator if (ircMessage.Args[2] == connectedUser.UserName) { ircClient.throttle = 350; } } else if (ircMessage.Args[1] == "-o") { // Set throttle for current user as member if (ircMessage.Args[2] == connectedUser.UserName) { ircClient.throttle = 1550; } } break; } } } }
public void ExecuteCommand(IrcMessage message) { // Get command user's Viewer object Viewer viewer = MainWindow.colDatabase.FirstOrDefault(x => x.UserName == message.Author); if (viewer == null) { return; } if (CanExecute(viewer)) { Match targetMatch = Regex.Match(message.Message, @"@(?<name>[a-zA-Z0-9_]{4,25})"); string target = targetMatch.Groups["name"].Value; // Parse response line here // @user@ -> Display name of the user of the command // @followdate@ -> Command user's follow date // @followdatetime@ -> Command user's follow date and time // @game@ -> Channels current game // @title@ -> Channels current title string parsedResponse = Regex.Replace(response, @"@(?<item>\w+)@", m => { string[] split = Regex.Split(message.Message, @"\s+"); switch (m.Groups["item"].Value.ToLower()) { case "user": return viewer.UserName; case "followdate": return viewer.GetFollowDateTime("yyyy-MM-dd"); case "followdatetime": return viewer.GetFollowDateTime("yyyy-MM-dd HH:mm"); case "game": return Utils.GetClient().GetMyChannel().Game; case "title": return Utils.GetClient().GetMyChannel().Status; case "var1": if (split.Count() == 2) { var1 = split[1]; return var1; } return ""; default: return ""; } }); // Set timestamps lastUsed = DateTime.UtcNow; dictLastUsed[message.Author] = DateTime.UtcNow; // Notify the UI of the new last used date NotifyPropertyChanged("LastUsed"); // Send the response if (sendAsStreamer) { // Send message to IRC, no need to add to collection as this will be received by bot account MainWindow.instance.streamerChatConnection.SendChatMessage(parsedResponse.Trim()); } else { // Send message to IRC MainWindow.instance.botChatConnection.SendChatMessage(parsedResponse.Trim()); // Add message to collection App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colChatMessages.Add(new IrcMessage( MainWindow.instance.accountBot.UserName, parsedResponse.Trim())); })); } } }
public void AddViewerMessage(IrcMessage message) { colViewerMessages.Add(message); }
public void ExecuteCommand(IrcMessage message) { // Get command user's Viewer object Viewer viewer = MainWindow.colDatabase.FirstOrDefault(x => x.UserName == message.Author); if (viewer == null) { return; } if (CanExecute(viewer)) { Match targetMatch = Regex.Match(message.Message, @"@(?<name>[a-zA-Z0-9_]{4,25})"); string target = targetMatch.Groups["name"].Value; // Parse response line here // @user@ -> Display name of the user of the command // @followdate@ -> Command user's follow date // @followdatetime@ -> Command user's follow date and time // @game@ -> Channels current game // @title@ -> Channels current title string parsedResponse = Regex.Replace(response, @"@(?<item>\w+)@", m => { string[] split = Regex.Split(message.Message, @"\s+"); switch (m.Groups["item"].Value.ToLower()) { case "user": return(viewer.UserName); case "followdate": return(viewer.GetFollowDateTime("yyyy-MM-dd")); case "followdatetime": return(viewer.GetFollowDateTime("yyyy-MM-dd HH:mm")); case "game": return(Utils.GetClient().GetMyChannel().Game); case "title": return(Utils.GetClient().GetMyChannel().Status); case "var1": if (split.Count() == 2) { var1 = split[1]; return(var1); } return(""); default: return(""); } }); // Set timestamps lastUsed = DateTime.UtcNow; dictLastUsed[message.Author] = DateTime.UtcNow; // Notify the UI of the new last used date NotifyPropertyChanged("LastUsed"); // Send the response if (sendAsStreamer) { // Send message to IRC, no need to add to collection as this will be received by bot account MainWindow.instance.streamerChatConnection.SendChatMessage(parsedResponse.Trim()); } else { // Send message to IRC MainWindow.instance.botChatConnection.SendChatMessage(parsedResponse.Trim()); // Add message to collection App.Current.Dispatcher.BeginInvoke(new Action(delegate { MainWindow.colChatMessages.Add(new IrcMessage( MainWindow.instance.accountBot.UserName, parsedResponse.Trim())); })); } } }
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); } } } }