2016-07-24 60 views
2

嘿,我想創建抽搐了聊天機器人,我是那種在這些事情是初學者的,這是我的工作至今:需要幫助的C#抽搐機器人

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

有一些庫可以幫助你做到這一點,所以你可以看到他們如何工作或直接使用它們。

例如上的NuGet最流行的似乎是:

TwitchLib - 抽搐聊天和API C#庫 - https://github.com/swiftyspiffy/TwitchLib