Created
May 22, 2021 01:19
-
-
Save RC0D3/68e2197bf4467117fe48118b2f0ee80d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
//using System; | |
using System.Threading.Tasks; | |
public class Test : MonoBehaviour | |
{ | |
public GameObject playerInstance; | |
public string streamerChat = "FILL"; | |
async void Main() | |
{ | |
string password = "oauth:FILLLLLLLLL"; | |
string botUsername = "simple_irc_bot"; | |
var twitchBot = new Twitch(botUsername, password); | |
twitchBot.Start(); | |
await twitchBot.JoinChannel(streamerChat); | |
twitchBot.OnMessage += async (sender, twitchChatMessage) => | |
{ | |
Debug.Log($"{twitchChatMessage.Sender} said '{twitchChatMessage.Message}'"); | |
//Listen for !hey command | |
if (twitchChatMessage.Message.StartsWith("!hey")) | |
{ | |
await twitchBot.SendMessage(twitchChatMessage.Channel, $"{twitchChatMessage.Sender} é sadboy falando com bot kakaka"); | |
} | |
}; | |
//await Task.Delay(-1); | |
} | |
private void Start() | |
{ | |
Main(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
using System.IO; | |
using System.Net.Sockets; | |
using System.Threading.Tasks; | |
public class Twitch | |
{ | |
const string ip = "irc.chat.twitch.tv"; | |
const int port = 6667; | |
private string nick; | |
private string password; | |
private StreamReader streamReader; | |
private StreamWriter streamWriter; | |
private TaskCompletionSource<int> connected = new TaskCompletionSource<int>(); | |
public event TwitchChatEventHandler OnMessage = delegate { }; | |
public delegate void TwitchChatEventHandler(object sender, TwitchChatMessage e); | |
public class TwitchChatMessage : EventArgs | |
{ | |
public string Sender { get; set; } | |
public string Message { get; set; } | |
public string Channel { get; set; } | |
} | |
public Twitch(string nick, string password) | |
{ | |
this.nick = nick; | |
this.password = password; | |
} | |
public async Task Start() | |
{ | |
var tcpClient = new TcpClient(); | |
await tcpClient.ConnectAsync(ip, port); | |
streamReader = new StreamReader(tcpClient.GetStream()); | |
streamWriter = new StreamWriter(tcpClient.GetStream()) { NewLine = "\r\n", AutoFlush = true }; | |
await streamWriter.WriteLineAsync($"PASS {password}"); | |
await streamWriter.WriteLineAsync($"NICK {nick}"); | |
connected.SetResult(0); | |
while (true) | |
{ | |
string line = await streamReader.ReadLineAsync(); | |
Console.WriteLine(line); | |
string[] split = line.Split(' '); | |
//PING :tmi.twitch.tv | |
//Respond with PONG :tmi.twitch.tv | |
if (line.StartsWith("PING")) | |
{ | |
Console.WriteLine("PONG"); | |
await streamWriter.WriteLineAsync($"PONG {split[1]}"); | |
} | |
if (split.Length > 2 && split[1] == "PRIVMSG") | |
{ | |
//:mytwitchchannel!mytwitchchannel@mytwitchchannel.tmi.twitch.tv | |
// ^^^^^^^^ | |
//Grab this name here | |
int exclamationPointPosition = split[0].IndexOf("!"); | |
string username = split[0].Substring(1, exclamationPointPosition - 1); | |
//Skip the first character, the first colon, then find the next colon | |
int secondColonPosition = line.IndexOf(':', 1);//the 1 here is what skips the first character | |
string message = line.Substring(secondColonPosition + 1);//Everything past the second colon | |
string channel = split[2].TrimStart('#'); | |
OnMessage(this, new TwitchChatMessage | |
{ | |
Message = message, | |
Sender = username, | |
Channel = channel | |
}); | |
} | |
} | |
} | |
public async Task SendMessage(string channel, string message) | |
{ | |
await connected.Task; | |
await streamWriter.WriteLineAsync($"PRIVMSG #{channel} :{message}"); | |
} | |
public async Task JoinChannel(string channel) | |
{ | |
await connected.Task; | |
await streamWriter.WriteLineAsync($"JOIN #{channel}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment