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.Net.Sockets;
- using System.IO;
- using System.Threading;
- using System.Data;
- using Newtonsoft.Json;
- using System.Net;
- using System.Diagnostics;
- using System.Text.RegularExpressions;
- namespace Gambly
- {
- public class IrcClient
- {
- private string userName = "";
- private TcpClient tcpClient;
- private StreamReader inputStream;
- private StreamWriter outputStream;
- private string channel = "";
- private bool IS_CONNECTED = false;
- private Thread listen, count, sender,pingsender;
- private string toFind;
- static List<Gambler> gamblers;
- static Entity viewers;
- private Queue<String> commands;
- private Config configs;
- public IrcClient(string ip, int port, string userName, string password, string channel)
- {
- try
- {
- Console.Write(Config.time() + ">> Loading IRClient");
- this.userName = userName;
- gamblers = new List<Gambler>();
- tcpClient = new TcpClient(ip, port);
- inputStream = new StreamReader(tcpClient.GetStream());
- configs = new Config();
- viewers = new Entity();
- commands = new Queue<string>();
- outputStream = new StreamWriter(tcpClient.GetStream());
- this.channel = channel;
- outputStream.WriteLine("PASS " + password);
- outputStream.WriteLine("NICK " + userName);
- outputStream.WriteLine("USER " + userName + " 8 * :" + userName);
- outputStream.Flush();
- toFind = "#" + channel + " :";
- Console.WriteLine("...done!");
- LoadFiles();
- listen = new Thread(ChatListener);
- listen.Start();
- sender = new Thread(ChatSender);
- sender.Start();
- count = new Thread(GetViewers);
- count.Start();
- sendChatMessage("I'm back bitches!");
- }
- catch (Exception)
- {
- Console.WriteLine("Connection has been lost");
- }
- }
- public void LoadFiles()
- {
- try
- {
- var file = System.IO.File.ReadAllText(configs.CONFIG_FILE);
- gamblers = JsonConvert.DeserializeObject<List<Gambler>>(file);
- }
- catch (Exception)
- {
- Console.WriteLine("file error");
- }
- }
- public void SaveFiles()
- {
- try
- {
- var file = JsonConvert.SerializeObject(gamblers, Formatting.Indented);
- System.IO.File.WriteAllText(configs.CONFIG_FILE, file);
- }
- catch (Exception)
- {
- Console.WriteLine("file");
- }
- }
- public bool getBool()
- {
- return IS_CONNECTED;
- }
- void ChatListener()
- {
- joinRoom(channel);
- while (true)
- {
- try
- {
- String message = readMessage();
- Console.WriteLine(message);
- if (message.Contains("PING"))
- {
- sendIrcMessage("PONG :tmi.twitch.tv\r\n");
- sendIrcMessage("PONG");
- sendIrcMessage("PING");
- sendIrcMessage("PING :tmi.twitch.tv\r\n");
- pongChat();
- }
- if (message.Contains("PRIVMSG"))
- {
- var myText = message.Substring(message.IndexOf(toFind) + toFind.Length);
- //listBox1.Invoke(new Action(() => textBox1.AppendText(texto)));
- if (myText.StartsWith("!"))
- {
- commands.Enqueue(message);
- Console.WriteLine(Config.time() + "queued command! by:" + getUser(message));
- }
- }
- }
- catch (Exception a)
- {
- Console.WriteLine("ChatListener failed " + a.Message);
- }
- }
- }
- void PingSender()
- {
- while (true)
- {
- try
- {
- sendIrcMessage("PONG :tmi.twitch.tv\r\n");
- Thread.Sleep(60000 * 5);
- }
- catch (Exception)
- {
- Console.WriteLine("erro ping");
- }
- }
- }
- void ChatSender()
- {
- while (true)
- {
- Thread.Sleep(320);
- try
- {
- if (commands.Count > 0)
- {
- string ex = commands.Dequeue();
- var myText = ex.Substring(ex.IndexOf(toFind) + toFind.Length);
- var text = ex.Split(';');
- var user = getUser(text[2]);
- if (myText.Contains("!coins"))
- {
- Console.WriteLine(user);
- var op = gamblers.Find(x => x.name == user.ToLower());
- sendChatMessage(op.name + " have " + op.points.ToString("N0") + " Bcoins");
- }
- if (myText.Contains("!time"))
- {
- var stp = new Stopwatch();
- stp.Start();
- var op = gamblers.FirstOrDefault(x => x.name == user.ToLower());
- TimeSpan guess = DateTime.Now - op.register;
- sendChatMessage(String.Format("{0} days, {1} hours, {2} minutes.", guess.Days, guess.Hours, guess.Minutes));
- stp.Stop();
- Console.WriteLine(stp.Elapsed);
- }
- }
- }
- catch (Exception a)
- {
- Console.WriteLine(a.Message);
- }
- }
- }
- void GetViewers()
- {
- while (true)
- {
- try
- {
- using (WebClient client = new WebClient())
- {
- // Console.Write(Config.time() + ">> Loading Viewers"); // *ch
- client.Headers["Client-ID"] = "tg7b4plvzsngwrwnjuj1ay5gvkryv6"; // Deve ser fornecida pelo usuario!
- client.Encoding = Encoding.UTF8;
- var s = client.DownloadString("https://tmi.twitch.tv/group/user/" + channel + "/chatters");
- var viewers = JsonConvert.DeserializeObject<Entity>(s);
- //viewers.chatters.viewers.Concat(viewers.chatters.moderators);
- viewers.chatters.viewers.AddRange(viewers.chatters.moderators);
- viewers.chatters.viewers.ForEach(viewer =>
- {
- if (gamblers.Exists(x => x.name == viewer))
- {
- // Console.WriteLine(Config.time() + " Já existe!");
- var op = gamblers.Find(x => x.name == viewer);
- op.points += configs.POINT_REWARD;
- }
- else
- {
- gamblers.Add(new Gambler(viewer, DateTime.Now, 0));
- }
- });
- //Console.WriteLine("...done! {0} New Viewers {1}", i, stp.Elapsed);
- s = JsonConvert.SerializeObject(gamblers, Formatting.Indented);
- System.IO.File.WriteAllText(configs.CONFIG_FILE, s);
- }
- Thread.Sleep(60000 * 10);
- }
- catch (Exception z)
- {
- Console.WriteLine(Config.time() + "[ERROR]" + z.Message + z.InnerException);
- }
- }
- }
- public void joinRoom(string channel)
- {
- this.channel = channel;
- outputStream.WriteLine("JOIN #" + channel + "\r\n"); //
- outputStream.WriteLine("CAP REQ :twitch.tv/tags\r\n");
- outputStream.Flush();
- }
- public void leaveRoom(string channel)
- {
- outputStream.WriteLine("PART #" + channel + "");
- IS_CONNECTED = false;
- outputStream.Flush();
- }
- 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 + "\r\n");
- //sendIrcMessage(":[email protected] PRIVMSG #barbosza: HAHA!");
- }
- public void sendPrivateMessage(string message, string user)
- {
- sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :/w " + user + " " + message + "\r\n");
- //sendIrcMessage(":[email protected] PRIVMSG #barbosza: HAHA!");
- }
- public void deleteChatMessage(string username)
- {
- sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :.timeout " + username + " 1\r\n");
- }
- public void banClient(string username)
- {
- sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :.ban " + username + "\r\n");
- }
- public void pongChat()
- {
- sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PONG " + channel + "\r\n");
- }
- public string getChannel()
- {
- return this.channel;
- }
- public string readMessage()
- {
- string message;
- try
- {
- message = inputStream.ReadLine();
- if (message == null)
- {
- return "null";
- }
- return message;
- }
- catch (Exception)
- {
- return "null";
- }
- }
- public string thisChannel(string a)
- {
- return this.channel;
- }
- public string getUser(string message)
- {
- try
- {
- return message.Substring(message.LastIndexOf('=') + 1);
- }
- catch (Exception a)
- {
- return "[ERROR] Cannot get user!" + a.Message;
- }
- }
- public void runCMD(string text)
- {
- try
- {
- if (text.Contains("csv"))
- {
- using (StreamWriter fp = new StreamWriter(@"A:\gamblers.csv"))
- {
- foreach (Gambler a in gamblers)
- {
- fp.WriteLine("{0}&{1}&{2}&{3}", a.name, a.points, a.register, a.prison_time);
- }
- fp.Close();
- }
- Console.WriteLine(Config.time() + "CSV file was exported succcesfully");
- }
- else if (text.StartsWith("add"))
- {
- string[] args = text.Split(' ');
- var opz = gamblers.FirstOrDefault(X => X.name == args[1].ToLower());
- if (opz == null) return;
- opz.points += int.Parse(args[2]);
- Console.WriteLine(Config.time() + "{0} added {1} points to {2}", "You", args[2], args[1]);
- }
- else if (text.StartsWith("tax"))
- {
- string[] args = text.Split(' ');
- float tax_value = float.Parse(args[1]) / 100;
- gamblers.ForEach(X => X.points = Convert.ToInt64(X.points * (1 - tax_value)));
- }
- else if (text.Contains("quit"))
- {
- var s = JsonConvert.SerializeObject(gamblers, Formatting.Indented);
- System.IO.File.WriteAllText(configs.CONFIG_FILE, s);
- System.Environment.Exit(0);
- }
- else if (text.Contains("ban"))
- {
- string[] cmd = text.Split(' ');
- var opz = gamblers.FirstOrDefault(X => X.name == cmd[1].ToLower());
- if (opz == null) return;
- var op = new DateTime();
- op = DateTime.Now.AddDays(double.Parse(cmd[2]));
- opz.prison_time = op;
- banClient(cmd[1].ToLower());
- sendChatMessage(String.Format("{0} was banished for {1} days", cmd[1], cmd[2]));
- }
- else
- {
- Console.WriteLine("Comando Inválido!");
- }
- }
- catch (Exception f)
- {
- Console.WriteLine(Config.time() + f.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment