SHOW:
|
|
- or go back to the newest paste.
1 | import socket | |
2 | import re | |
3 | import time | |
4 | import random | |
5 | ||
6 | HOST = "irc.twitch.tv" | |
7 | PORT = 6667 | |
8 | CHAN = "#neefly21" #the channel the bot should join | |
9 | NICK = "cpu57" #the username of the bot | |
10 | PASS = "oauth:asdfasdfasdf" #example oauth code | |
11 | ADMIN = "kaloncpu57" #a personal touch to add Admin commands | |
12 | ||
13 | def chat(sock, msg): | |
14 | """ | |
15 | Send a chat message to the server. | |
16 | Keyword arguments: | |
17 | sock -- the socket over which to send the message | |
18 | msg -- the message to be sent | |
19 | """ | |
20 | sock.send("PRIVMSG {} :{}\r\n".format(CHAN, msg).encode('utf-8')) | |
21 | ||
22 | def ban(sock, user): | |
23 | """ | |
24 | Ban a user from the current channel. | |
25 | Keyword arguments: | |
26 | sock -- the socket over which to send the ban command | |
27 | user -- the user to be banned | |
28 | """ | |
29 | chat(sock, ".ban {}".format(user)) | |
30 | ||
31 | def timeout(sock, user, secs=600): | |
32 | """ | |
33 | Time out a user for a set period of time. | |
34 | Keyword arguments: | |
35 | sock -- the socket over which to send the timeout command | |
36 | user -- the user to be timed out | |
37 | secs -- the length of the timeout in seconds (default 600) | |
38 | """ | |
39 | chat(sock, ".timeout {}".format(user, secs)) | |
40 | ||
41 | CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") | |
42 | s = socket.socket() | |
43 | s.connect((HOST, PORT)) | |
44 | s.send("PASS {}\r\n".format(PASS).encode("utf-8")) | |
45 | s.send("NICK {}\r\n".format(NICK).encode("utf-8")) | |
46 | s.send("JOIN {}\r\n".format(CHAN).encode("utf-8")) | |
47 | ||
48 | while True: | |
49 | response = s.recv(1024).decode("utf-8") | |
50 | if response == "PING :tmi.twitch.tv\r\n": | |
51 | s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) | |
52 | else: | |
53 | username = re.search(r"\w+", response).group(0) # return the entire match | |
54 | message = CHAT_MSG.sub("", response) | |
55 | msg_parts = message.lower().split() #split message to look for possible commands | |
56 | #check for commands and respond accordingly | |
57 | if msg_parts[0] == "!talk" and username == ADMIN: | |
58 | chat(s, "@%s Yes master. MrDestructoid" % ADMIN) | |
59 | elif msg_parts[0] == "!talk": | |
60 | chat(s, "@%s Hello" % username) | |
61 | elif msg_parts[0] == "!coinflip": | |
62 | chat(s, "@%s You flipped a coin and it landed on %s." % (username, ("heads" if random.randint(0, 1) == 0 else "tails"))) | |
63 | time.sleep(1 / 20 / 30) #(1 / 20 / 30) is the cooldown time for sending messages in Twitch chat |