Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Created by Rory Brown / "rory660"
- # www.twitch.tv/rory660
- # Do not re-share
- # This version edited by Levi is for personal use only!
- # Rory said i can use it
- # -Initialisation-
- # Module Imports
- import socket
- import re
- import time
- #Variable Initialisation
- HOST = "irc.twitch.tv" # Twitch Address.
- PORT = 6667 # Twitch IRC Port.
- NICK = "" # Displayed as "[Bot Name]"
- PASS = "oauth:_______________" # Displayed as "oauth:[Auth Characters]"
- CHAN = "#rory660" # Displayed as "#[Channel Name]"
- DELAY = time.time() # For us in limiting the command output of the bot.
- CMD = 0 # Boolean variable that dictates whether or not the message is a command.
- CMDID = 0 # ID assigned to a sent command to identify what command it is.
- USERNAME = ("Levi_Priestley") # Username of account for the bot to use.
- CMDSTRING = ("") # String to be printed to identify to the bot operater if a command is used.
- CMDDEF = ("") # String to be printed to identify to the bot operater what command is used.
- LIST = "!commands, !duck" # Command list variable
- # Twitch Connection
- s = socket.socket()
- s.connect((HOST, PORT))
- s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
- s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
- s.send("JOIN {}\r\n".format(CHAN).encode("utf-8"))
- # -Chat Message Handling-
- # Distinction between chat message and ping request is made.
- # The bot automatically responds to a ping request before a chat message.
- # Chat messages are also compiled into a username and message.
- while True:
- RESPONSE = s.recv(1024).decode("utf-8")
- if RESPONSE == "PING :tmi.twitch.tv\r\n":
- s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
- print ("duck")
- else:
- CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
- USERNAME = re.search(r"\w+", RESPONSE).group(0)
- MESSAGE = CHAT_MSG.sub("", RESPONSE)
- # Time delay variables used in ensuring the bot doesn't post messages too quickly.
- # The bot musn't post more than 20 chat messages in 30 seconds, or it may be blocked from the Twitch IRC server.
- # This bot is set to send 10 messages in 30 seconds for safety.
- REALTIME = time.time()
- TIMEGAP = REALTIME-DELAY
- # -Chat Commands-
- # Checks if message is a command, and if so, what command it is.
- if TIMEGAP >=3:
- if MESSAGE.startswith("!"):
- CMD = 1
- if "!commands" in MESSAGE or "!commandlist" in MESSAGE:
- CMDID = 1
- if "!quack" in MESSAGE or "!duck" in MESSAGE:
- CMDID = 2
- # Format:
- # if "![command]" in MESSAGE or "![synonymous command name]" in MESSAGE:
- # CMDID = [Next Unused Number]
- # Command IDs:
- #1 - Commands
- #2 - Duck
- # Command Funtionality
- if CMD == 1 and CMDID == 1:
- s.send("PRIVMSG {0} :Command List: {1}\r\n".format(CHAN, LIST).encode("utf-8"))
- DELAY = time.time()
- if CMD == 1 and CMDID == 2:
- s.send("PRIVMSG {} :/me has coded something\r\n".format(CHAN).encode("utf-8"))
- DELAY = time.time()
- # Console Output Handling
- if CMD == 1:
- CMDSTRING =(": !")
- if CMDID == 0:
- CMDDEF=("Error")
- if CMDID == 1:
- CMDDEF = ("Commands")
- if CMDID == 2:
- CMDDEF = ("Duck")
- if CMD == 0:
- CMDSTRING =("")
- CMDDEF = ("")
- if not USERNAME == "tmi":
- print(USERNAME + CMDSTRING + CMDDEF)
- # -Post Loop Iteration Variable Reinitialisation-
- CMD = 0
- CMDID = 0
- USERNAME = ("")
- CMDSTRING = ("")
- CMDDEF = ("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement