Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Twitch chat integration issues

Discussion in 'Editor & General Support' started by unity_fqlyjbgDafPWCw, Jan 25, 2022.

  1. unity_fqlyjbgDafPWCw

    unity_fqlyjbgDafPWCw

    Joined:
    Feb 5, 2021
    Posts:
    9
    Sorry if this is the wrong place to post this.

    I'm trying to make a simple project that incorporates twitch chat. I've found a couple tutorials and the process seems fairly simple, I've got code that connects to my twitch account and seems to be reading the chat back to me, except it won't acknowledge anything messages posted in the chat:

    Here's my one script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using System.ComponentModel;
    5. using System.Net.Sockets;
    6. using System.IO;
    7. using UnityEngine;
    8. using UnityEngine.UI;
    9.  
    10. public class TwitchChat : MonoBehaviour
    11. {
    12.  
    13.     private TcpClient twitchClient;
    14.     private StreamReader reader;
    15.     private StreamWriter writer;
    16.     [SerializeField]
    17.     Text chatBox;
    18.  
    19.     public string username, password, channelName;// get the password from https://twitchapps.com/tmi
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.  
    25.         Connect();
    26.        
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         if(!twitchClient.Connected)
    33.         {
    34.             Connect();
    35.         }
    36.  
    37.         ReadChat();
    38.     }
    39.  
    40.     private void Connect()
    41.     {
    42.         twitchClient = new TcpClient("irc.chat.twitch.tv", 6667);
    43.         reader = new StreamReader(twitchClient.GetStream());
    44.         writer = new StreamWriter(twitchClient.GetStream());
    45.         writer.WriteLine("PASS " + password);
    46.         writer.WriteLine("NICK " + username);
    47.         writer.WriteLine("USER " + username + " 8 * :" + username);
    48.         writer.WriteLine("JOIN #" + channelName);
    49.         writer.Flush();
    50.     }
    51.  
    52.     private void ReadChat()
    53.     {
    54.         if(twitchClient.Available > 0)
    55.         {
    56.             string message = reader.ReadLine();
    57.             print(message);
    58.             //chatBox.text = chatBox.text + "\n" + message;
    59.             if(message.Contains("PRIVMSG"))
    60.             {
    61.                 //split the user's name from the string
    62.                 var splitPoint = message.IndexOf("!", 1);
    63.                 var chatName = message.Substring(0, splitPoint);
    64.                 chatName = chatName.Substring(1);
    65.  
    66.                 //split the user's message from the string
    67.                 splitPoint = message.IndexOf(":", 1);
    68.                 message = message.Substring(splitPoint + 1);
    69.                 print(String.Format("{0}: {1}", chatName, message));
    70.                 chatBox.text = chatBox.text + "\n" + String.Format("{0}: {1}", chatName, message);
    71.                
    72.  
    73.             }
    74.         }
    75.     }
    76. }
    I have private messages being posted to a text box, while the entirety of the info received is printed to the console. This all works except it won't receive any private messages. I don't know if this is something to do with my twitch account or I'm missing something on the Unity end (I'm not at all familiar with twitch, I created the account specifically to test this project) Here's what running the project looks like:

    View attachment 993627

    Where it says "chat" is where the chat messages should be appearing. I can print the content in the console to the chatbox so that's not the issue. I'd try searching around on twitch itself for answers but that website is a confusing pile of garbage, and all the info I've found just from googling says I'm doing everything right. I've tried using the twitch unity package as well, but that lead to different issues involving duplicate Newtonsoft dlls, then other specific issues when I tried renaming one of the Newtonsoft dlls.
     
  2. unity_fqlyjbgDafPWCw

    unity_fqlyjbgDafPWCw

    Joined:
    Feb 5, 2021
    Posts:
    9
    Solved: The username and channel name you send to twitch MUST BE LOWERCASE. otherwise you'll connect, but you won't receive any info back