Advertisement
bacon_donut

Twitch Chat Bot Tutorial

Jan 19th, 2017
2,831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Speech.Synthesis;
  10. using System.Windows.Forms;
  11. using TwitchLib;
  12. using TwitchLib.Models.API;
  13. using TwitchLib.Models.Client;
  14. using TwitchLib.Events.Client;
  15. using TwitchLib.Exceptions.API;
  16. using TwitchLib.Events.PubSub;
  17. using TwitchLib.Events.Services.FollowerService;
  18. using TwitchLib.Events.Services.MessageThrottler;
  19. using TwitchLib.Enums;
  20. using TwitchLib.Extensions.Client;
  21.  
  22. namespace ChatroomTool
  23. {
  24.     public partial class Form1 : Form
  25.     {
  26.         public TwitchClient client = new TwitchClient(new TwitchLib.Models.Client.ConnectionCredentials(Properties.Settings.Default.username, Properties.Settings.Default.oauth));
  27.  
  28.         System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer();
  29.  
  30.         SpeechSynthesizer synth = new SpeechSynthesizer();
  31.  
  32.         public Form1()
  33.         {
  34.             InitializeComponent();
  35.         }
  36.  
  37.         private void button1_Click(object sender, EventArgs e)
  38.         {
  39.             if (client.IsConnected)
  40.             {
  41.                 richChat.Text = richChat.Text + "\n" + txtChatBox.Text;
  42.  
  43.                 client.SendMessage(txtChatBox.Text);
  44.  
  45.                 txtChatBox.Text = "";
  46.                                
  47.             }
  48.             else
  49.             {
  50.                 richChat.Text = richChat.Text + "\n" + "<< Disconnected From Chat >>";
  51.             }
  52.  
  53.  
  54.            
  55.         }
  56.  
  57.         private void btnConnect_Click(object sender, EventArgs e)
  58.         {
  59.             client.OnMessageReceived += new EventHandler<OnMessageReceivedArgs>(globalChatMessageReceived);
  60.             client.OnConnected += new EventHandler<OnConnectedArgs>(onConnected);
  61.             client.OnDisconnected += new EventHandler<OnDisconnectedArgs>(onDisconnected);
  62.  
  63.             if (client.IsConnected == false)
  64.             {
  65.                 client.Connect();
  66.             }
  67.            
  68.  
  69.            
  70.         }
  71.  
  72.         public void onConnected(object sender, OnConnectedArgs e)
  73.         {
  74.             CheckForIllegalCrossThreadCalls = false;
  75.  
  76.             client.JoinChannel(txtChatroom.Text);
  77.  
  78.             richChat.Text = richChat.Text + "\n" + "<< Connected to chat server >>";
  79.  
  80.         }
  81.  
  82.         public void onDisconnected(object sender, OnDisconnectedArgs e)
  83.         {
  84.             //Don't do this in production
  85.             CheckForIllegalCrossThreadCalls = false;
  86.             //MessageBox.Show("disconnected");
  87.             richChat.Text = richChat.Text + "\n" + "<< Disconnected From Channel >>";
  88.         }
  89.  
  90.         private void globalChatMessageReceived(object sender, OnMessageReceivedArgs e)
  91.         {
  92.             CheckForIllegalCrossThreadCalls = false;
  93.  
  94.             if (e.ChatMessage.Username == "bacon_donut" & e.ChatMessage.Message.StartsWith("!hello "))
  95.             {
  96.                 //MessageBox.Show("bacon says hello");
  97.  
  98.                 //soundPlayer.Load();
  99.                 //soundPlayer.Play();
  100.  
  101.                 synth.Speak(e.ChatMessage.Message);
  102.  
  103.                 richChat.Text = richChat.Text + "\n" + e.ChatMessage.Message;
  104.             }
  105.             else if (e.ChatMessage.UserType== UserType.Moderator & e.ChatMessage.Message == "!modcommand")
  106.             {
  107.                 MessageBox.Show("this is a mod command");
  108.                 richChat.Text = richChat.Text + "\n" + e.ChatMessage.Message;
  109.             }
  110.             else
  111.             {
  112.                 richChat.Text = richChat.Text + "\n" + e.ChatMessage.Message;
  113.             }
  114.  
  115.  
  116.            
  117.         }
  118.  
  119.         private void btnDisconnect_Click(object sender, EventArgs e)
  120.         {            
  121.             client.LeaveChannel(txtChatroom.Text);
  122.             client.Disconnect();
  123.  
  124.             richChat.Text = richChat.Text + "\n" + "<< Disconnecting.... >>";
  125.         }
  126.  
  127.         private void Form1_Load(object sender, EventArgs e)
  128.         {
  129.             soundPlayer.SoundLocation = "c:\\sizzle.wav";
  130.  
  131.             synth.SetOutputToDefaultAudioDevice();
  132.         }
  133.  
  134.         private void btnSizzle_Click(object sender, EventArgs e)
  135.         {
  136.             soundPlayer.SoundLocation = "c:\\sizzle.wav";
  137.  
  138.             soundPlayer.Load();
  139.             soundPlayer.Play();
  140.         }
  141.  
  142.         private void btnBomb_Click(object sender, EventArgs e)
  143.         {
  144.             soundPlayer.SoundLocation = "c:\\bomb.wav";
  145.  
  146.             soundPlayer.Load();
  147.             soundPlayer.Play();
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement