Advertisement
Guest User

twitch bot pseudo code

a guest
Apr 16th, 2016
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. the basics way the bot works is:
  2.  
  3. define some variables in your source code that tell the bot where to connect to, what name to use, and the token that is going to authenticate it to twitch IRC (internet relay chat).
  4.  
  5. HOST = "irc.chat.twitch.tv"
  6. NICK = "enter nickname for your bot here"
  7. PORT = 6667
  8. PASS = "oauth:inserttokenfromtwitch"
  9.  
  10. the oathtoken is a crypto string that represents who you are without exposing your password
  11.  
  12. from there we write the code to open the socket and connect to twitch
  13.  
  14. s = socket.socket()
  15. s.connect((HOST, PORT))
  16. s.send("PASS " + PASS + "\r\n")
  17. s.send("NICK " + NICK + "\r\n")
  18. s.send("JOIN #streamersNameWhereYouWantTheBotToGo \r\n")
  19.  
  20.  
  21. we make a quick function that uses the socket library to send our messages & replies to twitch
  22.  
  23.  
  24. def Send_message(message):
  25. s.send("PRIVMSG #streamersNameWhereYouWantTheBotToGo :" + message + "\r\n")
  26.  
  27.  
  28. we create a loop and a buffer that constantly reads the chat info coming from twitch.
  29.  
  30. while True:
  31. readbuffer = readbuffer + s.recv(1024)
  32. temp = string.split(readbuffer, "\n")
  33. readbuffer = temp.pop()
  34.  
  35.  
  36. the next part is more involved, this part takes the messages coming in and cuts out portions like the username of the person who sent the message and the message they sent:
  37.  
  38.  
  39. parts = string.split(line, ":")
  40.  
  41. if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
  42. try:
  43.  
  44. message = parts[2][:len(parts[2]) - 1]
  45. except:
  46. message = ""
  47.  
  48. usernamesplit = string.split(parts[1], "!")
  49. username = usernamesplit[0]
  50.  
  51. we now have two variables that we can use to reference the username of the person sending a message, and a message variable for the content they are sending. The first part with QUIT JOIN PART are all IRC commmands that twitch sends to the bot to let it know what's going on in chat & on the server. These commands and more are documented in the twitch API: https://github.com/justintv/Twitch-API/blob/master/IRC.md
  52.  
  53. the next snippet is how the bot handles !commands
  54.  
  55. if ("!command" in message):
  56. do something
  57.  
  58. for example:
  59.  
  60. if ("!sayHello" in message):
  61. send_message("Hello " + username +"!")
  62.  
  63.  
  64. first, the variable "message" is checked for the string "!sayHello". If it exists in the current message it then calls the send_message function we created and sends the response into chat.
  65.  
  66. that's the basic framework for the bot, the next step would be to bolt on a small database like sqlite to store info on users, points, quotes, etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement