Advertisement
Cookie042

Untitled

Oct 3rd, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1.  
  2.         public void Connect(string username, string password)
  3.         {
  4.             if (Connected)
  5.                 return;
  6.             Connected = true;
  7.  
  8.             twitchClient = new TcpClient("irc.chat.twitch.tv", 6667);
  9.             reader = new StreamReader(twitchClient.GetStream());
  10.             writer = new StreamWriter(twitchClient.GetStream());
  11.            
  12.             writer.WriteLine("CAP REQ :twitch.tv/tags twitch.tv/commands twitch.tv/membership");
  13.             writer.Flush();
  14.             writer.WriteLine("PASS " + "<oauth token goes here>");
  15.             writer.WriteLine("NICK " + username);
  16.             writer.WriteLine("USER " + username + " 8 * :" + username);
  17.  
  18.             //! TEMP CHANNEL
  19.             OnConnected?.Invoke();
  20.  
  21.             timer.Interval = TimeSpan.FromSeconds(.1f);
  22.             timer.Tick += TimerOnTick;
  23.             timer.Start();
  24.         }
  25.  
  26.         public void Disconnect()
  27.         {
  28.             timer.Stop();
  29.             twitchClient.Client.Close();
  30.             reader = null;
  31.             writer = null;
  32.             OnDisconnected?.Invoke();
  33.             Connected = false;
  34.             ConnectedChannels.Clear();
  35.         }
  36.  
  37.         private void TimerOnTick(object sender, EventArgs eventArgs)
  38.         {
  39.             if (Connected)
  40.             {
  41.                 ReadChat();
  42.             }
  43.             else
  44.             {
  45.                 timer.Tick -= TimerOnTick;
  46.                 timer.Stop();
  47.             }
  48.         }
  49.  
  50.         public void JoinChannel(string channel)
  51.         {
  52.             writer.WriteLine($"JOIN #{channel}");
  53.             writer.Flush();
  54.  
  55.             ConnectedChannels.Add( new IrcChannel( channel ));
  56.         }
  57.  
  58.         public void PartChannel(string channel)
  59.         {
  60.             writer.WriteLine($"PART #{channel}");
  61.             writer.Flush();
  62.             for (var i = ConnectedChannels.Count - 1; i >= 0; i--)
  63.             {
  64.                 var connectedChannel = ConnectedChannels[i];
  65.                 if (connectedChannel.ChannelName == channel)
  66.                 {
  67.                     ConnectedChannels.Remove(connectedChannel);
  68.                 }
  69.             }
  70.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement