Advertisement
Guest User

Problem

a guest
Feb 13th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.IO;
  13. using System.Threading;
  14. using System.Text.RegularExpressions;
  15. using System.Runtime.InteropServices;
  16. using System.Media;
  17. using System.Diagnostics;
  18.  
  19. namespace SuomiPelaajaaBot
  20. {
  21. public partial class PelaajaaBot : Form
  22. {
  23. private static string userName = "suomipelaajaa";
  24. private static string botName = "PelaajaaBot";
  25. private static string password = "oauth:skrzedgrot0zumyndrdpwufwe5lsyl";
  26. List<string> BannedWords = new List<string> { "" };
  27. List<string> Mods = new List<string>();
  28. IniFile KultaIni = new IniFile(@"S:\Koodaus\Koodaus\C#\SuomiPelaajaaBot\SuomiPelaajaaBot\Kulta.ini");
  29. IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
  30. NetworkStream serverStream = default(NetworkStream);
  31. string readData = "";
  32. Thread chatThread;
  33.  
  34. public PelaajaaBot()
  35. {
  36. InitializeComponent();
  37. }
  38.  
  39. private void Form1_Load(object sender, EventArgs e)
  40. {
  41. irc.joinRoom(userName);
  42. chatThread = new Thread(getMessage);
  43. chatThread.Start();
  44. userName = Bot.Default.Channelname;
  45. botName = Bot.Default.Botname;
  46. password = Bot.Default.Oauth;
  47. ChannelName.Text = userName;
  48. BotName.Text = botName;
  49. Oauth.Text = password;
  50. }
  51.  
  52. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  53. {
  54. irc.leaveRoom();
  55. serverStream.Dispose();
  56. Environment.Exit(0);
  57. }
  58.  
  59. /*public void Update()
  60. {
  61. userName = ChannelName.Text;
  62. botName = BotName.Text;
  63. password = Oauth.Text;
  64. Bot.Default.Channelname = ChannelName.Text;
  65. Bot.Default.Botname = BotName.Text;
  66. Bot.Default.Oauth = Oauth.Text;
  67. Bot.Default.Save();
  68. }*/
  69.  
  70. private void getMessage()
  71. {
  72. serverStream = irc.tcpClient.GetStream();
  73. int buffsize = 0;
  74. byte[] inStream = new byte[10025];
  75. buffsize = irc.tcpClient.ReceiveBufferSize;
  76. while (true)
  77. {
  78. try
  79. {
  80. readData = irc.readMessage();
  81. msg();
  82. }
  83. catch (Exception e)
  84. {
  85.  
  86. }
  87. }
  88. }
  89.  
  90. private void msg()
  91. {
  92. if (this.InvokeRequired)
  93. {
  94. this.Invoke(new MethodInvoker(msg));
  95. }
  96. else
  97. {
  98. string[] separator = new string[] { "#" + userName + " :" };
  99. string[] singlesep = new string[] { ":", "!" };
  100. if (readData.Contains("PRIVMSG"))
  101. {
  102. string username = readData.Split(singlesep, StringSplitOptions.None)[1];
  103. string message = readData.Split(separator, StringSplitOptions.None)[1];
  104. if (BannedWordFilter(username, message)) return;
  105. ChatBox.Text = ChatBox.Text + username + ": " + message + Environment.NewLine;
  106. }
  107. }
  108. }
  109.  
  110. private void commands(string username, string message)
  111. {
  112. string command = message.Split(new[] { ' ', '!' }, StringSplitOptions.None)[1];
  113.  
  114. switch (command.ToLower())
  115. {
  116. case "ping":
  117. irc.sendChatMessage("PONG");
  118. break;
  119. case "kulta add":
  120. if (username == "SuomiPelaajaa" || username == Mods.ToString())
  121. {
  122. string recipent = message.Split(new string[] { " " }, StringSplitOptions.None)[1];
  123. if (recipent[0] == '@')
  124. {
  125. recipent = recipent.Split(new[] { '@' }, StringSplitOptions.None)[1];
  126. }
  127. string pointstotransferststring = message.Split(new string[] { " " }, StringSplitOptions.None)[2];
  128. double pointstotransfer = 0;
  129. bool validnumber = double.TryParse(pointstotransferststring.Split(new[] { ' ' }, StringSplitOptions.None)[0], out pointstotransfer);
  130. if (!validnumber) break;
  131. if (pointstotransfer > 0)
  132. {
  133. Kulta(recipent, pointstotransfer);
  134. irc.sendChatMessage("Kulta added to " + username + " succesful");
  135. }
  136. }
  137. break;
  138. case "kulta":
  139. string yourpoints = KultaIni.IniReadValue("@" + userName + "." + username, "Points");
  140. if (yourpoints == "")
  141. {
  142. yourpoints = "20";
  143. Kulta(username, double.Parse(yourpoints));
  144. }
  145. else
  146. {
  147. double thepoints = double.Parse(yourpoints);
  148. }
  149. irc.sendChatMessage(username + " has " + yourpoints + " kultaa");
  150. break;
  151. case "mod":
  152. if (username == "SuomiPelaajaa")
  153. {
  154. Mods.Add(username);
  155. irc.sendChatMessage(username + "succesfully added mods");
  156. }
  157. break;
  158. case "time":
  159. irc.sendChatMessage("Current time on SuomiPelaajaa: " + DateTime.Now.ToString("h:mm:ss tt"));
  160. break;
  161. }
  162. }
  163.  
  164. private bool BannedWordFilter(string username, string message)
  165. {
  166. foreach (string word in BannedWords)
  167. {
  168. if (message.Contains(word))
  169. {
  170. string command = "/timeout " + username + " 10";
  171. irc.sendChatMessage(command);
  172. irc.sendChatMessage(username + " has been timed out for 10 seconds");
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178.  
  179. private void Kulta(string username, double points)
  180. {
  181. double finalnumber = 0;
  182. try
  183. {
  184. string[] separator = new string[] { @"\r\n" };
  185. username = username.Trim().ToLower();
  186. string pointsofuser = KultaIni.IniReadValue("#" + userName + "." + username, "Points");
  187. double numberofpoints = double.Parse(pointsofuser);
  188. finalnumber = Convert.ToDouble(numberofpoints + points);
  189. if (finalnumber > 0)
  190. {
  191. KultaIni.IniWriteValue("#" + userName + "." + username, "Points", finalnumber.ToString());
  192. }
  193. if (finalnumber <= 0)
  194. {
  195. KultaIni.IniWriteValue("#" + userName + "." + username, "Points", "0");
  196. }
  197. }
  198. catch(Exception e)
  199. {
  200. if (finalnumber > 0)
  201. {
  202. KultaIni.IniWriteValue("#" + userName + "." + username, "Points", points.ToString());
  203. }
  204. }
  205. }
  206.  
  207. private void OauthTXT_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
  208. {
  209. System.Diagnostics.Process.Start("https://twitchapps.com/tmi/");
  210. }
  211. }
  212.  
  213. public class IrcClient
  214. {
  215. private string userName;
  216. private string channel;
  217. public TcpClient tcpClient;
  218. private StreamReader inputStream;
  219. private StreamWriter outputStream;
  220.  
  221. public IrcClient(string ip, int port, string userName, string password)
  222. {
  223. tcpClient = new TcpClient(ip, port);
  224. inputStream = new StreamReader(tcpClient.GetStream());
  225. outputStream = new StreamWriter(tcpClient.GetStream());
  226.  
  227. outputStream.WriteLine("PASS " + password);
  228. outputStream.WriteLine("NICK " + userName);
  229. outputStream.WriteLine("USER " + userName + " 8 :" + userName);
  230. outputStream.WriteLine("CAP REQ :twitch.tv/membership");
  231. outputStream.WriteLine("CAP REQ :twitch.tv/commands");
  232. outputStream.Flush();
  233. }
  234.  
  235. public void joinRoom(string channel)
  236. {
  237. this.channel = channel;
  238. outputStream.WriteLine("JOIN #" + channel);
  239. outputStream.Flush();
  240. }
  241.  
  242. public void leaveRoom()
  243. {
  244. outputStream.Close();
  245. inputStream.Close();
  246. }
  247.  
  248. public void sendIrcMessage(string message)
  249. {
  250. outputStream.WriteLine(message);
  251. outputStream.Flush();
  252. }
  253.  
  254. public void sendChatMessage(string message)
  255. {
  256. sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :" + message);
  257. }
  258.  
  259. public void PingResponse()
  260. {
  261. sendIrcMessage("PONG tmi.twitch.tv\r\n");
  262. }
  263.  
  264. public string readMessage()
  265. {
  266. string message = "";
  267. message = inputStream.ReadLine();
  268. return message;
  269. }
  270. }
  271.  
  272. public class IniFile
  273. {
  274. public string path;
  275. [DllImport("kernel32")]
  276. private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
  277. [DllImport("kernel32")]
  278. private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
  279.  
  280. public IniFile(string IniPath)
  281. {
  282. path = IniPath;
  283. }
  284.  
  285. public void IniWriteValue(string Section, string Key, string value)
  286. {
  287. WritePrivateProfileString(Section, Key, value, this.path);
  288. }
  289.  
  290. public string IniReadValue(string Section, string Key)
  291. {
  292. StringBuilder temp = new StringBuilder(255);
  293. GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
  294. return temp.ToString();
  295. }
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement