Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Network.hpp>
  3. #include <SFML/System/Utf.hpp>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <string>
  7.  
  8. sf::TcpSocket socket;
  9. sf::Socket::Status status;
  10. std::string nickname = "tutasbot";
  11. std::string oauth = "oauth:XXXXXXXXXXXXXX"; //you won't be stealing it now :P
  12. std::string channel = "coolkidscode";
  13. bool isRunning = true;
  14.  
  15. void sendMessage(std::string message)
  16. {
  17.     std::string totalMSG = ":" + nickname+"!"+nickname+"@"+nickname+".tmi.twitch.tv PRIVMSG #"+channel+" :" + message + "\r\n";
  18.     socket.send(totalMSG.c_str(),totalMSG.size()+1);
  19. }
  20.  
  21. void doMessage(std::string message,std::string username)
  22. {
  23.     std::cout<<"I hav message! \a"<<std::endl;
  24.     if(message.find("!help") == 0)
  25.     {
  26.         sendMessage("BOT TUTAS IS HERE TO HELP! <3 <3 <3");
  27.     }else if(message.find("!quit") == 0 && username.find("tutasmaster") != -1)
  28.     {
  29.         std::cout<<"BOT SHUTTING OFF"<<std::endl;
  30.         isRunning = false;
  31.     }else if(message.find("ACTION flips a table!") == 1)
  32.     {
  33.         sendMessage(username + " please stop flipping tables. This is not a crisis.");
  34.     }
  35. }
  36.  
  37. void messageFunc(std::string buffer){
  38.     std::string buf(buffer);
  39.     if (buf.find("PRIVMSG") != -1) {
  40.         //std::cout<<buf<<std::endl;
  41.         int point = 0;
  42.         int userpoint = buf.find("!");
  43.         int messagepoint = buf.find(":",2);
  44.         std::string username;
  45.         if(userpoint != -1){
  46.             username = buf;
  47.             username.erase(userpoint, buf.size() - userpoint);
  48.             username.erase(0,1);
  49.             std::cout<<username;
  50.             if(messagepoint != -1)
  51.             {
  52.                 std::string strmessage = buf.erase(0,messagepoint+1);
  53.                 std::cout<<":"<<strmessage<<std::endl;
  54.                 doMessage(strmessage,username);
  55.             }
  56.             //std::cout<<userpoint<<std::endl;
  57.             //std::cout<<messagepoint<<std::endl;
  58.         }
  59.     }else{
  60.         std::cout<<buffer<<"\a"<<std::endl;
  61.     }
  62.  
  63. }
  64.  
  65.  
  66. int main()
  67. {
  68.     status = socket.connect("irc.twitch.tv", 6667);
  69.     if (status == sf::Socket::Done)
  70.     {
  71.         // error...
  72.         std::cout<<"The socket is working."<<std::endl;
  73.     }
  74.  
  75.     std::string nick = "NICK "+nickname+"\r\n";
  76.     std::string pass = "PASS "+oauth+"\r\n";
  77.     std::string chan = "JOIN #"+channel+"\r\n";
  78.     std::string pong = "PONG :tmi.twitch.tv\r\n";
  79.     std::string capreq = "CAP REQ :twitch.tv/commands\r\n";
  80.     socket.send(pass.c_str(),  pass.size() + 1);
  81.     socket.send(nick.c_str(),  nick.size() + 1);
  82.     socket.send(chan.c_str(),  chan.size() + 1);
  83.     socket.send(capreq.c_str(),capreq.size() + 1);
  84.     sendMessage("The bot is on!");
  85.     while(isRunning)
  86.     {
  87.         char buffer[500];
  88.         std::size_t received = 0;
  89.         socket.receive(buffer, sizeof(buffer), received);
  90.         if(buffer == "PING :tmi.twitch.tv"){
  91.             socket.send(pong.c_str(),pong.size() + 1);
  92.             std::cout<<"I HAVE BEEN PINGED!"<<std::endl;
  93.         }else{
  94.             messageFunc(buffer);
  95.         }
  96.     }
  97.  
  98.  
  99.  
  100.     return EXIT_SUCCESS;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement