Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using System.Net.Sockets;
- using UnityEngine;
- using System;
- using System.Collections;
- using UnityEngine.Networking;
- public class TwitchChatSimple : MonoBehaviour
- {
- private TcpClient twitchClient;
- private StreamReader reader;
- private StreamWriter writer;
- //Oauth password can be generated at https://twitchapps.com/tmi
- // [HideInInspector]
- public string username, password, channel;
- public string custom_rewards_id;
- public GameObject emotePrefab;
- public Transform startPoint;
- void Start()
- {
- Connect();
- }
- void Update()
- {
- // if not connected, reconnect
- if (!twitchClient.Connected)
- {
- Connect();
- }
- ProcessChat();
- }
- IEnumerator GetTexture(string url)
- {
- UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
- yield return www.SendWebRequest();
- Texture2D img = DownloadHandlerTexture.GetContent(www);
- GameObject emotePart = Instantiate(emotePrefab, startPoint.position, startPoint.rotation);
- emotePart.GetComponent<ParticleSystem>().GetComponent<Renderer>().material.mainTexture = img;
- }
- private void Connect()
- {
- // log into the twitch chat of a channel
- twitchClient = new TcpClient("irc.chat.twitch.tv", 6667);
- reader = new StreamReader(twitchClient.GetStream());
- writer = new StreamWriter(twitchClient.GetStream());
- writer.WriteLine("PASS " + password);
- writer.WriteLine("NICK " + username);
- writer.WriteLine("USER " + username + " 8 * :" + username);
- writer.WriteLine("JOIN #" + channel);
- writer.WriteLine("CAP REQ :twitch.tv/tags");
- writer.WriteLine("CAP REQ :twitch.tv/commands");
- writer.WriteLine("CAP REQ :twitch.tv/membership");
- writer.Flush();
- }
- private void ProcessChat()
- {
- // check if there is any data to read
- if (twitchClient.Available > 0)
- {
- string message = reader.ReadLine();
- print(message);
- if (message.Contains("PRIVMSG"))
- {
- // if the emotes list is not empty, get the emote texture
- if (!message.Contains("emotes=;"))
- {
- string[] stringSeparators = new string[] { "emotes=" };
- string[] result = message.Split(stringSeparators, StringSplitOptions.None);
- // split the emote string in case of multiple emotes
- var splitPointallEmotes = result[1].IndexOf(";", 0);
- var allemotes = result[1].Substring(0, splitPointallEmotes);
- string[] seperateEmotes = allemotes.Split('/');
- // grab all emote textures
- for (int i = 0; i < seperateEmotes.Length; i++)
- {
- var id = seperateEmotes[i].IndexOf(":", 0);
- var emoteID = seperateEmotes[i].Substring(0, id);
- // 1.0 / 2.0 / 3.0 is texture sizes
- StartCoroutine(GetTexture("https://static-cdn.jtvnw.net/emoticons/v1/" + emoteID + "/3.0"));
- }
- }
- if (message.Contains("@badge-info=subscriber"))
- {
- print("this person is a subscriber");
- }
- // custom reward
- if (message.Contains("custom-reward-id=" + custom_rewards_id))
- {
- print("Redeemed a custom reward");
- }
- }
- //reply to ping to stay connected
- if (message.Contains("PING :tmi.twitch.tv"))
- {
- writer.WriteLine("PONG " + "tmi.twitch.tv" + "\r\n");
- writer.Flush();
- print("replied");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement