Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Threading;
- using System.Text.RegularExpressions;
- using System.Runtime.InteropServices;
- using System.Media;
- using System.Diagnostics;
- namespace SuomiPelaajaaBot
- {
- public partial class PelaajaaBot : Form
- {
- private static string userName = "suomipelaajaa";
- private static string botName = "PelaajaaBot";
- private static string password = "oauth:skrzedgrot0zumyndrdpwufwe5lsyl";
- List<string> BannedWords = new List<string> { "" };
- List<string> Mods = new List<string>();
- IniFile KultaIni = new IniFile(@"S:\Koodaus\Koodaus\C#\SuomiPelaajaaBot\SuomiPelaajaaBot\Kulta.ini");
- IrcClient irc = new IrcClient("irc.chat.twitch.tv", 6667, userName, password);
- NetworkStream serverStream = default(NetworkStream);
- string readData = "";
- Thread chatThread;
- public PelaajaaBot()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- irc.joinRoom(userName);
- chatThread = new Thread(getMessage);
- chatThread.Start();
- userName = Bot.Default.Channelname;
- botName = Bot.Default.Botname;
- password = Bot.Default.Oauth;
- ChannelName.Text = userName;
- BotName.Text = botName;
- Oauth.Text = password;
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- irc.leaveRoom();
- serverStream.Dispose();
- Environment.Exit(0);
- }
- /*public void Update()
- {
- userName = ChannelName.Text;
- botName = BotName.Text;
- password = Oauth.Text;
- Bot.Default.Channelname = ChannelName.Text;
- Bot.Default.Botname = BotName.Text;
- Bot.Default.Oauth = Oauth.Text;
- Bot.Default.Save();
- }*/
- private void getMessage()
- {
- serverStream = irc.tcpClient.GetStream();
- int buffsize = 0;
- byte[] inStream = new byte[10025];
- buffsize = irc.tcpClient.ReceiveBufferSize;
- while (true)
- {
- try
- {
- readData = irc.readMessage();
- msg();
- }
- catch (Exception e)
- {
- }
- }
- }
- private void msg()
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(msg));
- }
- else
- {
- string[] separator = new string[] { "#" + userName + " :" };
- string[] singlesep = new string[] { ":", "!" };
- if (readData.Contains("PRIVMSG"))
- {
- string username = readData.Split(singlesep, StringSplitOptions.None)[1];
- string message = readData.Split(separator, StringSplitOptions.None)[1];
- if (BannedWordFilter(username, message)) return;
- ChatBox.Text = ChatBox.Text + username + ": " + message + Environment.NewLine;
- }
- }
- }
- private void commands(string username, string message)
- {
- string command = message.Split(new[] { ' ', '!' }, StringSplitOptions.None)[1];
- switch (command.ToLower())
- {
- case "ping":
- irc.sendChatMessage("PONG");
- break;
- case "kulta add":
- if (username == "SuomiPelaajaa" || username == Mods.ToString())
- {
- string recipent = message.Split(new string[] { " " }, StringSplitOptions.None)[1];
- if (recipent[0] == '@')
- {
- recipent = recipent.Split(new[] { '@' }, StringSplitOptions.None)[1];
- }
- string pointstotransferststring = message.Split(new string[] { " " }, StringSplitOptions.None)[2];
- double pointstotransfer = 0;
- bool validnumber = double.TryParse(pointstotransferststring.Split(new[] { ' ' }, StringSplitOptions.None)[0], out pointstotransfer);
- if (!validnumber) break;
- if (pointstotransfer > 0)
- {
- Kulta(recipent, pointstotransfer);
- irc.sendChatMessage("Kulta added to " + username + " succesful");
- }
- }
- break;
- case "kulta":
- string yourpoints = KultaIni.IniReadValue("@" + userName + "." + username, "Points");
- if (yourpoints == "")
- {
- yourpoints = "20";
- Kulta(username, double.Parse(yourpoints));
- }
- else
- {
- double thepoints = double.Parse(yourpoints);
- }
- irc.sendChatMessage(username + " has " + yourpoints + " kultaa");
- break;
- case "mod":
- if (username == "SuomiPelaajaa")
- {
- Mods.Add(username);
- irc.sendChatMessage(username + "succesfully added mods");
- }
- break;
- case "time":
- irc.sendChatMessage("Current time on SuomiPelaajaa: " + DateTime.Now.ToString("h:mm:ss tt"));
- break;
- }
- }
- private bool BannedWordFilter(string username, string message)
- {
- foreach (string word in BannedWords)
- {
- if (message.Contains(word))
- {
- string command = "/timeout " + username + " 10";
- irc.sendChatMessage(command);
- irc.sendChatMessage(username + " has been timed out for 10 seconds");
- return true;
- }
- }
- return false;
- }
- private void Kulta(string username, double points)
- {
- double finalnumber = 0;
- try
- {
- string[] separator = new string[] { @"\r\n" };
- username = username.Trim().ToLower();
- string pointsofuser = KultaIni.IniReadValue("#" + userName + "." + username, "Points");
- double numberofpoints = double.Parse(pointsofuser);
- finalnumber = Convert.ToDouble(numberofpoints + points);
- if (finalnumber > 0)
- {
- KultaIni.IniWriteValue("#" + userName + "." + username, "Points", finalnumber.ToString());
- }
- if (finalnumber <= 0)
- {
- KultaIni.IniWriteValue("#" + userName + "." + username, "Points", "0");
- }
- }
- catch(Exception e)
- {
- if (finalnumber > 0)
- {
- KultaIni.IniWriteValue("#" + userName + "." + username, "Points", points.ToString());
- }
- }
- }
- private void OauthTXT_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
- {
- System.Diagnostics.Process.Start("https://twitchapps.com/tmi/");
- }
- }
- public class IrcClient
- {
- private string userName;
- private string channel;
- public TcpClient tcpClient;
- private StreamReader inputStream;
- private StreamWriter outputStream;
- public IrcClient(string ip, int port, string userName, string password)
- {
- tcpClient = new TcpClient(ip, port);
- inputStream = new StreamReader(tcpClient.GetStream());
- outputStream = new StreamWriter(tcpClient.GetStream());
- outputStream.WriteLine("PASS " + password);
- outputStream.WriteLine("NICK " + userName);
- outputStream.WriteLine("USER " + userName + " 8 :" + userName);
- outputStream.WriteLine("CAP REQ :twitch.tv/membership");
- outputStream.WriteLine("CAP REQ :twitch.tv/commands");
- outputStream.Flush();
- }
- public void joinRoom(string channel)
- {
- this.channel = channel;
- outputStream.WriteLine("JOIN #" + channel);
- outputStream.Flush();
- }
- public void leaveRoom()
- {
- outputStream.Close();
- inputStream.Close();
- }
- 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);
- }
- public void PingResponse()
- {
- sendIrcMessage("PONG tmi.twitch.tv\r\n");
- }
- public string readMessage()
- {
- string message = "";
- message = inputStream.ReadLine();
- return message;
- }
- }
- public class IniFile
- {
- public string path;
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
- [DllImport("kernel32")]
- private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
- public IniFile(string IniPath)
- {
- path = IniPath;
- }
- public void IniWriteValue(string Section, string Key, string value)
- {
- WritePrivateProfileString(Section, Key, value, this.path);
- }
- public string IniReadValue(string Section, string Key)
- {
- StringBuilder temp = new StringBuilder(255);
- GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
- return temp.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement