Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nicetrysean committed Jul 3, 2016
1 parent 88cde0d commit 6b8c9d0
Show file tree
Hide file tree
Showing 10 changed files with 673 additions and 0 deletions.
22 changes: 22 additions & 0 deletions TwitchBot.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwitchBot", "TwitchBot\TwitchBot.csproj", "{829B27BB-019E-4F22-A25B-0B1E494548A4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{829B27BB-019E-4F22-A25B-0B1E494548A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{829B27BB-019E-4F22-A25B-0B1E494548A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{829B27BB-019E-4F22-A25B-0B1E494548A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{829B27BB-019E-4F22-A25B-0B1E494548A4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions TwitchBot/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
112 changes: 112 additions & 0 deletions TwitchBot/ConfigurationReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;

namespace TwitchBot
{
internal class ConfigurationReader
{
public class Credentials
{
public string Username = "username";
public string Password = "oauth:xxxxx";
public string Channel = "#channel";
}

public class Item
{
public string Name;
public string Command;
public string Action;
public string Text;
public string MisfireText;
public int VotesRequired;
public string FileName;
public double Cooldown;
}

public List<Item> Items;
public Credentials User;
public Dictionary<string, string[]> Texts;

public ConfigurationReader(string credentials, string items, string messages)
{
if (!File.Exists(credentials))
{
Console.WriteLine("No credentials found! Created credentials.json for you to fill in.");
using (StreamWriter r = new StreamWriter(credentials))
{
r.Write(JsonConvert.SerializeObject(new Credentials(), Formatting.Indented));
}
return;
}

if (!File.Exists(items))
{
Console.WriteLine("No Commands found! Created commands.json");
Items = new List<Item>(1) {new Item {Action = "Text", Text = "This is a live demo!", Command = "!demo", Cooldown = 0.1, Name = "Demo Text"} };
using (StreamWriter r = new StreamWriter(items))
{
r.Write(JsonConvert.SerializeObject(Items, Formatting.Indented));
}
}

if (!File.Exists(messages))
{
Console.WriteLine("No Messages found! Created messages.json");
Texts = new Dictionary<string, string[]>(1) {{"Demo", new[] {"Write", "Stuff", "Here"}}};
using (StreamWriter r = new StreamWriter(messages))
{
r.Write(JsonConvert.SerializeObject(Texts, Formatting.Indented));
}
}

LoadItems(items);
LoadTextMessages(messages);
LoadCredentials(credentials);
}

public void LoadItems(string filePath)
{
if (!File.Exists(filePath))
{
Console.WriteLine("No commands! Make sure commands.json is in the directory.");
return;
}
using (StreamReader r = new StreamReader(filePath))
{
var json = r.ReadToEnd();
Items = JsonConvert.DeserializeObject<List<Item>>(json);
}
}

public void LoadTextMessages(string filePath)
{
if (!File.Exists(filePath))
{
Console.WriteLine("No messages! Make sure messages.json is in the directory.");
return;
}
using (StreamReader r = new StreamReader(filePath))
{
var json = r.ReadToEnd();
Texts = JsonConvert.DeserializeObject<Dictionary<string,string[]>>(json);
}
}

public void LoadCredentials(string filePath)
{
if (!File.Exists(filePath))
{
Console.WriteLine("No credentials! Make sure credentials.json is in the directory.");
return;
}
using (StreamReader r = new StreamReader(filePath))
{
var json = r.ReadToEnd();
User = JsonConvert.DeserializeObject<Credentials>(json);
}
}
}
}
68 changes: 68 additions & 0 deletions TwitchBot/IrcClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.IO;
using System.Net.Sockets;

namespace TwitchBot
{
internal class IrcClient
{
private readonly string _username;
private string _channel;

private string _lastUsedIp;
private int _lastUsedPort;

private StreamReader _inputStream;
private StreamWriter _outputStream;
private TcpClient _tcpClient;

public bool Connected => _tcpClient.Connected;

public IrcClient(string ip, int port, string username, string password)
{
_username = username;
Connect(ip, port);
_outputStream.WriteLine("PASS " + password);
_outputStream.WriteLine("NICK " + username);
_outputStream.WriteLine("USER {0} 8 * :{0}", username);
_outputStream.Flush();
}

private void Connect(string ip, int port)
{
_lastUsedIp = ip;
_lastUsedPort = port;
_tcpClient = new TcpClient(ip, port);
_inputStream = new StreamReader(_tcpClient.GetStream());
_outputStream = new StreamWriter(_tcpClient.GetStream());
}

public void JoinRoom(string channel)
{
_channel = channel;
_outputStream.WriteLine("JOIN " + channel);
_outputStream.Flush();
}

public void SendIrcMessage(string message)
{
_outputStream.WriteLine(message);
_outputStream.Flush();
}

public void SendChatMessage(string message)
{
SendIrcMessage(string.Format(":{0}!{0}@{0}.tmi.twitch.tv PRIVMSG {1} :{2}", _username, _channel, message));
}

public void Reconnect()
{
_tcpClient.Connect(_lastUsedIp, _lastUsedPort);
}

public string ReadMessage()
{
return _inputStream.ReadLine();
}
}
}
Binary file added TwitchBot/Newtonsoft.Json.dll
Binary file not shown.
58 changes: 58 additions & 0 deletions TwitchBot/Parser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace TwitchBot
{
internal static class Parser
{
public static Dictionary<string, ShuffleBag<string>> Generations = new Dictionary<string, ShuffleBag<string>>();

private static readonly Regex VariableRegex = new Regex(@"^(#)([a-zA-Z\d\-]+)(#)");

private static readonly Regex TextRegex = new Regex(@"^[\s\S]+?(?=[#]|$)");

public static string Parse(string src, params KeyValuePair<string, string>[] values)
{
string result = "";
while (src.Length > 0)
{
Match cap;

//Search Variables
if ((cap = VariableRegex.Match(src)).Success)
{
ShuffleBag<string> captureResult;
if (Generations.TryGetValue(cap.Groups[2].Value, out captureResult))
{
result += Parse(captureResult.Next());
}
else
{
//Insert variables
for (int i = 0; i < values.Length; i++)
{
var value = values[i];
if (value.Key == cap.Groups[2].Value)
{
result += value.Value;
}
}
}

src = src.Substring(cap.Groups[0].Length);
}

//Search Text
if (!(cap = TextRegex.Match(src)).Success) continue;
result += cap.Groups[0].Value;
src = src.Substring(cap.Groups[0].Length);
}

return result;
}
}
}
Loading

0 comments on commit 6b8c9d0

Please sign in to comment.