Advertisement
Guest User

bot

a guest
Jan 30th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. # Created by Rory Brown / "rory660"
  2. # www.twitch.tv/rory660
  3. # Do not re-share
  4. # This version edited by Levi is for personal use only!
  5. # Rory said i can use it
  6. # -Initialisation-
  7.  
  8. # Module Imports
  9.  
  10. import socket
  11. import re
  12. import time
  13.  
  14. #Variable Initialisation
  15.  
  16. HOST = "irc.twitch.tv" # Twitch Address.
  17. PORT = 6667 # Twitch IRC Port.
  18. NICK = "" # Displayed as "[Bot Name]"
  19. PASS = "oauth:_______________" # Displayed as "oauth:[Auth Characters]"
  20. CHAN = "#rory660" # Displayed as "#[Channel Name]"
  21. DELAY = time.time() # For us in limiting the command output of the bot.
  22. CMD = 0 # Boolean variable that dictates whether or not the message is a command.
  23. CMDID = 0 # ID assigned to a sent command to identify what command it is.
  24. USERNAME = ("Levi_Priestley") # Username of account for the bot to use.
  25. CMDSTRING = ("") # String to be printed to identify to the bot operater if a command is used.
  26. CMDDEF = ("") # String to be printed to identify to the bot operater what command is used.
  27. LIST = "!commands, !duck" # Command list variable
  28.  
  29. # Twitch Connection
  30.  
  31. s = socket.socket()
  32. s.connect((HOST, PORT))
  33. s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
  34. s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
  35. s.send("JOIN {}\r\n".format(CHAN).encode("utf-8"))
  36.  
  37. # -Chat Message Handling-
  38.  
  39. # Distinction between chat message and ping request is made.
  40. # The bot automatically responds to a ping request before a chat message.
  41. # Chat messages are also compiled into a username and message.
  42.  
  43. while True:
  44. RESPONSE = s.recv(1024).decode("utf-8")
  45. if RESPONSE == "PING :tmi.twitch.tv\r\n":
  46. s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
  47. print ("duck")
  48. else:
  49. CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
  50. USERNAME = re.search(r"\w+", RESPONSE).group(0)
  51. MESSAGE = CHAT_MSG.sub("", RESPONSE)
  52.  
  53. # Time delay variables used in ensuring the bot doesn't post messages too quickly.
  54. # The bot musn't post more than 20 chat messages in 30 seconds, or it may be blocked from the Twitch IRC server.
  55. # This bot is set to send 10 messages in 30 seconds for safety.
  56.  
  57. REALTIME = time.time()
  58. TIMEGAP = REALTIME-DELAY
  59.  
  60. # -Chat Commands-
  61.  
  62. # Checks if message is a command, and if so, what command it is.
  63.  
  64. if TIMEGAP >=3:
  65. if MESSAGE.startswith("!"):
  66. CMD = 1
  67. if "!commands" in MESSAGE or "!commandlist" in MESSAGE:
  68. CMDID = 1
  69. if "!quack" in MESSAGE or "!duck" in MESSAGE:
  70. CMDID = 2
  71.  
  72. # Format:
  73. # if "![command]" in MESSAGE or "![synonymous command name]" in MESSAGE:
  74. # CMDID = [Next Unused Number]
  75.  
  76.  
  77. # Command IDs:
  78. #1 - Commands
  79. #2 - Duck
  80.  
  81. # Command Funtionality
  82.  
  83. if CMD == 1 and CMDID == 1:
  84. s.send("PRIVMSG {0} :Command List: {1}\r\n".format(CHAN, LIST).encode("utf-8"))
  85. DELAY = time.time()
  86.  
  87. if CMD == 1 and CMDID == 2:
  88. s.send("PRIVMSG {} :/me has coded something\r\n".format(CHAN).encode("utf-8"))
  89. DELAY = time.time()
  90.  
  91. # Console Output Handling
  92.  
  93. if CMD == 1:
  94. CMDSTRING =(": !")
  95.  
  96. if CMDID == 0:
  97. CMDDEF=("Error")
  98. if CMDID == 1:
  99. CMDDEF = ("Commands")
  100. if CMDID == 2:
  101. CMDDEF = ("Duck")
  102.  
  103. if CMD == 0:
  104. CMDSTRING =("")
  105. CMDDEF = ("")
  106.  
  107. if not USERNAME == "tmi":
  108. print(USERNAME + CMDSTRING + CMDDEF)
  109.  
  110. # -Post Loop Iteration Variable Reinitialisation-
  111.  
  112. CMD = 0
  113. CMDID = 0
  114. USERNAME = ("")
  115. CMDSTRING = ("")
  116. CMDDEF = ("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement