-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (62 loc) · 2.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import config
import utility
import pattern
import socket
import time
import csv
import re
CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
command_list = utility.loadCommands()
try:
s = socket.socket()
s.connect((config.HOST, config.PORT))
s.send("PASS {}\r\n".format(config.PASS).encode("utf-8"))
s.send("NICK {}\r\n".format(config.NICK).encode("utf-8"))
s.send("JOIN {}\r\n".format(config.CHAN).encode("utf-8"))
connected = True #Socket succefully connected
print("Connection to " + config.CHAN + " succesful")
print(command_list.keys())
except Exception as e:
print("Failed to connect:")
print(str(e))
connected = False #Socket failed to connect
def bot_loop():
while connected:
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("Pong")
else:
username = re.search(r"\w+", response).group(0)
message = CHAT_MSG.sub("", response)
print(username + ": " + message)
# Ban pattern check
for pat in pattern.BAN_PAT:
if re.match(pat, message):
utility.ban(s, username)
utility.chat(s,"Tap, tap, tap. Nevermore. " + username + " banned")
break
# Time out pattern check
for pat in pattern.TO_PAT:
if re.match(pat, message):
utility.timeout(s, username)
utility.chat(s,"Caw caw! " + username + " silence! You know what you've done...")
break
# Command check
if re.match(r'^(![A-Z,a-z])\w', message):
# Check if command exists
if message.strip() in command_list:
utility.runCommand(s, message.strip(), command_list[message.strip()])
else:
# New command check
if re.match(r'^(![A-Z,a-z])\w+\s([A-Z,a-z])', message):
command = message.split(" ", 1)[0]
action = message.split(" ", 1)[1]
utility.newCommand(s, command, action, command_list.keys())
command_list[command] = action
print("A new command: " + command + " action: " + action)
else:
utility.chat(s, "Command doesn't exists")
time.sleep(1 / config.RATE)
if __name__ == "__main__":
bot_loop()