public void Program_MessageReceived(object sender, IrcMessageEventArgs e) { // I believe in most cases this should be just one element, but it is an array in IrcDotNet's definitions so we'll iterate it anyway. foreach (IIrcMessageTarget target in e.Targets) { // Get channel for commands Channels CurChan = null; foreach (Channels c in config.TwitchChannels) { if (target.Name.ToLower() == c.ChannelName.ToLower()) CurChan = c; }//foreach (Channels c in config.TwitchChannels) IrcChannel CurIrcChan = ircConnection.Channels.First(x => x.Name == CurChan.ChannelName); #region Beta work if (e.Text.StartsWith("!viewercount")) { int viewertotal = 0; foreach(TwitchStuff tstuff in CurChan.StreamInfo) { if(!String.IsNullOrWhiteSpace(tstuff.streamerviewcount)) { int tempcount = Convert.ToInt32(tstuff.streamerviewcount); viewertotal = viewertotal + tempcount; } } if (viewertotal > 0) { ircConnection.LocalUser.SendMessage(CurChan.ChannelName, String.Format("There are {0} total viewers watching channels on the {1} watch list.", viewertotal, CurChan.ChannelName)); } } if (e.Text.StartsWith("!viewercountall")) { int viewertotal = 0; foreach (TwitchStuff tstuff in CurChan.StreamInfo) { int tempcount = Convert.ToInt32(tstuff.streamerviewcount); viewertotal = viewertotal + tempcount; } if (viewertotal > 0) { ircConnection.LocalUser.SendMessage(CurChan.ChannelName, String.Format("There are {0} total viewers watching channels on the watch lists.", viewertotal)); } } if (e.Text.StartsWith("!multitwitch")) { List<string> matchingnames = new List<string>(); string[] args = e.Text.Split(' '); if (args.Length > 1) { for (int i = 1; i < args.Length; i++) { var o = CurChan.StreamInfo.Where(x => (x.game.ToLower().Contains(args[i].ToLower()) || x.streamname.ToLower().Contains(args[i].ToLower())) && x.streamerlive == "true"); foreach (TwitchStuff user in o) { // do not repeatedly print the name if (!matchingnames.Contains(user.streamername)) matchingnames.Add(user.streamername); } } } if (matchingnames.Count == 0) { var o = CurChan.StreamInfo.Where(x => x.streamerlive == "true"); foreach (TwitchStuff user in o) { // do not repeatedly print the name if (!matchingnames.Contains(user.streamername)) matchingnames.Add(user.streamername); } } if (matchingnames.Count > 1) { StringBuilder multitwitchannounce = new StringBuilder("http://www.multitwitch.tv/"); foreach(string s in matchingnames) { multitwitchannounce.Append(string.Format("{0}/",s)); } if (args.Length == 0) { ircConnection.LocalUser.SendMessage(CurChan.ChannelName, "No matching searches."); } else { ircConnection.LocalUser.SendMessage(CurChan.ChannelName, multitwitchannounce.ToString()); } } else if (matchingnames.Count == 1) { ircConnection.LocalUser.SendMessage(CurChan.ChannelName, String.Format("Only one user matches. http://www.twitch.tv/{0}", matchingnames[0])); } } #endregion #region Mystery if (( e.Text.ToLower().StartsWith("!gimmegame ") || e.Text.ToLower() == "!gimmegame" ) && CurChan.Mystery) { string filter = String.Empty; List<MysteryGame> grabgames = null; if (e.Text.ToLower().StartsWith("!gimmegame ")) { filter = e.Text.ToLower().Replace("!gimmegame ", ""); } DataAccess mysteryAccess = new DataAccess(); if (!String.IsNullOrEmpty(filter)) { grabgames = mysteryAccess.GameList(config.ConnectionString, "mystery",filter); } else { grabgames = mysteryAccess.GameList(config.ConnectionString, "mystery"); } Random getGame = new Random(); if (grabgames.Count > 0) { MysteryGame pulled = grabgames[getGame.Next(0, grabgames.Count - 1)]; foreach (var prop in pulled.GetType().GetProperties()) { if (String.IsNullOrEmpty(prop.GetValue(pulled,null).ToString())) prop.SetValue(pulled,"",null); } mysteryAccess.IncrementDrawCount(config.ConnectionString, "mystery", pulled.gameid); string gamebroadcast = String.Format("$4{0}$x - Platform: $b{1}$x - Submitter: $b{2}$x - Goal: $b{3}$x - Download: $b{4}$x - $b{5}$x - Tournament Race Result: $b{6}$x - Drawn {7}x", pulled.name, pulled.platform, pulled.submitter, pulled.goal, pulled.download, pulled.pastebin, pulled.tournamentraceresult,(Convert.ToInt32(pulled.draws)+1)); gamebroadcast = Utilities.TemplateMysteryGame(gamebroadcast, pulled, config.ServerName.ToLower().Contains("twitch.tv")); ircConnection.LocalUser.SendMessage(e.Targets[0].Name, gamebroadcast); } else { ircConnection.LocalUser.SendMessage(e.Targets[0].Name, "No platform found."); } } if (e.Text.ToLower().StartsWith("!setmatch ") && CurChan.Mystery) { this.SetCompetitors(CurChan, e.Text, e.Source.Name, CurIrcChan); } if (e.Text.ToLower() == "!mtinfo") { this.SendTournamentInfo(CurChan); } if (e.Text.ToLower() == "!players") { this.SendCompetitors(CurChan); } #endregion #region Lives if (e.Text.ToLower().StartsWith("!liveall")) { this.SendAllLiveList(CurChan, e.Source); } else if (e.Text.ToLower().StartsWith("!live")) { string[] args = new string[0]; if(e.Text.IndexOf(' ') >= 0) { string argstrings = e.Text.Substring(e.Text.IndexOf(' ')+1); if(argstrings.Length > 0) args = argstrings.Split(' '); } SendLiveList(CurChan, e.Source.Name, args); } #endregion #region "Misc other commands" if (e.Text.ToLower().StartsWith("!help")) { SendHelp(e.Text.Split(' '), e.Source); } if (e.Text.ToLower().StartsWith("!setnotice on ") || e.Text.ToLower().StartsWith("!setnotice off")) { string[] args = e.Text.Split(' '); if (args.Length >= 3) { if (e.Text.ToLower().StartsWith("!setnotice on ")) { if(config.SetNotice(args[2], CurChan, target, e.Source, ircConnection, true)) { ircConnection.LocalUser.SendNotice(e.Source.Name, "User " + args[2] + " now has their setnotice value set to true."); } } if (e.Text.ToLower().StartsWith("!setnotice off ")) { if(config.SetNotice(args[2], CurChan, target, e.Source, ircConnection, false)) { ircConnection.LocalUser.SendNotice(e.Source.Name, "User " + args[2] + " now has their setnotice value set to false."); } } } } // Watchlist name report if (e.Text.StartsWith("!watching")) { StringBuilder sbAssembleWatchList = new StringBuilder(); foreach (string s in CurChan.Streamers) { sbAssembleWatchList.Append(String.Format("{0} ", s)); if (sbAssembleWatchList.Length > 180) { ircConnection.LocalUser.SendNotice(e.Source.Name, sbAssembleWatchList.ToString()); sbAssembleWatchList.Clear(); }//if (sbAssembleWatchList.Length > 180) }//foreach(string s in CurChan.Streamers) if (sbAssembleWatchList.Length > 0) { ircConnection.LocalUser.SendNotice(e.Source.Name, sbAssembleWatchList.ToString()); sbAssembleWatchList.Clear(); }//if (sbAssembleWatchList.Length > 0) }//if (e.Text.StartsWith("!watching")) #endregion #region Transform if (e.Text.StartsWith(".transform")) { if (e.Text == ".transform" || e.Text == ".transform ") { DoTransform(CurChan, e.Source.Name); } else { string user = e.Text.Replace(".transform ", ""); if (user != config.BotInfo.NickName) DoTransform(CurChan, user); else DoTransform(CurChan, e.Source.Name); } } #endregion Transform #region "Config modifications" if (e.Text.StartsWith("!remove ")) { if (Utilities.CheckOp(e.Source.Name,ircConnection.Channels.First(x => x.Name == CurChan.ChannelName))) { bool success = true; string[] users = e.Text.Split(' '); foreach (string s in users) { bool founduser = false; if (s != "!remove") { while (SweepingChannels) { }//wait out a channel sweep so we don't modify the collection during it foreach (string existingUser in CurChan.Streamers) { if (s == existingUser) { founduser = true; } } if (!founduser) { ircConnection.LocalUser.SendMessage(target, "User is not currently being watched."); } else if (!config.RemoveUser(s, CurChan, target, e.Source, ircConnection)) { success = false; } } } if (success) { ircConnection.LocalUser.SendMessage(target, "User(s) successfully removed."); } else { ircConnection.LocalUser.SendMessage(target, "Not all user(s) were successfully removed."); } } } if (e.Text.StartsWith("!whitelist ")) { if (Utilities.CheckOp(e.Source.Name,ircConnection.Channels.First(x => x.Name == CurChan.ChannelName))) { if (e.Text == "!whitelist on") { if (config.SetWhiteList(true, CurChan, target, e.Source, ircConnection)) ircConnection.LocalUser.SendNotice(CurChan.ChannelName, "Whitelist turned on."); } if (e.Text == "!whitelist off") { if (config.SetWhiteList(false, CurChan, target, e.Source, ircConnection)) ircConnection.LocalUser.SendNotice(CurChan.ChannelName, "Whitelist turned off."); } } } if (e.Text.StartsWith("!whitelistgame ")) { string game = e.Text.Replace("!whitelistgame ", ""); if (Utilities.CheckOp(e.Source.Name, ircConnection.Channels.First(x => x.Name == CurChan.ChannelName))) { if (config.AddWhiteList(game, CurChan, target, e.Source, ircConnection)) ircConnection.LocalUser.SendNotice(CurChan.ChannelName, "Whitelisted " + game + "."); } } if (e.Text.StartsWith("!goliveannouncement")) { if (e.Text == "!goliveannouncement") { if (String.IsNullOrEmpty(CurChan.AnnounceMessage)) { ircConnection.LocalUser.SendNotice(CurChan.ChannelName, config.BaseMessageStartStreaming); } else { ircConnection.LocalUser.SendNotice(CurChan.ChannelName, CurChan.AnnounceMessage); } } if (e.Text.StartsWith("!goliveannouncement ")) { string msg = e.Text.Replace("!goliveannouncement ", ""); if (Utilities.CheckOp(e.Source.Name, ircConnection.Channels.First(x => x.Name == CurChan.ChannelName))) { if (config.ChangeAnnounceMessage("liveannouncement", msg, CurChan, target, e.Source, ircConnection)) ircConnection.LocalUser.SendNotice(CurChan.ChannelName, "Announce message is now " + msg + "."); } } } if (e.Text.StartsWith("!titlechangeannouncement")) { if (e.Text == "!titlechangeannouncement") { if (String.IsNullOrEmpty(CurChan.ChangedMessage)) { ircConnection.LocalUser.SendNotice(CurChan.ChannelName, config.ChangeMessage); } else { ircConnection.LocalUser.SendNotice(CurChan.ChannelName, CurChan.ChangedMessage); } } if (e.Text.StartsWith("!titlechangeannouncement ")) { string msg = e.Text.Replace("!titlechangeannouncement ", ""); if (Utilities.CheckOp(e.Source.Name, ircConnection.Channels.First(x => x.Name == CurChan.ChannelName))) { if (config.ChangeAnnounceMessage("titlechangeannouncement", msg, CurChan, target, e.Source, ircConnection)) ircConnection.LocalUser.SendNotice(CurChan.ChannelName, "Announce message is now " + msg + "."); } } } if (e.Text.StartsWith("!streamannouncement")) { if (e.Text == "!streamannouncement") { if (String.IsNullOrEmpty(CurChan.LiveMessage)) { ircConnection.LocalUser.SendNotice(CurChan.ChannelName, config.LiveMessage); } else { ircConnection.LocalUser.SendNotice(CurChan.ChannelName, CurChan.LiveMessage); } } if (e.Text.StartsWith("!streamannouncement ")) { string msg = e.Text.Replace("!streamannouncement ", ""); if (Utilities.CheckOp(e.Source.Name, ircConnection.Channels.First(x => x.Name == CurChan.ChannelName))) { if (config.ChangeAnnounceMessage("streamannouncement", msg, CurChan, target, e.Source, ircConnection)) ircConnection.LocalUser.SendNotice(CurChan.ChannelName, "Announce message is now " + msg + "."); } } } if (e.Text.StartsWith("!add ") || e.Text.StartsWith("!adduser ")) { if (Utilities.CheckOp(e.Source.Name,ircConnection.Channels.First(x => x.Name == CurChan.ChannelName))) { bool success = true; string[] users = e.Text.Split(' '); foreach (string s in users) { bool founduser = false; if (s != "!add" && s != "!adduser") { if(SweepingChannels) { ircConnection.LocalUser.SendNotice(e.Source.Name, "Currently checking twitch API for all users, will add when completed."); } while (SweepingChannels) { }//wait out a channel sweep so we don't modify the collection during it foreach (string existingUser in CurChan.Streamers) { if (s == existingUser) { founduser = true; } } if (founduser) { ircConnection.LocalUser.SendNotice(e.Source.Name, "User already exists in watchlist"); } else if (!config.AddUser(s, CurChan, target, e.Source, ircConnection)) { success = false; } } } if (success) { ircConnection.LocalUser.SendNotice(e.Source.Name, "User(s) successfully added."); } else { ircConnection.LocalUser.SendNotice(e.Source.Name, "Not all user(s) were successfully added."); } } } #endregion //Console.WriteLine(String.Format("<{0}:{1}> {2}", target, e.Source, e.Text)); } }
public void SweepChannels() { while (config.ModifyingConfig) { } // wait out config modifications SweepingChannels = true; if (LastFullUpdate < DateTime.Now) { LastFullUpdate = DateTime.Now.AddMinutes(2); foreach (Channels channel in config.TwitchChannels) { Dictionary<string, List<TwitchStuff>> updateDB = new Dictionary<string, List<TwitchStuff>>(); List<TwitchStuff> dbList = new List<TwitchStuff>(); foreach (TwitchStuff streamInfo in channel.StreamInfo) { TwitchStuff oldInfo = new TwitchStuff(); oldInfo.streamerlive = streamInfo.streamerlive; oldInfo.streamername = streamInfo.streamername; oldInfo.streamname = streamInfo.streamname; oldInfo.game = streamInfo.game; try { string wasLive = streamInfo.streamerlive; // get the info for the 'stream' field string addToList = ""; if(streamInfo.UpdateInfo(streamInfo.streamername,config)) { if (oldInfo.streamerlive == "false" && streamInfo.streamerlive == "true") { if (channel.LiveMessage != "") { addToList = channel.LiveMessage.Trim(); }//if (channel.LiveMessage != "") else { addToList = config.LiveMessage.Trim(); }//else addToList = Utilities.TemplateString(addToList, streamInfo.streamername, streamInfo.game, streamInfo.streamerviewcount, streamInfo.streamname, config.ServerName.ToLower().Contains("twitch.tv")); bool meetswhitelist = channel.MeetsWhiteBlackList(streamInfo); if (streamInfo.lastannounce.AddMinutes(30) <= DateTime.Now && meetswhitelist && streamInfo.LastOffLine.AddMinutes(30) <= DateTime.Now) { if (meetswhitelist && !streamInfo.setnotice) ircConnection.LocalUser.SendMessage(channel.ChannelName, addToList); streamInfo.lastannounce = DateTime.Now; }//if (streamInfo.lastannounce.AddMinutes(30) <= DateTime.Now && meetswhitelist) } else if (streamInfo.streamerlive == "true" && oldInfo.streamname != streamInfo.streamname && oldInfo.game != streamInfo.game) { bool changesmeetwhitelist = channel.MeetsWhiteBlackList(streamInfo); if (changesmeetwhitelist) { if (channel.ChangedMessage != "") { addToList = channel.ChangedMessage; }//if (channel.ChangedMessage != "") else { addToList = config.ChangeMessage.Trim(); }//else addToList = Utilities.TemplateString(addToList, streamInfo.streamername, streamInfo.game, streamInfo.streamerviewcount, streamInfo.streamname, config.ServerName.ToLower().Contains("twitch.tv")); if (streamInfo.lastchangeannounce.AddMinutes(15) < DateTime.Now && !streamInfo.setnotice) { ircConnection.LocalUser.SendMessage(channel.ChannelName, addToList); streamInfo.lastchangeannounce = DateTime.Now; } }//if (changesmeetwhitelist) streamInfo.lastannounce = DateTime.Now; } if (streamInfo.streamerlive == "false" && oldInfo.streamerlive == "true") { streamInfo.LastOffLine = DateTime.Now; } dbList.Add(streamInfo); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; } } // Update DB updateDB.Add(channel.ChannelName, dbList); if (config.UseDB) { DataAccess daMumbo = new DataAccess(); daMumbo.UpdateStreamInfo(updateDB, config.ConnectionString); } } } SweepingChannels = false; }