static void Main(string[] args) { var client = new IrcClient("irc.freenode.net", new IrcUser("ChatSharp", "ChatSharp")); client.NetworkError += (s, e) => Console.WriteLine("Error: " + e.SocketError); client.RawMessageRecieved += (s, e) => Console.WriteLine("<< {0}", e.Message); client.RawMessageSent += (s, e) => Console.WriteLine(">> {0}", e.Message); client.UserMessageRecieved += (s, e) => { if (e.PrivateMessage.Message.StartsWith(".join ")) client.Channels.Join(e.PrivateMessage.Message.Substring(6)); else if (e.PrivateMessage.Message.StartsWith(".list ")) { var channel = client.Channels[e.PrivateMessage.Message.Substring(6)]; var list = channel.Users.Select(u => u.Nick).Aggregate((a, b) => a + "," + b); client.SendMessage(list, e.PrivateMessage.User.Nick); } else if (e.PrivateMessage.Message.StartsWith(".whois ")) client.WhoIs(e.PrivateMessage.Message.Substring(7), null); else if (e.PrivateMessage.Message.StartsWith(".raw ")) client.SendRawMessage(e.PrivateMessage.Message.Substring(5)); else if (e.PrivateMessage.Message.StartsWith(".mode ")) { var parts = e.PrivateMessage.Message.Split(' '); client.ChangeMode(parts[1], parts[2]); } }; client.ChannelMessageRecieved += (s, e) => { Console.WriteLine("<{0}> {1}", e.PrivateMessage.User.Nick, e.PrivateMessage.Message); }; client.ConnectAsync(); while (true) ; }
static void Main(string[] args) { var client = new IrcClient("irc.freenode.net", new IrcUser("ChatSharp", "ChatSharp")); client.NetworkError += (s, e) => Console.WriteLine("Error: " + e.SocketError); client.RawMessageRecieved += (s, e) => Console.WriteLine("<< {0}", e.Message); client.RawMessageSent += (s, e) => Console.WriteLine(">> {0}", e.Message); client.UserMessageRecieved += (s, e) => { if (e.PrivateMessage.Message.StartsWith(".join ")) client.Channels.Join(e.PrivateMessage.Message.Substring(6)); else if (e.PrivateMessage.Message.StartsWith(".list ")) { var channel = client.Channels[e.PrivateMessage.Message.Substring(6)]; var list = channel.Users.Select(u => u.Nick).Aggregate((a, b) => a + "," + b); client.SendMessage(list, e.PrivateMessage.User.Nick); } else if (e.PrivateMessage.Message.StartsWith(".whois ")) client.WhoIs(e.PrivateMessage.Message.Substring(7), null); else if (e.PrivateMessage.Message.StartsWith(".raw ")) client.SendRawMessage(e.PrivateMessage.Message.Substring(5)); else if (e.PrivateMessage.Message.StartsWith(".bans ")) { client.GetBanList(e.PrivateMessage.Message.Substring(6), bans => { client.SendMessage(string.Join(",", bans.Select(b => string.Format("{0} by {1} at {2}", b.Value, b.Creator, b.CreationTime) ).ToArray()), e.PrivateMessage.User.Nick); }); } else if (e.PrivateMessage.Message.StartsWith(".exceptions ")) { client.GetExceptionList(e.PrivateMessage.Message.Substring(12), exceptions => { client.SendMessage(string.Join(",", exceptions.Select(ex => string.Format("{0} by {1} at {2}", ex.Value, ex.Creator, ex.CreationTime) ).ToArray()), e.PrivateMessage.User.Nick); }); } }; client.ChannelMessageRecieved += (s, e) => { Console.WriteLine("<{0}> {1}", e.PrivateMessage.User.Nick, e.PrivateMessage.Message); }; client.ConnectAsync(); while (true) ; }
// Start the bot public IRCBot() { // Load the settings if (File.Exists(Directory.GetCurrentDirectory() + "/Settings/settings.json")) settings = Utils.Load<Settings>(); else Utils.Save(ref settings); // Load the words if (File.Exists(Directory.GetCurrentDirectory() + "/Settings/words.json")) words = Utils.Load<Words>(); else Utils.Save(ref words); // Load the seen_tells' if (File.Exists(Directory.GetCurrentDirectory() + "/Settings/seen_tell.json")) seenTell = Utils.Load<Seen_Tell>(); else Utils.Save(ref seenTell); /* // Load the aliase if (File.Exists(Directory.GetCurrentDirectory() + "/Settings/alias.json")) alias = Utils.Load<Alias>(); else Utils.Save(ref alias); */ // Create the randomizer BaseUtils.random = new Random(); // Startup BaseUtils.LogSpecial(settings.name + " - a friendly IRC bot!"); // Connection client = new IrcClient(settings.host, new IrcUser(settings.name, settings.name)); client.ConnectionComplete += (s, e) => { client.SendMessage("identify " + settings.pw, new[] { "NickServ" }); settings.channels.ForEach(c => client.JoinChannel(c)); }; client.ChannelMessageRecieved += Client_ChannelMessageRecieved; client.UserKicked += Client_UserKicked; //client.NickChanged += Client_NickChanged; client.ConnectAsync(); AppDomain.CurrentDomain.ProcessExit += (s, e) => { client.Quit(); BaseUtils.Writer().WriteLine("[Stop] ============ " + DateTime.UtcNow.ToShortDateString() + " " + DateTime.UtcNow.ToLongTimeString() + " ============"); BaseUtils.Writer().Close(); }; // Stop GC GC.KeepAlive(this); }
static void Main(string[] args) { var client = new IrcClient("irc.freenode.net", new IrcUser("ChatSharp", "ChatSharp")); client.NetworkError += (s, e) => Console.WriteLine("Error: " + e.SocketError); client.RawMessageRecieved += (s, e) => Console.WriteLine("<< {0}", e.Message); client.RawMessageSent += (s, e) => Console.WriteLine(">> {0}", e.Message); client.UserMessageRecieved += (s, e) => { if (e.PrivateMessage.Message.StartsWith(".join ")) client.Channels.Join(e.PrivateMessage.Message.Substring(6)); else if (e.PrivateMessage.Message.StartsWith(".list ")) { var channel = client.Channels[e.PrivateMessage.Message.Substring(6)]; var list = channel.Users.Select(u => u.Nick).Aggregate((a, b) => a + "," + b); client.SendMessage(list, e.PrivateMessage.User.Nick); } else if (e.PrivateMessage.Message.StartsWith(".whois ")) client.WhoIs(e.PrivateMessage.Message.Substring(7), null); else if (e.PrivateMessage.Message.StartsWith(".raw ")) client.SendRawMessage(e.PrivateMessage.Message.Substring(5)); else if (e.PrivateMessage.Message.StartsWith(".mode ")) { var parts = e.PrivateMessage.Message.Split(' '); client.ChangeMode(parts[1], parts[2]); } else if (e.PrivateMessage.Message.StartsWith(".topic ")) { string messageArgs = e.PrivateMessage.Message.Substring(7); if (messageArgs.Contains(" ")) { string channel = messageArgs.Substring(0, messageArgs.IndexOf(" ")); string topic = messageArgs.Substring(messageArgs.IndexOf(" ") + 1); client.Channels[channel].SetTopic(topic); } else { string channel = messageArgs.Substring(messageArgs.IndexOf("#")); client.GetTopic(channel); } } }; client.ChannelMessageRecieved += (s, e) => { Console.WriteLine("<{0}> {1}", e.PrivateMessage.User.Nick, e.PrivateMessage.Message); }; client.ChannelTopicReceived += (s, e) => { Console.WriteLine("Received topic for channel {0}: {1}", e.Channel.Name, e.Topic); }; client.ConnectAsync(); while (true) ; }
void _client_RawMessageRecieved(object sender, ChatSharp.Events.RawMessageEventArgs e) { if (e.Message.IndexOf("/MOTD") >= 0 && !_connected) { if (!_nickserv) { Connected = true; Connecting = false; OnConnected(this, null); } else { _client.SendMessage("identify " + _password, "NickServ"); } } else if (e.Message[0] == ':' && (e.Message.Contains(String.Format("MODE {0} :+r", _nick)) || e.Message.Contains(":Your nick isn't registered."))) { Connected = true; Connecting = false; OnConnected(this, null); } }
/// <summary> /// Interprets commands /// </summary> /// <param name="command">The full message</param> /// <param name="sender">The full sender</param> /// <param name="recipient">The full recipient</param> private static void InterpretCommand(string command, IrcUser sender, IrcClient client) { command = command.TrimStart(ProgramSettings.settings.CommandPrefix); //simply removes the prefix if (command.StartsWith ("slap")) { if (ProgramSettings.settings.SlapEnabled) { string[] splitCommand = command.Split (new char[] { ' ' }, 2); if (splitCommand.Length > 1) { var listCopy = UsersList; bool foundUser = false; try { foreach (IrcUserAndSeen user in listCopy) { if (user.User.Nick.ToLower () == splitCommand [1].ToLower ().Trim ()) { if (user.User.Nick == client.User.Nick) client.SendRawMessage ("PRIVMSG {0} :What'd I do? :(", client.Channels [0].Name); else client.SendRawMessage ("PRIVMSG {0} :" + "\x01" + "ACTION slaps {1} with a giant fish.\x01", client.Channels [0].Name, user.User.Nick); foundUser = true; break; } } if (!foundUser) client.SendRawMessage ("PRIVMSG {0} :User not found!"); } catch{} } else client.SendRawMessage ("PRIVMSG {0} :Slap who?", client.Channels [0].Name); } } if (command.StartsWith("removeuser")) { string[] split = command.Split(new char[]{' '}, 2); Console.ForegroundColor = ConsoleColor.Yellow; if (split.Length > 1) { bool removed = false; for(int i = 0; i < ProgramSettings.settings.UsersAllowedToDisable.Count; i++) { if (ProgramSettings.settings.UsersAllowedToDisable[i] == split[1].ToLower()) { ProgramSettings.settings.UsersAllowedToDisable.RemoveAt(i); removed = true; } } if (removed) { Console.WriteLine("Removed user \"{0}\" successfully!", split[1].ToLower()); client.SendRawMessage("PRIVMSG {0} :Removed user \"{1}\" successfully!", client.Channels[0].Name, split[1].ToLower()); } else { Console.WriteLine("User doesn't exist!"); client.SendRawMessage("PRIVMSG {0} :That user never existed in the database!", client.Channels[0].Name); } } else { Console.WriteLine("Remove who?"); } Console.ForegroundColor = ConsoleColor.White; } if (command.StartsWith("adduser")) { string[] split = command.Split(new char[]{' '}, 2); Console.ForegroundColor = ConsoleColor.Yellow; if (split.Length > 1) { ProgramSettings.settings.UsersAllowedToDisable.Add(split[1].ToLower()); Console.WriteLine("Added user \"{0}\" to the VIP list", split[1].ToLower()); client.SendRawMessage("PRIVMSG {0} :Added user \"{1}\" to the VIP list.?", client.Channels[0].Name, split[1].ToLower()); } else { Console.WriteLine("Who?"); client.SendRawMessage("PRIVMSG {0} :Add who?", client.Channels[0].Name); } Console.ForegroundColor = ConsoleColor.White; } if (command.StartsWith("disableseen")) { foreach (string user in ProgramSettings.settings.UsersAllowedToDisable) { if (user.ToLower() == sender.Nick.ToLower()) { ProgramSettings.settings.SeenEnabled = false; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Seen Command Disabled"); Console.ForegroundColor = ConsoleColor.White; client.SendRawMessage ("PRIVMSG {0} :Disabled seen command!", client.Channels[0].Name); } } } if (command.StartsWith("enableseen")) { foreach (string user in ProgramSettings.settings.UsersAllowedToDisable) { if (user.ToLower() == sender.Nick.ToLower()) { ProgramSettings.settings.SeenEnabled = true; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Seen Command Enabled"); Console.ForegroundColor = ConsoleColor.White; client.SendRawMessage ("PRIVMSG {0} :Enabled seen command!", client.Channels[0].Name); } } } if (command.StartsWith ("enableurlparse")) { foreach (string user in ProgramSettings.settings.UsersAllowedToDisable) { if (user.ToLower () == sender.Nick.ToLower ()) { ProgramSettings.settings.UrlParsingEnabled = true; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("URL Parsing Enabled"); Console.ForegroundColor = ConsoleColor.White; client.SendRawMessage ("PRIVMSG {0} :Enabling URL parsing!", client.Channels[0].Name); } } } if (command.StartsWith ("disableurlparse")) { foreach (string user in ProgramSettings.settings.UsersAllowedToDisable) { if (user.ToLower () == sender.Nick.ToLower ()) { ProgramSettings.settings.UrlParsingEnabled = false; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("URL Parsing Disabled"); Console.ForegroundColor = ConsoleColor.White; client.SendRawMessage ("PRIVMSG {0} :Disabling URL parsing!", client.Channels[0].Name); } } } if (command.StartsWith ("nick") || command.StartsWith ("name")) { string[] splitCommand = command.Split (new char[]{ ' ' }, 2); if (splitCommand.Length > 1) { foreach (string user in ProgramSettings.settings.UsersAllowedToDisable) { if (user.ToLower () == sender.Nick.ToLower ()) { string cleanNick = CleanInput (splitCommand [1]); client.Nick (cleanNick); ProgramSettings.settings.LastUsedNick = client.User.Nick; } } } } if(command.StartsWith("motto") || command.StartsWith("slogan")) { client.SendRawMessage("PRIVMSG {0} :Luigibot does what Reta don't \u00a9", client.Channels[0].Name); } if(command.StartsWith("version")) { #if __MONOCS__ client.SendRawMessage("PRIVMSG {0} :Luigibot v{1} - http://www.github.com/Luigifan/Luigibot - Running under Mono", client.Channels[0].Name, ProgramVersion.ToString()); #else client.SendRawMessage("PRIVMSG {0} :Luigibot v{1} - http://www.github.com/Luigifan/Luigibot", client.Channels[0].Name, ProgramVersion.ToString()); #endif } if(command.StartsWith("changeprefix")) { string[] splitCommand = command.Split(new char[] { ' ' }, 2); if (splitCommand.Length > 1) { foreach (string user in ProgramSettings.settings.UsersAllowedToDisable) { if(user.ToLower() == sender.Nick.ToLower()) { if (splitCommand[1].Length == 1) { if (splitCommand[1] == "/") { client.SendRawMessage("PRIVMSG {0} :oi u cheeky c**t ill bash ur ead in i swear on me mum", client.Channels[0].Name); return; } try { char newPrefix = char.Parse(splitCommand[1]); ProgramSettings.settings.CommandPrefix = newPrefix; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Command prefix changed to '{0}' successfully!", newPrefix.ToString()); Console.ForegroundColor = ConsoleColor.White; client.SendRawMessage("PRIVMSG {0} :Command prefix changed to '{1}' successfully!", client.Channels[0].Name, newPrefix.ToString()); return; } catch (Exception ex) { client.SendRawMessage("PRIVMSG {0} :Uh-oh! Luigibot encountered an error. Email Luigifan @ [email protected]", client.Channels[0].Name); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("UH OH BIG ERROR\nUH OH BIG ERROR"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(ex.Message + "\n"); } } else { client.SendRawMessage("PRIVMSG {0} :The prefix must be a single character!", client.Channels[0].Name); } } } } else client.SendRawMessage("PRIVMSG {0} :What character will prefix my commands?", client.Channels[0].Name); } if(command.StartsWith("kick")) { //0: command //1: user //2: reason string[] splitCommand = command.Split(new char[] { ' ' }, 3); foreach (string user in ProgramSettings.settings.UsersAllowedToDisable) { if(user.ToLower() == sender.Nick.ToLower()) { try { if (splitCommand.Length > 2) { client.KickUser(client.Channels[0].Name, splitCommand[1], splitCommand[2]); } else client.KickUser(client.Channels[0].Name, splitCommand[1]); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: {0}", ex.Message); Console.ForegroundColor = ConsoleColor.White; } } } } if(command.StartsWith("getmodes")) { client.SendMessage("Modes: " + client.User.Mode, client.Channels[0].Name); } if (command.StartsWith ("ann")) { string[] splitCommand = command.Split (new char[]{ ' ' }, 2); if (splitCommand.Length > 1) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { if (splitCommand [1] == "test") { if (ProgramSettings.settings.WelcomeMessage.Contains ("/me")) { client.SendAction (String.Format (ProgramSettings.settings.WelcomeMessage.Substring (4), sender.Nick), client.Channels [0].Name); } else { client.SendRawMessage ("PRIVMSG {0} :{1}", client.Channels [0].Name, String.Format (ProgramSettings.settings.WelcomeMessage, sender.Nick)); } } else { if(splitCommand[1].Contains("{") && splitCommand[1].Contains("}")) { string[] tester = splitCommand[1].Split(new char[] { '{', '}' }, 3); foreach(string x in tester) { int xf = -1; bool parsed = false; try { xf = int.Parse(x.ToString()); parsed = true; } catch{parsed = false;} if (parsed) { if (xf > 0 || xf < 0) { client.SendRawMessage("PRIVMSG {0} :ERROR: Index numbers can't be bigger than 0 or less than 0. MUST BE 0!", client.Channels[0].Name); return; } } } } ProgramSettings.settings.WelcomeMessage = splitCommand [1]; client.SendRawMessage ("PRIVMSG {0} :Welcome message set!", client.Channels [0].Name); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("New welcome message set: " + splitCommand [1]); Console.ForegroundColor = ConsoleColor.White; } } } } else { client.SendRawMessage ("PRIVMSG {0} :What message do I set?", client.Channels[0].Name); } } if (command.StartsWith ("enableslap")) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { ProgramSettings.settings.SlapEnabled = true; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Enabling slap"); Console.ForegroundColor = ConsoleColor.White; break; } } } if (command.StartsWith ("enablewelcome")) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { ProgramSettings.settings.WelcomeUserEnabled = true; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Enabling welcome messages"); Console.ForegroundColor = ConsoleColor.White; break; } } } if (command.StartsWith ("disablewelcome")) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { ProgramSettings.settings.WelcomeUserEnabled = false; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Disabling welcome messages"); Console.ForegroundColor = ConsoleColor.White; break; } } } if (command.StartsWith ("disableslap")) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { ProgramSettings.settings.SlapEnabled = false; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Disabling slap"); Console.ForegroundColor = ConsoleColor.White; break; } } } if (command.StartsWith ("eightball") || command.StartsWith ("8ball") || command.StartsWith ("fortune")) { if (ProgramSettings.settings.EightballEnabled) { int ranMessage = random.Next (EightballMessages.Length - 1); if (command.ToLower ().Contains ("waluigibot1337") || command.ToLower ().Contains ("waluigibot") || command.ToLower ().Contains ("waluigi bot")) client.SendRawMessage ("PRIVMSG {0} :Waluigibot is a tool and will never come to fruition.", client.Channels [0].Name); else if (command.ToLower ().Contains ("bot") && !command.ToLower ().Contains ("luigi")) client.SendRawMessage ("PRIVMSG {0} :Your bot is inadequate for the job.", client.Channels [0].Name); else client.SendRawMessage ("PRIVMSG {0} :{1}", client.Channels [0].Name, EightballMessages [ranMessage]); } } if (command.StartsWith ("status")) { client.SendRawMessage ("PRIVMSG {0} :Slap Enabled: {1}. Eight Ball Enabled: {2}. Welcome Enabled: {3}. Command Prefix: {4}. URL Parsing: {5}", client.Channels [0].Name, ProgramSettings.settings.SlapEnabled, ProgramSettings.settings.EightballEnabled, ProgramSettings.settings.WelcomeUserEnabled, ProgramSettings.settings.CommandPrefix.ToString(), ProgramSettings.settings.UrlParsingEnabled); } if (command.StartsWith ("lastfm")) { #if __MONOCS__ client.SendRawMessage("PRIVMSG {0} :The Last.FM API we use isn't available on Mono.", client.Channels[0].Name); #elif __MSCS__ string[] split = command.Split (new char[] { ' ' }, 2); if (split.Length > 1) { client.SendRawMessage("PRIVMSG {0} :Sending LastFM request, this may take a few seconds..", client.Channels[0].Name); try { var lastfmClient = new LastfmClient ("4de0532fe30150ee7a553e160fbbe0e0", "0686c5e41f20d2dc80b64958f2df0f0c"); var response = lastfmClient.User.GetRecentScrobbles (split [1].ToString ().Trim (), null, 0, 1); LastTrack lastTrack = response.Result.Content [0]; client.SendRawMessage ("PRIVMSG {0} :{1} last listened to {2} by {3}.", client.Channels [0].Name, split [1].Trim (), lastTrack.Name, lastTrack.ArtistName); } catch (ArgumentOutOfRangeException iex) { client.SendRawMessage ("PRIVMSG {0} :That user doesn't exist or hasn't scrobbled anything yet!", client.Channels [0].Name); } catch (Exception ex) { client.SendRawMessage ("PRIVMSG {0} :Uh-oh! Luigibot encountered an error. Email Luigifan @ [email protected]", client.Channels [0].Name); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine ("UH OH BIG ERROR\nUH OH BIG ERROR"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine (ex.Message + "\n"); } } #endif } if (command.StartsWith ("enable8ball") || command.StartsWith ("enableeightball")) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { ProgramSettings.settings.EightballEnabled = true; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Enabling eight ball"); Console.ForegroundColor = ConsoleColor.White; break; } } } if (command.StartsWith ("disable8ball") || command.StartsWith ("disableeightball")) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { ProgramSettings.settings.EightballEnabled = false; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine ("Disabling eight ball"); Console.ForegroundColor = ConsoleColor.White; break; } } } if (command.StartsWith ("selfdestruct")) { foreach (var nick in ProgramSettings.settings.UsersAllowedToDisable) { if (sender.Nick.ToLower () == nick) { ExitSafely (); break; } } } if (command.StartsWith ("42")) { client.SendRawMessage ("PRIVMSG {0} :The Answer to Life, the Universe, and Everything.", client.Channels [0].Name); } if (command.StartsWith ("commands")) { client.SendRawMessage ("PRIVMSG {0} :You can find a list of my commands here: https://github.com/Luigifan/Luigibot/wiki/Luigibot-Commands", client.Channels [0].Name); } if (command.StartsWith ("seen")) { string[] splitCommand = command.Split (new char[] { ' ' }, 2); if (splitCommand.Length > 1) { if (splitCommand [1].ToLower () == "knux" || splitCommand [1].ToLower () == "knuckles" || splitCommand [1].ToLower () == "knuckles96") { client.SendRawMessage ("PRIVMSG {0} :Never.", client.Channels [0].Name); return; } var UsersListCopy = UsersList; var UserDatabaseCopy = UsersSeenDatabase.UsersSeenDatabase; //First, check to see if the user is on now bool foundInOnline = false; foreach (IrcUserAndSeen user in UsersListCopy) { if (user.User.Nick.ToLower () == splitCommand [1].ToLower ().Trim ()) { foundInOnline = true; if (user.User.Nick == client.User.Nick) client.SendRawMessage ("PRIVMSG {0} :I'm always online c:", client.Channels [0].Name); else client.SendRawMessage ("PRIVMSG {0} :That user is online now!", client.Channels [0].Name); foundInOnline = true; break; } } //Then, we'll check the database bool foundInDatabase = false; List<IrcUserAndSeen> removeLater = new List<IrcUserAndSeen>(); if (!foundInOnline) { foreach (IrcUserAndSeen user in UserDatabaseCopy) { if (user.User.Nick == null) { Console.WriteLine("Adding null entry to be removed later"); removeLater.Add(user); } else { if (user.User.Nick.ToLower() == splitCommand[1].ToLower().Trim()) { foundInDatabase = true; client.SendRawMessage("PRIVMSG {0} :{1} was last seen at {2} (EST)", client.Channels[0].Name, user.User.Nick, user.LastSeen.ToString()); break; } } } if (!foundInDatabase && !foundInOnline) client.SendRawMessage ("PRIVMSG {0} :I'm not sure :(", client.Channels [0].Name); } } else client.SendRawMessage ("PRIVMSG {0} :Last seen who?", client.Channels [0].Name); } }
private static void Main(string[] args) { var options = new Opciones(); if (!Parser.Default.ParseArguments(args, options)) { var ht = HelpText.AutoBuild(options); ht.Copyright = new CopyrightInfo("Kevin López Brante", 2015); Console.WriteLine(ht); Environment.Exit(2); } else { var config = Config.LeerDesdeArchivo(options.ConfigurationFile); var spotifyRegex = new Regex(@"(?:https?:\/\/)?(?:open\.spotify\.com\/|spotify:)track(?:\/|:)(\w*)"); var ytRegex = new Regex(@"(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?\/(?:watch\?v=|embed\/)?(\w*)"); var client = new IrcClient($"{config.Hostname}:{config.Port}", new IrcUser(config.Nick, config.Ident, config.Password)); client.ConnectionComplete += (s, e) => { Console.WriteLine($"{DateTime.Now} ¡Conectado al servidor!"); foreach (var channel in config.Channels) { client.JoinChannel(channel); Console.WriteLine($"{DateTime.Now} Uniéndose al canal {channel}"); } }; client.RawMessageRecieved += (s, e) => { Console.WriteLine($"{DateTime.Now} <= {e.Message}"); }; client.RawMessageSent += (s, e) => { Console.WriteLine($"{DateTime.Now} => {e.Message}"); }; client.ChannelMessageRecieved += (s, e) => { var channel = client.Channels[e.PrivateMessage.Source]; Console.WriteLine($"{DateTime.Now} {channel.Name}/{e.PrivateMessage.User}: {e.PrivateMessage.Message}"); if (spotifyRegex.IsMatch(e.PrivateMessage.Message)) { Console.WriteLine($"{DateTime.Now} Capturado enlace de Spotify."); try { var mx = spotifyRegex.Match(e.PrivateMessage.Message); Console.WriteLine($"{DateTime.Now} Obteniendo info de pista..."); var spotify = SpotifyAPI.obtenerPista(mx.Groups[1].Value); Console.WriteLine($"{DateTime.Now} Info obtenida."); var ts = TimeSpan.FromMilliseconds(spotify.duration_ms); string artj = string.Join(", ", spotify.artists.Take(spotify.artists.Count - 1)) + (spotify.artists.Count <= 1 ? "" : " y ") + spotify.artists.LastOrDefault(); var ou = $"\x02{spotify.name}\x02 (\x1D{ts.ToString("hh':'mm':'ss")}\x1D) por \x02{artj}\x02 en \x02{spotify.album}\x02 (\x1D{"disco"} {spotify.disc_number}, pista {spotify.track_number}\x1D)"; channel.SendMessage(ou); Console.WriteLine($"{DateTime.Now} Mensaje enviado."); } catch (Exception exception) { Console.WriteLine(exception); channel.SendMessage(exception.Message); } } if (e.PrivateMessage.Message.StartsWith("!spotify")) { Console.WriteLine($"{DateTime.Now} Capturado comando Buscar en Spotify."); if (e.PrivateMessage.Message.StartsWith("!spotify ")) { var x = e.PrivateMessage.Message.Replace("!spotify ", ""); var sps = SpotifySearch.buscar(x); Console.WriteLine($"{DateTime.Now} Buscando \"{x}\" en Spotify..."); channel.SendMessage($"{e.PrivateMessage.User.Nick}, te mandé los resultados por privado."); for (int index = 0; index < sps.tracks.items.Count; index++) { var spotify = sps.tracks.items[index]; var ts = TimeSpan.FromMilliseconds(spotify.duration_ms); string artj = string.Join(", ", spotify.artists.Take(spotify.artists.Count - 1)) + (spotify.artists.Count <= 1 ? "" : " y ") + spotify.artists.LastOrDefault(); var ou = $"{index+1}: \x02{spotify.name}\x02 (\x1D{ts.ToString("hh':'mm':'ss")}\x1D) por \x02{artj}\x02 en \x02{spotify.album}\x02: spotify:track:{spotify.id}"; client.SendMessage(ou, e.PrivateMessage.User.Nick); } } else { channel.SendMessage("Debes poner algo después de !spotify."); } } if (ytRegex.IsMatch(e.PrivateMessage.Message)) { Console.WriteLine($"{DateTime.Now} Capturado enlace de YouTube."); try { var mx = ytRegex.Match(e.PrivateMessage.Message); Console.WriteLine($"{DateTime.Now} Obteniendo info de video..."); var youTube = YouTube.GetInfo(mx.Groups[1].Value); Console.WriteLine($"{DateTime.Now} Info obtenida."); _item = youTube.Items[0]; var ts = _item.ContentDetails.Duracion(); var ou = $"\x02{_item.Snippet.Title}\x02 (\x1D{ts.ToString("hh':'mm':'ss")}\x1D) por \x02{_item.Snippet.ChannelTitle}\x02 el \x02{_item.Snippet.PublishedAt.ToString(CultureInfo.GetCultureInfoByIetfLanguageTag("es"))}\x02"; channel.SendMessage(ou); Console.WriteLine($"{DateTime.Now} Mensaje enviado."); } catch (Exception exception) { Console.WriteLine(exception); channel.SendMessage(exception.Message); } } if (e.PrivateMessage.Message.StartsWith("!youtube")) { Console.WriteLine($"{DateTime.Now} Capturado comando Buscar en YouTube."); if (e.PrivateMessage.Message.StartsWith("!youtube ")) { var x = e.PrivateMessage.Message.Replace("!youtube ", ""); var sps = YouTubeSearch.buscar(x, config.ApiKeys.YtData); Console.WriteLine($"{DateTime.Now} Buscando \"{x}\" en YouTube..."); channel.SendMessage($"{e.PrivateMessage.User.Nick}, te mandé los resultados por privado."); for (int index = 0; index < sps.Items.Count; index++) { var yt = sps.Items[index]; var ou = $"{index + 1}: \x02{yt.Snippet.Title}\x02, publicado por \x02{yt.Snippet.ChannelTitle}\x02 el {yt.Snippet.PublishedAt.ToString(CultureInfo.GetCultureInfoByIetfLanguageTag("es"))}: https://www.youtube.com/watch?v={yt.Id.VideoId}"; client.SendMessage(ou, e.PrivateMessage.User.Nick); } } else { channel.SendMessage("Debes poner algo después de !spotify."); } } }; Console.WriteLine($"{DateTime.Now} Conectando al servidor {config.Hostname}:{config.Port} como {config.Nick}..."); client.ConnectAsync(); while (true) { Thread.Sleep(500); } } }