Simple Twitch IRC bot


SUBMITTED BY: Guest

DATE: Nov. 15, 2013, 12:59 a.m.

FORMAT: Text only

SIZE: 2.2 kB

HITS: 944

  1. ##Socket library
  2. import socket
  3. ##IRC connection data
  4. HOST="199.9.253.199" ##This is the Twitch IRC ip, don't change it.
  5. PORT=6667 ##Same with this port, leave it be.
  6. NICK="KanthBot" ##This has to be your bots username.
  7. PASS="testpass1" ##Instead of a password, use this http://twitchapps.com/tmi/, since Twitch is soon updating to it.
  8. IDENT="KanthBot" ##Bot username again
  9. REALNAME="Kanthes Bot" ##This doesn't really matter.
  10. CHANNEL="#kanthes" ##This is the channel your bot will be working on.
  11. s = socket.socket( ) ##Creating the socket variable
  12. s.connect((HOST, PORT)) ##Connecting to Twitch
  13. s.send("PASS %s\r\n" % PASS) ##Notice how I'm sending the password BEFORE the username!
  14. ##Just sending the rest of the data now.
  15. s.send("NICK %s\r\n" % NICK)
  16. s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
  17. ##Connecting to the channel.
  18. s.send("JOIN %s\r\n" % CHANNEL)
  19. readbuffer = ""
  20. ##Eternal loop letting the bot run.
  21. while (1):
  22. ##Receiving data from IRC and spitting it into manageable lines.
  23. readbuffer=readbuffer+nfSocket.recv(1024)
  24. temp=string.split(readbuffer, "\n")
  25. readbuffer=temp.pop( )
  26. for line in temp:
  27. ##Checks if the first character is a !, for commands.
  28. if(line[3][0:2]==":!"):
  29. QUERIED_COMMAND = ENTIRE_MESSAGE.split(" ",1)[0]
  30. ##Checks what command was queried.
  31. if(QUERIED_COMMAND=="!hello"):
  32. ##Sending a reply to the channel. Notice the : before the actual message, that's mandatory, as well as the \r\n to let it post the new line.
  33. reply ="PRIVMSG "+CHANNEL+" :Hello world!\r\n"
  34. ##Sending the reply through the socket
  35. s.send(reply)
  36. ##IRC checks connectiond with ping. Every ping has to be replied to with a Pong.
  37. elif(line[0]=="PING"):
  38. s.send("PONG %s\r\n" % line[1])
  39. ##Disclaimer:
  40. ##This is a VERY simple bot I've taken straight from my code, I've not tried running it nor can I guarantee it will. But perhaps it will give you some idea of the syntax to use and all that!

comments powered by Disqus