Advertisement
CrateStudios

Twitch Template

Apr 21st, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.12 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Net.Sockets;
  4.  
  5. namespace revloCheat
  6. {
  7.     class revloCheat
  8.     {
  9.         private static byte[] data;
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             Int32 port = 6667;
  15.             TcpClient client = new TcpClient("irc.twitch.tv", port);
  16.             // Enter in channel (the username of the stream chat you wish to connect to) without the #
  17.             string channel = "awesomejoey";
  18.  
  19.             // Get a client stream for reading and writing.
  20.             //  Stream stream = client.GetStream();
  21.  
  22.             NetworkStream stream = client.GetStream();
  23.  
  24.             // Send the message to the connected TcpServer.
  25.  
  26.             string loginstring = "PASS oauth:\r\nNICK Cwavs\r\n";
  27.             Byte[] login = System.Text.Encoding.ASCII.GetBytes(loginstring);
  28.             stream.Write(login, 0, login.Length);
  29.             Console.WriteLine("Sent login.\r\n");
  30.             Console.WriteLine(loginstring);
  31.  
  32.             // Receive the TcpServer.response.
  33.             // Buffer to store the response bytes.
  34.             data = new Byte[512];
  35.  
  36.             // String to store the response ASCII representation.
  37.             String responseData = String.Empty;
  38.  
  39.             // Read the first batch of the TcpServer response bytes.
  40.             Int32 bytes = stream.Read(data, 0, data.Length);
  41.             responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  42.             Console.WriteLine("Received WELCOME: \r\n\r\n{0}", responseData);
  43.  
  44.             // send message to join channel
  45.  
  46.             string joinstring = "JOIN " + "#" + channel + "\r\n";
  47.             Byte[] join = System.Text.Encoding.ASCII.GetBytes(joinstring);
  48.             stream.Write(join, 0, join.Length);
  49.             Console.WriteLine("Sent channel join.\r\n");
  50.             Console.WriteLine(joinstring);
  51.  
  52.             // PMs the channel to announce that it's joined and listening
  53.             // These three lines are the example for how to send something to the channel
  54.  
  55.             string announcestring = channel + "!" + channel + "@" + channel + ".tmi.twitch.tv PRIVMSG " + channel + " BOT ENABLED\r\n";
  56.             Byte[] announce = System.Text.Encoding.ASCII.GetBytes(announcestring);
  57.             stream.Write(announce, 0, announce.Length);
  58.  
  59.             // Lets you know its working
  60.  
  61.             Console.WriteLine("TWITCH CHAT HAS BEGUN.\r\n\r\nr.");
  62.             Console.WriteLine("\r\nBE CAREFUL.");
  63.  
  64.             while (true)
  65.             {
  66.  
  67.                 // build a buffer to read the incoming TCP stream to, convert to a string
  68.  
  69.                 byte[] myReadBuffer = new byte[1024];
  70.                 StringBuilder myCompleteMessage = new StringBuilder();
  71.                 int numberOfBytesRead = 0;
  72.  
  73.                 // Incoming message may be larger than the buffer size.
  74.                 do
  75.                 {
  76.                     try { numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); }
  77.                     catch (Exception e)
  78.                     {
  79.                         Console.WriteLine("OH, SOMETHING WENT WRONG\r\n", e);
  80.                     }
  81.  
  82.                     myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
  83.                 }
  84.  
  85.                 // when we've received data, do Things
  86.  
  87.                 while (stream.DataAvailable);
  88.  
  89.                 // Print out the received message to the console.
  90.                 Console.WriteLine(myCompleteMessage.ToString());
  91.                 switch (myCompleteMessage.ToString())
  92.                 {
  93.                     // Every 5 minutes the Twitch server will send a PING, this is to respond with a PONG to keepalive
  94.  
  95.                     case "PING :tmi.twitch.tv\r\n":
  96.                         try
  97.                         {
  98.                             Byte[] say = System.Text.Encoding.ASCII.GetBytes("PONG :tmi.twitch.tv\r\n");
  99.                             stream.Write(say, 0, say.Length);
  100.                             Console.WriteLine("Ping? Pong!");
  101.                         }
  102.                         catch (Exception e)
  103.                         {
  104.                             Console.WriteLine("OH, SOMETHING WENT WRONG\r\n", e);
  105.                         }
  106.                         break;
  107.  
  108.                     // If it's not a ping, it's probably something we care about.  Try to parse it for a message.
  109.                     default:
  110.                         try
  111.                         {
  112.                             string messageParser = myCompleteMessage.ToString();
  113.                             string[] message = messageParser.Split(':');
  114.                             string[] preamble = message[1].Split(' ');
  115.                             string tochat;
  116.  
  117.                             // This means it's a message to the channel.  Yes, PRIVMSG is IRC for messaging a channel too
  118.                             if (preamble[1] == "PRIVMSG")
  119.                             {
  120.                                 string[] sendingUser = preamble[0].Split('!');
  121.                                 tochat = sendingUser[0] + ": " + message[2];
  122.  
  123.                                 // sometimes the carriage returns get lost (??)
  124.                                 if (tochat.Contains("\n") == false)
  125.                                 {
  126.                                     tochat = tochat + "\n";
  127.                                 }
  128.  
  129.                                 // Ignore some well known bots
  130.                                 if (sendingUser[0] != "moobot" && sendingUser[0] != "whale_bot")
  131.                                 {
  132.                                     SendKeys.SendWait(tochat.TrimEnd('\n'));
  133.                                 }
  134.                             }
  135.                             // A user joined.
  136.                             else if (preamble[1] == "JOIN")
  137.                             {
  138.                                 string[] sendingUser = preamble[0].Split('!');
  139.                                 tochat = "JOINED: " + sendingUser[0];
  140.                                 //    Console.WriteLine(tochat);
  141.                                 SendKeys.SendWait(tochat.TrimEnd('\n'));
  142.                             }
  143.                         }
  144.                         // This is a disgusting catch for something going wrong that keeps it all running.  I'm sorry.
  145.                         catch (Exception e)
  146.                         {
  147.                             Console.WriteLine("OH, SOMETHING WENT WRONG\r\n", e);
  148.                         }
  149.  
  150.                         // Uncomment the following for raw message output for debugging
  151.                         //
  152.                         // Console.WriteLine("Raw output: " + message[0] + "::" + message[1] + "::" + message[2]);
  153.                         // Console.WriteLine("You received the following message : " + myCompleteMessage);
  154.                         break;
  155.                 }
  156.             }
  157.  
  158.             // Close everything.  Should never happen because you gotta close the window.
  159.             stream.Close();
  160.             client.Close();
  161.             Console.WriteLine("\n Press Enter to continue...");
  162.             Console.Read();
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement