Guest User

simpletwitchbot

a guest
Oct 13th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. # This is just a basic command bot for Twitch chat.
  2.  
  3.  
  4. # Imports
  5. import socket
  6.  
  7.  
  8. # no need to change, always this
  9. SERVER = "irc.twitch.tv"
  10.  
  11. # no need to change, always this
  12. PORT = 6667
  13.  
  14. # change inbetween "quotes" your oath you get from https://twitchapps.com/tmi/
  15. PASS = "REPLACETHISWITHOATH:SOMERANDOMCHARACTERSINUROATH"
  16.  
  17. # channel in witch you want this bot to work
  18. CHANNEL = "lowercasechannelname"
  19.  
  20. # account name in lowercase which will be used as bot
  21. BOT = "lowercasenameofyourstandardchatbot"
  22.  
  23.  
  24. def sendMessage(s, message):
  25. messageTemp = "PRIVMSG #" + CHANNEL + " :" + message
  26. s.send((messageTemp + "\r\n").encode())
  27.  
  28. def getUser(line):
  29. separate = line.split(":", 2)
  30. user = separate[1].split("!", 1)[0]
  31. return user
  32.  
  33. def getMessage(line):
  34. global message
  35. try:
  36. message = (line.split(":", 2))[2]
  37. except:
  38. message = ""
  39. return message
  40.  
  41. def joinchat():
  42. readbuffer_join = "".encode()
  43. Loading = True
  44. while Loading:
  45. readbuffer_join = s.recv(1024)
  46. readbuffer_join = readbuffer_join.decode()
  47. temp = readbuffer_join.split("\n")
  48. readbuffer_join = readbuffer_join.encode()
  49. readbuffer_join = temp.pop()
  50. for line in temp:
  51. Loading = loadingCompleted(line)
  52. sendMessage(s, "Bot Entered Channel Message")
  53. print("Bot has joined " + CHANNEL + " Channel!")
  54.  
  55. def loadingCompleted(line):
  56. if ("End of /NAMES list" in line):
  57. return False
  58. else:
  59. return True
  60.  
  61. s_prep = socket.socket()
  62. s_prep.connect((SERVER, PORT))
  63. s_prep.send(("PASS " + PASS + "\r\n").encode())
  64. s_prep.send(("NICK " + BOT + "\r\n").encode())
  65. s_prep.send(("JOIN #" + CHANNEL + "\r\n").encode())
  66. s = s_prep
  67. joinchat()
  68. readbuffer = ""
  69.  
  70. def Console(line):
  71. if "PRIVMSG" in line:
  72. return False
  73. else:
  74. return True
  75.  
  76.  
  77. while True:
  78. try:
  79. readbuffer = s.recv(1024)
  80. readbuffer = readbuffer.decode()
  81. temp = readbuffer.split("\n")
  82. readbuffer = readbuffer.encode()
  83. readbuffer = temp.pop()
  84. except:
  85. temp = ""
  86. for line in temp:
  87. if line == "":
  88. break
  89. if "PING" in line and Console(line):
  90. msgg = "PONG tmi.twitch.tv\r\n".encode()
  91. s.send(msgg)
  92. print(msgg)
  93. break
  94. user = getUser(line)
  95. message = getMessage(line)
  96. PMSG = "/w " + user + " "
  97.  
  98. # Now the stuff to change. Just copy paste the if to break part as many times as you want to make multiple commands.
  99.  
  100. if "!command" in message:
  101. sendMessage(s, "First respond message.")
  102. sendMessage(s, "Second respond message.")
  103. break
  104.  
  105. if ".anothercommand" in message:
  106. sendMessage(s, "Your message.")
  107. break
Add Comment
Please, Sign In to add comment