Advertisement
Guest User

TwitchChatSimple

a guest
Jun 23rd, 2021
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | None | 0 0
  1. using System.IO;
  2. using System.Net.Sockets;
  3. using UnityEngine;
  4. using System;
  5. using System.Collections;
  6. using UnityEngine.Networking;
  7.  
  8. public class TwitchChatSimple : MonoBehaviour
  9. {
  10.  
  11.     private TcpClient twitchClient;
  12.     private StreamReader reader;
  13.     private StreamWriter writer;
  14.     //Oauth password can be generated at https://twitchapps.com/tmi
  15.     // [HideInInspector]
  16.     public string username, password, channel;
  17.  
  18.     public string custom_rewards_id;
  19.  
  20.     public GameObject emotePrefab;
  21.  
  22.     public Transform startPoint;
  23.     void Start()
  24.     {
  25.         Connect();
  26.     }
  27.  
  28.     void Update()
  29.     {
  30.         // if not connected, reconnect
  31.         if (!twitchClient.Connected)
  32.         {
  33.             Connect();
  34.         }
  35.  
  36.         ProcessChat();
  37.     }
  38.  
  39.  
  40.     IEnumerator GetTexture(string url)
  41.     {
  42.         UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
  43.         yield return www.SendWebRequest();
  44.  
  45.         Texture2D img = DownloadHandlerTexture.GetContent(www);
  46.         GameObject emotePart = Instantiate(emotePrefab, startPoint.position, startPoint.rotation);
  47.         emotePart.GetComponent<ParticleSystem>().GetComponent<Renderer>().material.mainTexture = img;
  48.  
  49.     }
  50.  
  51.     private void Connect()
  52.     {
  53.         // log into the twitch chat of a channel
  54.         twitchClient = new TcpClient("irc.chat.twitch.tv", 6667);
  55.         reader = new StreamReader(twitchClient.GetStream());
  56.         writer = new StreamWriter(twitchClient.GetStream());
  57.         writer.WriteLine("PASS " + password);
  58.         writer.WriteLine("NICK " + username);
  59.         writer.WriteLine("USER " + username + " 8 * :" + username);
  60.         writer.WriteLine("JOIN #" + channel);
  61.         writer.WriteLine("CAP REQ :twitch.tv/tags");
  62.         writer.WriteLine("CAP REQ :twitch.tv/commands");
  63.         writer.WriteLine("CAP REQ :twitch.tv/membership");
  64.         writer.Flush();
  65.     }
  66.  
  67.     private void ProcessChat()
  68.     {
  69.         // check if there is any data to read
  70.         if (twitchClient.Available > 0)
  71.         {
  72.             string message = reader.ReadLine();
  73.             print(message);
  74.  
  75.             if (message.Contains("PRIVMSG"))
  76.             {
  77.                 // if the emotes list is not empty, get the emote texture
  78.                 if (!message.Contains("emotes=;"))
  79.                 {
  80.  
  81.                     string[] stringSeparators = new string[] { "emotes=" };
  82.                     string[] result = message.Split(stringSeparators, StringSplitOptions.None);
  83.                     // split the emote string in case of multiple emotes
  84.                     var splitPointallEmotes = result[1].IndexOf(";", 0);
  85.                     var allemotes = result[1].Substring(0, splitPointallEmotes);
  86.                     string[] seperateEmotes = allemotes.Split('/');
  87.                     // grab all emote textures
  88.                     for (int i = 0; i < seperateEmotes.Length; i++)
  89.                     {
  90.                         var id = seperateEmotes[i].IndexOf(":", 0);
  91.                         var emoteID = seperateEmotes[i].Substring(0, id);
  92.                         // 1.0 / 2.0 / 3.0 is texture sizes
  93.                         StartCoroutine(GetTexture("https://static-cdn.jtvnw.net/emoticons/v1/" + emoteID + "/3.0"));
  94.                     }
  95.                 }
  96.  
  97.                 if (message.Contains("@badge-info=subscriber"))
  98.                 {
  99.                     print("this person is a subscriber");
  100.                 }
  101.  
  102.                 // custom reward
  103.                 if (message.Contains("custom-reward-id=" + custom_rewards_id))
  104.                 {
  105.                     print("Redeemed a custom reward");
  106.  
  107.                 }
  108.  
  109.  
  110.             }
  111.             //reply to ping to stay connected
  112.             if (message.Contains("PING :tmi.twitch.tv"))
  113.             {
  114.                 writer.WriteLine("PONG " + "tmi.twitch.tv" + "\r\n");
  115.                 writer.Flush();
  116.                 print("replied");
  117.             }
  118.  
  119.  
  120.         }
  121.     }
  122.  
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement