Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net.Sockets;
- using UnityEngine;
- public class botScript : MonoBehaviour {
- TcpClient tcpClient;
- StreamReader reader;
- StreamWriter writer;
- string userName, password, chatCommandId, chatMessagePrefix;
- public string channelName;
- public string oAuthToken;
- DateTime lastMessage;
- Queue<string> sendMessageQueue;
- Queue<string> timedQueue;
- void Start () {
- sendMessageQueue = new Queue<string>();
- timedQueue = new Queue<string>();
- this.userName = "lucthesloth".ToLower();
- this.password = "oauth:***********************************";
- chatCommandId = "PRIVMSG";
- //chatMessagePrefix = String.Format(":{0}!{0}@{0}.tmi.twitch.tv {1} #{2} :", userName, chatCommandId, channelName);
- chatMessagePrefix = String.Format("PRIVMSG #{0} :", channelName);
- Recoonect();
- //sendMessageQueue.Enqueue(String.Format("Luc bot v1.0 has come online, use !lick to request a lick. (Now licking on {0})", channelName));
- StartCoroutine(emptyTimedQueue());
- }
- void OnApplicationQuit()
- {
- sendMessageQueue.Enqueue(String.Format("rooBot Luc bot going offline rooBot"));
- }
- private void Recoonect()
- {
- tcpClient = new TcpClient("irc.twitch.tv", 6667);
- reader = new StreamReader(tcpClient.GetStream());
- writer = new StreamWriter(tcpClient.GetStream());
- writer.WriteLine("PASS " + password);
- writer.WriteLine("NICK " + userName);
- writer.WriteLine("CAP REQ :twitch.tv/membership");
- writer.WriteLine("JOIN #" + channelName);
- writer.Flush();
- lastMessage = DateTime.Now;
- }
- // Update is called once per frame
- void Update () {
- if (!tcpClient.Connected)
- {
- Recoonect();
- }
- TrySendingMessages();
- TryReceiveMessages();
- if (Input.GetKey(KeyCode.P))
- {
- Recoonect();
- }
- }
- private void TryReceiveMessages()
- {
- if(tcpClient.Available > 0)
- {
- var message = reader.ReadLine();
- //print(String.Format("\r\n{0}", message));
- var iCollon = message.IndexOf(":", 1);
- if (iCollon > 0)
- {
- var command = message.Substring(1, iCollon);
- if (command.Contains(chatCommandId))
- {
- var iBang = command.IndexOf("!");
- if (iBang > 0)
- {
- var speaker = command.Substring(0, iBang);
- var chatMessage = message.Substring(iCollon + 1);
- TryReceiveMessages(speaker, chatMessage);
- }
- }
- }
- }
- }
- private void TryReceiveMessages(string speaker, string chatMessage)
- {
- print(String.Format("\r\n{0}: {1}", speaker, chatMessage));
- /*if (chatMessage.ToLower().Equals("!lick"))
- {
- sendTwitchMessage(String.Format("{0} requested to be licked! rooLick {0} rooLick2", speaker));
- }*/
- if (chatMessage.ToLower().StartsWith("!love"))
- {
- timedQueue.Enqueue(String.Format("rooLove {0} rooLove", speaker));
- StartCoroutine(emptyTimedQueue());
- }
- else
- if (chatMessage.ToLower().Contains("!lick") && speaker.ToLower().Equals("lucthesloth"))
- {
- timedQueue.Enqueue(String.Format("rooLick {0} rooLick2", chatMessage.Split(new string[] { "!lick" }, System.StringSplitOptions.None)[1]));
- StartCoroutine(emptyTimedQueue());
- } else if (chatMessage.ToLower().Contains("!quit") && speaker.ToLower().Equals("lucthesloth"))
- {
- Application.Quit();
- } else if (chatMessage.ToLower().StartsWith("!") && speaker.ToLower().Equals("lucthesloth"))
- {
- timedQueue.Enqueue(String.Format("{0}{0}{0}", chatMessage.Replace('!', ' ')));
- StartCoroutine(emptyTimedQueue());
- }
- }
- private void sendTwitchMessage(string v)
- {
- sendMessageQueue.Enqueue(v);
- }
- IEnumerator emptyTimedQueue()
- {
- if (timedQueue.Count > 0)
- {
- yield return new WaitForSeconds(1f);
- sendTwitchMessage(timedQueue.Dequeue());
- }
- }
- private void TrySendingMessages()
- {
- if (DateTime.Now - lastMessage > TimeSpan.FromSeconds(2))
- {
- if(sendMessageQueue.Count > 0)
- {
- var message = sendMessageQueue.Dequeue();
- writer.WriteLine(String.Format("{0}{1}", chatMessagePrefix, message));
- writer.Flush();
- lastMessage = DateTime.Now;
- print(String.Format("{0}{1}", chatMessagePrefix, message));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement