Advertisement
Guest User

twitch whisper irc

a guest
Jan 31st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.49 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine.UI;
  6.  
  7. public class TwitchChat : MonoBehaviour {
  8.  
  9.    
  10.     System.IO.TextReader input;
  11.     System.IO.TextWriter output;
  12.     string buf;
  13.     string nick, owner, server, chan, password;
  14.     int port;
  15.     System.Net.Sockets.TcpClient sock;
  16.  
  17.      public GameManager gameManager;
  18.  
  19.      string lastusercolor, lastusercolorname;
  20.  
  21.     void awake()
  22.     {
  23.    
  24.     }
  25.  
  26.     // Use this for initialization
  27.     void Start () {
  28.  
  29.         sock = new System.Net.Sockets.TcpClient();
  30.    
  31.         //Get nick, owner, server, port, and channel from user
  32.  
  33.         nick = "username";
  34.         server = "199.9.253.120";  // for whispers this must be a Group Chat server from http://twitchstatus.com/
  35.         port = 443;
  36.         password = "oauth:XXXXXXXXXXX";
  37.         chan = "#channel";
  38.  
  39.         //Connect to irc server and get input and output text streams from TcpClient.
  40.         sock.Connect(server, port);
  41.         if (!sock.Connected) {
  42.             Debug.Log("TwitchChat Failed to connect!");
  43.             return;
  44.         }
  45.  
  46.         input = new System.IO.StreamReader(sock.GetStream());
  47.         output = new System.IO.StreamWriter(sock.GetStream());
  48.        
  49.         //Starting USER and NICK login commands
  50.         output.Write(
  51.             "PASS " + password + "\r\n" +
  52.             "USER " + nick + " 0 * :" + nick + "\r\n" +
  53.             "NICK " + nick + "\r\n"
  54.             );
  55.         output.Flush();
  56.  
  57.         InvokeRepeating("FlushBufferDelayed", 5f, 5f);
  58.     }
  59.  
  60.        
  61.     void Update () {
  62.    
  63.         if (!sock.Connected)
  64.         {
  65.             Debug.Log ("lost connection");
  66.                 return;
  67.         }
  68.                    
  69.    
  70.         if (sock.Available > 0)
  71.             buf = input.ReadLine ();
  72.         else
  73.             buf= "";
  74.  
  75.         try
  76.         {
  77.  
  78.             if (buf != "")
  79.             {
  80.                 Debug.Log(buf);
  81.  
  82.                 //Send pong reply to any ping messages
  83.                 if (buf.StartsWith("PING"))
  84.                 {
  85.                     output.Write(buf.Replace("PING", "PONG") + "\r\n");
  86.                     output.Flush();
  87.                 }
  88.  
  89.  
  90.                 //After server sends 001 command, we can join a channel
  91.                 if (buf.Split(' ')[1] == "001")
  92.                 {
  93.                     output.Write(
  94.                         "JOIN " + chan + "\r\n"
  95.                         );
  96.                     output.Flush();
  97.  
  98.                     // after we join request whisper messages
  99.                     output.Write(
  100.                         "CAP REQ :twitch.tv/commands\r\n"
  101.                         );
  102.                     output.Flush();
  103.  
  104.                     // after we join request colors and other messages
  105.                     output.Write(
  106.                         "CAP REQ :twitch.tv/tags\r\n"
  107.                         );
  108.                     output.Flush();
  109.  
  110.                     output.Write(
  111.                          "CAP REQ :twitch.tv/membership\r\n"
  112.                     );
  113.                     output.Flush();
  114.  
  115.                 }
  116.  
  117.                 if (buf.StartsWith("@")) // is a message priv/whisper with user tags
  118.                 {
  119.              
  120.                     if (buf.Split(' ')[2] == "WHISPER" && buf.StartsWith("@color=#")) // user color
  121.                     {
  122.                         lastusercolorname = buf.Split(';')[1].ToString().Replace("display-name=","");
  123.                         lastusercolor = buf.Split(';')[0].ToString().Replace("@color=", "");
  124.                         if (gameManager.players != null && gameManager.players.Count > 0)
  125.                         {
  126.                             foreach (GameObject tf in gameManager.players)
  127.                             {
  128.                                 Player pl = tf.GetComponent<Player>();
  129.                                 if (pl.isActiveAndEnabled)
  130.                                 {
  131.                                     if (pl.TwitchUserName.ToLower() == lastusercolorname.ToLower())
  132.                                     {
  133.                                         pl.playerColor = HexToColor(lastusercolor);
  134.                                         pl.UpdatePlayerColor();
  135.                                     }
  136.                                 }
  137.                             }
  138.                         }
  139.                         output.Write(
  140.                         "PING\r\n"
  141.                         );
  142.                         output.Flush();
  143.                     }
  144.  
  145.  
  146.                     // there is new twitch chat messages to parse
  147.                     if (buf.Split(' ')[2] == "WHISPER")
  148.                     {
  149.                         string inputUserName = buf.Split(';')[1].ToString().Replace("display-name=", "");
  150.                         string messageText = buf.Split(':')[2];
  151.  
  152.                         if (messageText.ToLower() == "!test")
  153.                         {
  154.                             for (int i = 1; i < 50; i++)
  155.                             {
  156.                                 gameManager.CreatePlayer("test player " + i);
  157.                             }
  158.                         }
  159.  
  160.                         if (messageText.ToLower() == "!setup")
  161.                         {
  162.                             output.Write(
  163.                                             "PRIVMSG " + chan + " :/mod afastrunner\r\n"
  164.                                         );
  165.                             output.Flush();
  166.                         }
  167.  
  168.  
  169.  
  170.                         if (messageText.ToLower() == "!play")
  171.                         {
  172.                             Debug.Log("new player request");
  173.                             bool newPlayer = true;
  174.                             if (gameManager.players != null && gameManager.players.Count > 0)
  175.                             {
  176.                                 foreach (GameObject tf in gameManager.players)
  177.                                 {
  178.                                     Player pl = tf.GetComponent<Player>();
  179.                                     if (pl.isActiveAndEnabled)
  180.                                     {
  181.                                         if (pl.TwitchUserName.ToLower() == inputUserName.ToLower())
  182.                                         {
  183.                                             newPlayer = false;
  184.                                             output.Write(
  185.                                                   "PRIVMSG " + chan + " :/w " + inputUserName + " your allready playing.\r\n"
  186.                                                 );
  187.                                             output.Flush();
  188.                                         }
  189.                                     }
  190.                                 }
  191.                             }
  192.                             else
  193.                             {
  194.                                 newPlayer = true;
  195.                             }
  196.  
  197.                             if (newPlayer)
  198.                             {
  199.                                 gameManager.CreatePlayer(inputUserName);
  200.                          
  201.                                 if (gameManager.players != null && gameManager.players.Count > 0)
  202.                                 {
  203.                                     foreach (GameObject tf in gameManager.players)
  204.                                     {
  205.                                         Player pl = tf.GetComponent<Player>();
  206.                                         if (pl.isActiveAndEnabled)
  207.                                         {
  208.                                             if (pl.TwitchUserName.ToLower() == lastusercolorname.ToLower())
  209.                                             {
  210.                                                 pl.playerColor = HexToColor(lastusercolor);
  211.                                                 pl.UpdatePlayerColor();
  212.                                             }
  213.                                         }
  214.                                     }
  215.                                 }
  216.                             }
  217.                         }
  218.              
  219.  
  220.                         // go through each player and check if they have new commands
  221.                         if (gameManager.players != null && gameManager.players.Count > 0)
  222.                         {
  223.                             foreach (GameObject tf in gameManager.players)
  224.                             {
  225.                                 Player pl = tf.GetComponent<Player>();
  226.                                 if (pl.isActiveAndEnabled)
  227.                                 {
  228.                                     if (pl.TwitchUserName.ToLower() == inputUserName.ToLower())
  229.                                     {
  230.                                         pl.TwitchMessage(messageText); // send message from user to there player instance  
  231.                                     }
  232.                                 }
  233.  
  234.                             }
  235.                         }
  236.  
  237.                      
  238.                     } // end of incoming PRIVMSG parsing
  239.                 } // end of whispers
  240.  
  241.                
  242.  
  243.             } // end of buffer
  244.  
  245.                 // go through each player and check if they have whispers to send back to the player via whisper
  246.                 if (gameManager.players != null && gameManager.players.Count > 0)
  247.                 {
  248.                     foreach (GameObject tf in gameManager.players)
  249.                     {
  250.                         Player pl = tf.GetComponent<Player>();
  251.                         if (pl.isActiveAndEnabled)
  252.                         {
  253.                             if (pl.getMessageToTwitchWhisper(false) != "") // get any message from the player instance that needs to go back to the user in twitch chat
  254.                             {
  255.                                 //   Debug.LogWarning("output whisper directly to player");
  256.                                 output.Write(
  257.                                     "PRIVMSG " + chan + " :/w " + pl.TwitchUserName + " " + pl.getMessageToTwitchWhisper(true) + " \r\n"
  258.                                 );
  259.                             }
  260.                         }
  261.                     }
  262.                 }
  263.         }
  264.         catch (System.Exception ex)
  265.         {
  266.             Debug.Log("errored" + ex.ToString());
  267.         }
  268.     }
  269.  
  270.     void FlushBufferDelayed()
  271.     {
  272.          output.Flush();           
  273.     }
  274.  
  275.     Color HexToColor(string hex)
  276.     {
  277.         hex = hex.Replace("#", "");
  278.         byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
  279.         byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
  280.         byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
  281.         return new Color32(r, g, b, 255);
  282.     }
  283.  
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement