Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import re
- import time
- import random
- HOST = "irc.twitch.tv"
- PORT = 6667
- CHAN = "#neefly21" #the channel the bot should join
- NICK = "cpu57" #the username of the bot
- PASS = "oauth:asdfasdfasdf" #example oauth code
- ADMIN = "kaloncpu57" #a personal touch to add Admin commands
- def chat(sock, msg):
- """
- Send a chat message to the server.
- Keyword arguments:
- sock -- the socket over which to send the message
- msg -- the message to be sent
- """
- sock.send("PRIVMSG {} :{}\r\n".format(CHAN, msg).encode('utf-8'))
- def ban(sock, user):
- """
- Ban a user from the current channel.
- Keyword arguments:
- sock -- the socket over which to send the ban command
- user -- the user to be banned
- """
- chat(sock, ".ban {}".format(user))
- def timeout(sock, user, secs=600):
- """
- Time out a user for a set period of time.
- Keyword arguments:
- sock -- the socket over which to send the timeout command
- user -- the user to be timed out
- secs -- the length of the timeout in seconds (default 600)
- """
- chat(sock, ".timeout {}".format(user, secs))
- CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
- 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"))
- 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"))
- else:
- username = re.search(r"\w+", response).group(0) # return the entire match
- message = CHAT_MSG.sub("", response)
- msg_parts = message.lower().split() #split message to look for possible commands
- #check for commands and respond accordingly
- if msg_parts[0] == "!talk" and username == ADMIN:
- chat(s, "@%s Yes master. MrDestructoid" % ADMIN)
- elif msg_parts[0] == "!talk":
- chat(s, "@%s Hello" % username)
- elif msg_parts[0] == "!coinflip":
- chat(s, "@%s You flipped a coin and it landed on %s." % (username, ("heads" if random.randint(0, 1) == 0 else "tails")))
- time.sleep(1 / 20 / 30) #(1 / 20 / 30) is the cooldown time for sending messages in Twitch chat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement