Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.IO;
- using System.Net.Sockets;
- using System.Threading;
- namespace TwitchChatBot
- {
- class Program
- {
- private string username = "";
- private string password = "";
- private string channel = "";
- private string Host = "irc.twitch.tv";
- private int port = 6667;
- StreamReader ReadFromTwitch;
- StreamWriter WriteToTwitch;
- TcpClient TwitchSocket;
- static void Main(string[] args)
- {
- Program TwitchBot = new Program();
- TwitchBot.Run();
- //TwitchBot.Reconnect();
- //TwitchBot.ReadMessages();
- }
- public void readConsoleInput()
- {
- while (true)
- {
- if (Console.KeyAvailable)
- {
- string cInput = Console.ReadLine();
- Fault.log(cInput);
- }
- }
- //string cInput = Console.ReadLine();
- //Fault.log(cInput);
- }
- public void PreRun()
- {
- Thread t = new Thread(() => { readConsoleInput(); });
- // t.IsBackground = true;
- t.Start();
- Thread tr = new Thread(() => { Run(); });
- tr.Start();
- //Run();
- }
- public void Run()
- {
- string[] channels = { "#flosd", "#mushisgosu" };
- Reconnect();
- //JoinChannel(channels);
- ReadMessages();
- }
- public void Reconnect()
- {
- TwitchSocket = new TcpClient(Host, port);
- ReadFromTwitch = new StreamReader(TwitchSocket.GetStream());
- WriteToTwitch = new StreamWriter(TwitchSocket.GetStream());
- //send login data.
- WriteToTwitch.WriteLine($"PASS {password}\r\n");
- WriteToTwitch.WriteLine($"NICK {username}\r\n");
- WriteToTwitch.Flush();
- //WriteToTwitch.WriteLine($"JOIN #flosd\r\n");
- WriteToTwitch.WriteLine($"JOIN {channel}\r\n");
- WriteToTwitch.Flush();
- }
- public void JoinChannel(string[] Channels)
- {
- //list of channels #channel1 #channel2 etc
- foreach(string channel in Channels)
- {
- WriteToTwitch.WriteLine($"/JOIN {channel}\r\n");
- }
- }
- public void ReadMessages()
- {
- while (true)
- {
- if (!TwitchSocket.Connected)
- {
- Run();
- }
- if(TwitchSocket.Available > 0 || ReadFromTwitch.Peek() >= 0)
- {
- string Data = ReadFromTwitch.ReadLine();
- //parse the data. :person_name!Person_name@Person_name.tmi.twitch.tv PRIVMSG #Roomname : Message
- if (Data.Contains("PRIVMSG"))
- {
- try
- {
- string newData = Data.Substring(1, Data.Length - 1);
- string Poster = Data.Substring(1, Data.IndexOf('!') -1);
- string Data2 = newData.Substring(0, newData.IndexOf(':'));
- string Room = Data2.Substring(Data2.IndexOf('#'));
- Room = Room.TrimEnd();
- Poster = Poster.TrimEnd();
- string Data3 = Data.Substring(Data.IndexOf("PRIVMSG"));
- Data3 = Data3.Substring(Data3.IndexOf(':') + 1);
- Console.WriteLine($"<{System.DateTime.Now}> [{Room}] {Poster}: {Data3}");
- }
- catch(Exception ex)
- {
- Fault.log(ex.Message);
- if (!TwitchSocket.Connected)
- {
- Run();
- }
- else
- {
- ReadMessages();
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement