Advertisement
Guest User

Untitled

a guest
Dec 25th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.IO;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10.  
  11.  
  12. namespace TwitchChatBot
  13. {
  14.     class Program
  15.     {
  16.         private string username = "";
  17.         private string password = "";
  18.         private string channel = "";
  19.         private string Host = "irc.twitch.tv";
  20.         private int port = 6667;
  21.         StreamReader ReadFromTwitch;
  22.         StreamWriter WriteToTwitch;
  23.         TcpClient TwitchSocket;
  24.        
  25.        
  26.         static void Main(string[] args)
  27.         {
  28.             Program TwitchBot = new Program();
  29.  
  30.             TwitchBot.Run();
  31.             //TwitchBot.Reconnect();
  32.             //TwitchBot.ReadMessages();
  33.         }
  34.         public void readConsoleInput()
  35.         {
  36.             while (true)
  37.             {
  38.                 if (Console.KeyAvailable)
  39.                 {
  40.                     string cInput = Console.ReadLine();
  41.                     Fault.log(cInput);
  42.                 }
  43.             }
  44.            
  45.            
  46.             //string cInput = Console.ReadLine();
  47.             //Fault.log(cInput);
  48.         }
  49.         public void PreRun()
  50.         {
  51.             Thread t = new Thread(() => { readConsoleInput(); });
  52.             // t.IsBackground = true;
  53.             t.Start();
  54.             Thread tr = new Thread(() => { Run(); });
  55.             tr.Start();
  56.  
  57.  
  58.             //Run();
  59.         }
  60.  
  61.         public void Run()
  62.         {
  63.             string[] channels = { "#flosd", "#mushisgosu" };
  64.  
  65.          
  66.             Reconnect();
  67.             //JoinChannel(channels);
  68.             ReadMessages();
  69.         }
  70.  
  71.         public void Reconnect()
  72.         {
  73.              
  74.             TwitchSocket = new TcpClient(Host, port);
  75.             ReadFromTwitch = new StreamReader(TwitchSocket.GetStream());
  76.             WriteToTwitch = new StreamWriter(TwitchSocket.GetStream());
  77.  
  78.             //send login data.
  79.             WriteToTwitch.WriteLine($"PASS {password}\r\n");
  80.             WriteToTwitch.WriteLine($"NICK {username}\r\n");
  81.             WriteToTwitch.Flush();
  82.             //WriteToTwitch.WriteLine($"JOIN #flosd\r\n");
  83.             WriteToTwitch.WriteLine($"JOIN {channel}\r\n");
  84.             WriteToTwitch.Flush();
  85.  
  86.  
  87.            
  88.         }
  89.  
  90.         public void JoinChannel(string[] Channels)
  91.         {
  92.             //list of channels #channel1 #channel2 etc
  93.             foreach(string channel in Channels)
  94.             {
  95.                 WriteToTwitch.WriteLine($"/JOIN {channel}\r\n");
  96.             }
  97.         }
  98.  
  99.         public void ReadMessages()
  100.         {
  101.             while (true)
  102.             {
  103.                 if (!TwitchSocket.Connected)
  104.                 {
  105.                     Run();
  106.                 }
  107.                 if(TwitchSocket.Available > 0 || ReadFromTwitch.Peek() >= 0)
  108.                 {
  109.                     string Data = ReadFromTwitch.ReadLine();
  110.                     //parse the data. :person_name!Person_name@Person_name.tmi.twitch.tv PRIVMSG  #Roomname : Message
  111.                     if (Data.Contains("PRIVMSG"))
  112.                     {
  113.                         try
  114.                         {
  115.                             string newData = Data.Substring(1, Data.Length - 1);
  116.                             string Poster = Data.Substring(1, Data.IndexOf('!') -1);
  117.                             string Data2 = newData.Substring(0, newData.IndexOf(':'));
  118.                             string Room = Data2.Substring(Data2.IndexOf('#'));
  119.                             Room = Room.TrimEnd();
  120.                             Poster = Poster.TrimEnd();
  121.                             string Data3 = Data.Substring(Data.IndexOf("PRIVMSG"));
  122.                             Data3 = Data3.Substring(Data3.IndexOf(':') + 1);
  123.  
  124.                             Console.WriteLine($"<{System.DateTime.Now}> [{Room}] {Poster}: {Data3}");
  125.  
  126.                         }
  127.                         catch(Exception ex)
  128.                         {
  129.                             Fault.log(ex.Message);
  130.                             if (!TwitchSocket.Connected)
  131.                             {
  132.                                 Run();
  133.                             }
  134.                             else
  135.                             {
  136.                                 ReadMessages();
  137.                             }
  138.                         }
  139.                     }
  140.                  
  141.                 }
  142.                
  143.             }
  144.            
  145.         }
  146.     }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement