Skip to content

Instantly share code, notes, and snippets.

Created September 17, 2017 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6c8e2d68e7b442d936e914f6a76e0999 to your computer and use it in GitHub Desktop.
Save anonymous/6c8e2d68e7b442d936e914f6a76e0999 to your computer and use it in GitHub Desktop.
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.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
#region Variables
private static string userName = "williamtgfbot";
private static string password = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
NetworkStream serverStream = default(NetworkStream);
string readData = "";
Thread chatThread;
#endregion
private bool completedConnection = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
irc.joinRoom("william_tgf");
chatThread = new Thread(getMessage);
chatThread.Start();
}
public void getMessage()
{
serverStream = irc.tcpClient.GetStream();
int buffsize = 0;
byte[] inStream = 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
{
ChatBox.Text = ChatBox.Text + readData.ToString() + Environment.NewLine;
string actualMessage = readData.ToString();
if (completedConnection)
{
string[] splitStarting = actualMessage.Split(':');
if(splitStarting[2] == "Trigger")
{
System.Threading.Timer timer = null;
timer = new System.Threading.Timer((obj) =>
{
irc.sendChatmessage("Response");
timer.Dispose();
},
null, 1000, System.Threading.Timeout.Infinite);
}
}
//end of else check:
if (actualMessage == ":tmi.twitch.tv ROOMSTATE #william_tgf")
{
completedConnection = true;
}
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
public class IrcClient
{
private string userName = "williamtgfbot";
private string channel;
public TcpClient tcpClient;
private StreamReader inputStream; // coming to the bot from chat
private StreamWriter outputStream; // things going to chat from the bot
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.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 pingResposne()
{
sendIrcMessage("PONG tmi.twitch.tv\r\n");
}
public string readMessage()
{
string message = "";
message = inputStream.ReadLine();
return message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment