Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- using System.Net.Sockets;
- using System.IO;
- namespace twitchbotplaying
- {
- public partial class Form1 : Form
- {
- #region Variables
- private static string userName = "toggbot";
- private static string password = "";
- private string channel = "sip_";
- IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
- NetworkStream serverStream = default(NetworkStream);
- string readData = "";
- Thread chatThread;
- #endregion
- public Form1()
- {
- InitializeComponent();
- }
- private void getMessage()
- {
- serverStream = irc.tcpClient.GetStream();
- int buffssize = 0;
- byte[] inStream = new byte[10025];
- buffssize = irc.tcpClient.ReceiveBufferSize;
- while(true)
- {
- try
- {
- readData = irc.readMessage();
- msg();
- }
- catch(Exception e)
- {
- }
- }
- }
- private void msg()
- {
- if (this.InvokeRequired) this.Invoke(new MethodInvoker(msg));
- else
- {
- if (readData.Contains("PRIVMSG"))
- {
- if (readData.Contains("bits="))
- {
- string[] bitVal = readData.Split(new[] { "bits=", ":" }, StringSplitOptions.None);
- rtxtChatMeta.AppendText(bitVal[0] + " " + bitVal[1] + Environment.NewLine);
- }
- else
- {
- string[] message = readData.Split(new[] { "#" + channel + " :" }, StringSplitOptions.None);
- string username = readData.Split(new[] { ":", "!" }, StringSplitOptions.None)[1];
- rtxtChatMessage.AppendText(/*username + ": " +*/ message[1] + Environment.NewLine);
- }
- }
- else
- {
- rtxtChatMeta.AppendText(readData + Environment.NewLine);
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- irc.joinRoom(channel);
- chatThread = new Thread(getMessage);
- chatThread.Start();
- }
- private void Form1_Closing(object sender, FormClosingEventArgs e)
- {
- irc.leaveRoom();
- serverStream.Dispose();
- Environment.Exit(0);
- }
- }
- class IrcClient
- {
- private string userName;
- private string channel;
- public TcpClient tcpClient;
- private StreamReader inputStream;
- private StreamWriter outputStream;
- public IrcClient(string ip, int port, string userName, string password)
- {
- tcpClient = new TcpClient(ip, port);
- inputStream = new StreamReader(tcpClient.GetStream());
- outputStream = new StreamWriter(tcpClient.GetStream());
- outputStream.WriteLine("PASS " + password);
- outputStream.WriteLine("NICK " + userName);
- outputStream.WriteLine("USER " + userName + " 8 :" + userName);
- outputStream.WriteLine("CAP REQ :twitch.tv/membership");
- outputStream.WriteLine("CAP REQ :twitch.tv/commands");
- outputStream.WriteLine("CAP REQ :twitch.tv/tags");
- outputStream.Flush();
- }
- public void joinRoom(string channel)
- {
- this.channel = channel;
- outputStream.WriteLine("JOIN #" + channel);
- outputStream.Flush();
- }
- public void leaveRoom()
- {
- outputStream.Close();
- inputStream.Close();
- }
- public void sendIrcMessage(string message)
- {
- outputStream.WriteLine(message);
- outputStream.Flush();
- }
- public void sendChatMessage(string message)
- {
- sendIrcMessage(":" + userName + "|" + userName + "@" + ".tmi.twitch.tv PRIVMSG #" + channel + ":" + message);
- }
- public void pingResponse()
- {
- sendIrcMessage("PONG tmi.twitch.tv\r\n");
- }
- public string readMessage()
- {
- string message = "";
- message = inputStream.ReadLine();
- return (message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement