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.IO;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace TwitchChatBot
- {
- public partial class mainWindow : Form
- {
- #region variables
- private static string userName = "amg_bot";
- private static string password = "oauth:someotherbsafter";
- IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
- NetworkStream serverStream = default(NetworkStream);
- string readData = "";
- Thread chatThread;
- #endregion
- public mainWindow()
- {
- //Initialization of the Windows Forms Components
- InitializeComponent();
- }
- private void mainWindow_Load(object sender, EventArgs e)
- {
- irc.joinRoom("officialantimattergaming");
- chatThread = new Thread(getMessage);
- chatThread.Start();
- }
- private void getMessage()
- {
- serverStream = irc.tcpClient.GetStream();
- int buffSize = 0;
- byte[] intStream = new byte[10025];
- buffSize = 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
- {
- chatWindow.Text = chatWindow.Text + readData.ToString() + Environment.NewLine;
- }
- }
- }
- 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 * :");
- outputStream.WriteLine("CAP REQ :twitch.tv/commands");
- outputStream.WriteLine("CAP REQ :twitch.tv/membership");
- 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 + "@" + 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