· 7 years ago · Sep 11, 2018, 05:00 PM
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Net.Sockets;
6using UnityEngine;
7
8public class botScript : MonoBehaviour {
9
10 TcpClient tcpClient;
11 StreamReader reader;
12 StreamWriter writer;
13
14 string userName, password, chatCommandId, chatMessagePrefix;
15 public string channelName;
16 public string oAuthToken;
17 DateTime lastMessage;
18
19 Queue<string> sendMessageQueue;
20 Queue<string> timedQueue;
21
22 void Start () {
23 sendMessageQueue = new Queue<string>();
24 timedQueue = new Queue<string>();
25 this.userName = "lucthesloth".ToLower();
26 this.password = "oauth:***********************************";
27 chatCommandId = "PRIVMSG";
28 //chatMessagePrefix = String.Format(":{0}!{0}@{0}.tmi.twitch.tv {1} #{2} :", userName, chatCommandId, channelName);
29 chatMessagePrefix = String.Format("PRIVMSG #{0} :", channelName);
30 Recoonect();
31
32 //sendMessageQueue.Enqueue(String.Format("Luc bot v1.0 has come online, use !lick to request a lick. (Now licking on {0})", channelName));
33 StartCoroutine(emptyTimedQueue());
34 }
35 void OnApplicationQuit()
36 {
37 sendMessageQueue.Enqueue(String.Format("rooBot Luc bot going offline rooBot"));
38 }
39 private void Recoonect()
40 {
41 tcpClient = new TcpClient("irc.twitch.tv", 6667);
42 reader = new StreamReader(tcpClient.GetStream());
43 writer = new StreamWriter(tcpClient.GetStream());
44
45 writer.WriteLine("PASS " + password);
46 writer.WriteLine("NICK " + userName);
47 writer.WriteLine("CAP REQ :twitch.tv/membership");
48 writer.WriteLine("JOIN #" + channelName);
49 writer.Flush();
50 lastMessage = DateTime.Now;
51 }
52 // Update is called once per frame
53 void Update () {
54 if (!tcpClient.Connected)
55 {
56 Recoonect();
57 }
58 TrySendingMessages();
59 TryReceiveMessages();
60 if (Input.GetKey(KeyCode.P))
61 {
62 Recoonect();
63 }
64 }
65
66 private void TryReceiveMessages()
67 {
68 if(tcpClient.Available > 0)
69 {
70 var message = reader.ReadLine();
71 //print(String.Format("\r\n{0}", message));
72 var iCollon = message.IndexOf(":", 1);
73 if (iCollon > 0)
74 {
75 var command = message.Substring(1, iCollon);
76 if (command.Contains(chatCommandId))
77 {
78 var iBang = command.IndexOf("!");
79 if (iBang > 0)
80 {
81 var speaker = command.Substring(0, iBang);
82 var chatMessage = message.Substring(iCollon + 1);
83
84 TryReceiveMessages(speaker, chatMessage);
85 }
86 }
87 }
88 }
89 }
90
91 private void TryReceiveMessages(string speaker, string chatMessage)
92 {
93 print(String.Format("\r\n{0}: {1}", speaker, chatMessage));
94 /*if (chatMessage.ToLower().Equals("!lick"))
95 {
96 sendTwitchMessage(String.Format("{0} requested to be licked! rooLick {0} rooLick2", speaker));
97
98 }*/
99 if (chatMessage.ToLower().StartsWith("!love"))
100 {
101 timedQueue.Enqueue(String.Format("rooLove {0} rooLove", speaker));
102 StartCoroutine(emptyTimedQueue());
103 }
104 else
105 if (chatMessage.ToLower().Contains("!lick") && speaker.ToLower().Equals("lucthesloth"))
106 {
107 timedQueue.Enqueue(String.Format("rooLick {0} rooLick2", chatMessage.Split(new string[] { "!lick" }, System.StringSplitOptions.None)[1]));
108 StartCoroutine(emptyTimedQueue());
109 } else if (chatMessage.ToLower().Contains("!quit") && speaker.ToLower().Equals("lucthesloth"))
110 {
111 Application.Quit();
112 } else if (chatMessage.ToLower().StartsWith("!") && speaker.ToLower().Equals("lucthesloth"))
113 {
114 timedQueue.Enqueue(String.Format("{0}{0}{0}", chatMessage.Replace('!', ' ')));
115 StartCoroutine(emptyTimedQueue());
116 }
117 }
118
119 private void sendTwitchMessage(string v)
120 {
121 sendMessageQueue.Enqueue(v);
122 }
123 IEnumerator emptyTimedQueue()
124 {
125 if (timedQueue.Count > 0)
126 {
127 yield return new WaitForSeconds(1f);
128 sendTwitchMessage(timedQueue.Dequeue());
129 }
130 }
131 private void TrySendingMessages()
132 {
133 if (DateTime.Now - lastMessage > TimeSpan.FromSeconds(2))
134 {
135 if(sendMessageQueue.Count > 0)
136 {
137 var message = sendMessageQueue.Dequeue();
138 writer.WriteLine(String.Format("{0}{1}", chatMessagePrefix, message));
139 writer.Flush();
140 lastMessage = DateTime.Now;
141 print(String.Format("{0}{1}", chatMessagePrefix, message));
142 }
143 }
144 }
145
146}