嘿我想为Twitch创建一个聊天机器人,我在这些事情上是初学者,这是我迄今为止的工作:
Form1.cs如下:
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;
namespace MyTwitchChat
{
public partial class Form1 : Form
{
private string username;
private string password;
bool joined;
IRCClient ircClient;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
username = userNameTB.Text;
password = TokenBox.Text;
ircClient = new IRCClient("irc.chat.twitch.tv", 6667, username, password);
ircClient.joinChannel("deadlycursed");
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (ircClient.tcpClient.Available > 0 )
{
string message = ircClient.inputStream.ReadLine();
ChatBox.Items.Add(message);
if (message.Contains("!Hallo"))
ircClient.sendChatMessage("Hi!");
}
}
}
}
IRCClient.cs如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace MyTwitchChat
{
class IRCClient
{
private string username;
private string channel;
private string nickname = "deadlycursed";
public TcpClient tcpClient;
public StreamReader inputStream;
public StreamWriter outputStream;
public IRCClient(string ip, int port, string username, string password)
{
this.username = username;
tcpClient = new TcpClient(ip, port);
inputStream = new StreamReader(tcpClient.GetStream());
outputStream = new StreamWriter(tcpClient.GetStream());
outputStream.WriteLine("PASS " + password);
outputStream.WriteLine("NICK " + nickname);
outputStream.WriteLine("USER " + username + " 0 * :" + nickname );
outputStream.Flush();
}
public void joinChannel(string channel)
{
this.channel = channel;
outputStream.WriteLine("JOIN #" + channel);
outputStream.Flush();
}
public void sendIRCMessage(string message)
{
outputStream.WriteLine(message);
outputStream.Flush();
}
public void sendChatMessage(string message)
{
outputStream.WriteLine(":" + nickname + "!" + username + "@" + nickname + ".tmi.twitch.tv PRIVMSG #"
+ channel + ":" + message);
outputStream.Flush();
}
public string getMessage()
{
string message = inputStream.ReadLine();
return message;
}
}
}
我想知道为什么我实际上无法发送消息或收到消息? 有时阅读的东西是有效的,有时候没有,我不能写我的机器人聊天
答案 0 :(得分:0)
有一些库可以帮助您完成此操作,因此您可以查看它们的工作原理或直接使用它们。
例如,NuGet上最受欢迎的似乎是:
TwitchLib - Twitch Chat和API C#Library - https://github.com/swiftyspiffy/TwitchLib