-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitchbot.py
More file actions
231 lines (210 loc) · 9.42 KB
/
twitchbot.py
File metadata and controls
231 lines (210 loc) · 9.42 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# twitchbot.py
import re
import os
import sys
import config
import socket
import requests
from time import sleep
import datetime
from datetime import date
log = config.LOG
cfg = True
if len(sys.argv) > 1:
for arg in sys.argv:
if arg == "--config" or sys.argv[1] == "-c":
USERNAME = raw_input("Username: ")
CHANNEL = raw_input("Channel: ")
PASSS = raw_input("OAuth: ")
cfg = False
elif arg == "--log" or arg == "-l":
log = True
elif arg != "twitchbot.py":
print("***ERROR: The following command-line arguement, \'"+sys.argv[1]+"\', is not valid modifier.***")
sys.exit()
if cfg:
# Put your channel name here
USERNAME = config.NICK
CHANNEL = config.CHAN[1:]
PASS = config.PASS
# network functions
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(USERNAME).encode("utf-8"))
s.send("JOIN {}\r\n".format('#'+CHANNEL).encode("utf-8"))
CHAT_MSG=re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")
def chat(msg):
"""
Send a chat message to the server.
Keyword arguments:
msg -- the message to be sent
"""
s.send("PRIVMSG #" + CHANNEL + " :" + msg + "\r\n")
if log:
log("BOT: " + msg + "\r\n")
print("BOT: " + msg + "\r\n")
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))
def initLog():
today = date.today()
filename = "log/log_" + str(today.month) + '-' + str(today.day) + '-' + str(today.year) + ".txt"
dir = os.path.dirname(filename)
if not os.path.exists(dir):
os.makedirs(dir)
return filename
def log(msg):
logger=open(logFile,'a+')
logger.write(msg)
def cleanLog():
filtered = []
oldLog = open(logFile, 'r+')
contents = oldLog.readlines()
for line in contents:
if 'tmi.twitch.tv' not in line and line[0] != ':':
filtered.append(line)
newLog = open(logFile, 'w+')
newLog.writelines(filtered);
# class Report:
# """
# A custom data type for report submissions
# Keyword arguments:
# self -- the Report itself
# report -- the viewer being reported
# user -- the user that submitted the report
# """
# def __init__(self, report, user):
# self.report = report
# self.user = user
def report(user, report):
# """
# Allows users to report probematic viewers using the '@report' command and stores the reports to be reviewed later by the streamer.
# Keyword arguments:
# user -- the user submitting the report
# report -- the viewer being reported
# """"
f = open('reportLog.txt', 'a+')
f.write(user + " (reported by " + user + ")\n")
print("***Report logged***")
logFile= ""
if log:
logFile = initLog()
count = 0
while sys.stdin.isatty():
if count == 2 and log:
cleanLog()
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)
message = CHAT_MSG.sub("", response)
if log:
log(str(username) + ": " + str(message))
print(username + ": " + message)
if re.match("Hey", message, re.IGNORECASE) or re.match("Hello", message, re.IGNORECASE):
# chats to viewer greetings
chat("Welcome to my stream, " + str(username) + "!")
if re.match("@report", message, re.IGNORECASE):
report(username, message[8:])
# Viewer reports
# Channel Stats
elif re.match("ChannelAge", message, re.IGNORECASE):
# Returns the age of the channel
response = requests.get('http://api.rtainc.co/twitch/channels/'+ CHANNEL + '?format=%5B0%5D%27s+account+has+existed+for+%5B1%5D')
chat(response.content)
elif re.match("WhatGame", message, re.IGNORECASE):
# Returns the game being played
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/status?format=%5B0%5D+is+currently+playing+%5B1%5D')
chat(response.content)
elif re.match("Uptime", message, re.IGNORECASE):
# Returns the length of the current stream
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/uptime?format=%5B0%5D%27s+stream+has+been+up+for+%5B1%5D&units=2')
chat(response.content)
elif re.match("ViewCount", message, re.IGNORECASE):
# Returns the channels current viewer count
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/status?format=%5B0%5D+currently+has+%5B2%5D+viewers')
chat(response.content)
elif re.match("PickViewer", message, re.IGNORECASE):
# Returns a random viewer
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/viewers/random?format=I+choose...+%5B0%5D')
chat(response.content)
# Follower Stats
elif re.match("FollowerCount", message, re.IGNORECASE):
# Returns the number of followers
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/followers/count?format=%5B0%5D+has+%5B1%5D+followers')
chat(response.content)
elif re.match("FollowTime", message, re.IGNORECASE):
# Returns how long the viewer has followed the channel
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/followers/'+username+'?format=%5B1%5D+has+been+following+%5B0%5D+for+%5B2%5D')
chat(response.content)
elif re.match("PickFollower", message, re.IGNORECASE):
# Returns a random subscriber
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/followers/random?format=I+choose...+%5B0%5D&token=TOKEN')
chat(response.content)
elif re.match("NewFollow", message, re.IGNORECASE):
# Returns the channels newest follower
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/followers?format=%5B0%5D%27s+last+%5B1%5D+followers+are:+%5B2%5D&count=1')
chat(response.content)
# Subscriber Stats
elif re.match("SubCount", message, re.IGNORECASE):
# Returns the number of subscribers
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/subs/count?format=%5B0%5D+has+%5B1%5D+subs&token=TOKEN')
chat(response.content)
elif re.match("SubTime", message, re.IGNORECASE):
# Returns how long the viewer has followed the channel
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/subs/'+username+'?format=%5B1%5D+has+been+subbed+to+%5B0%5D+for+%5B2%5D&token=TOKEN')
chat(response.content)
elif re.match("NewSub", message, re.IGNORECASE):
# Returns the channels newest subscriber
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/subs?format=%5B0%5D%27s+last+%5B1%5D+subs+are:+%5B2%5D&count=1')
chat(response.content)
elif re.match("PickSub", message, re.IGNORECASE):
# Returns a random subscriber
response = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/subs/random?format=I+choose...+%5B0%5D&token=TOKEN')
chat(response.content)
elif re.match("Time", message, re.IGNORECASE):
# Returns how long the viewer has followed and how long the viewer has subscribed to the channel
follower = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/followers/'+username+'?format=%5B1%5D+has+been+following+%5B0%5D+for+%5B2%5Dunit=3')
sub = requests.get('http://api.rtainc.co/twitch/channels/'+CHANNEL+'/subs/'+username+'?format=%5B1%5D+has+been+subbed+to+%5B0%5D+for+%5B2%5D&token=TOKEN')
chat(follower.content +' and '+ sub.content)
# Just for Fun
elif re.match("QOTD", message, re.IGNORECASE):
# Returns the quote of the day
response = requests.get('http://api.rtainc.co/twitch/brainyquote?format=Your+quote+of+the+day+is:+%22%5B0%5D%22&type=br')
chat(response.content)
elif re.match("8Ball", message, re.IGNORECASE):
# Magic 8-Ball answers
response = requests.get('http://api.rtainc.co/twitch/8ball?format=The+Magic+8-Ball+says...+%5B0%5D')
chat(response.content)
# Chat Violations
for pattern in config.VIOLATIONS:
if re.match(pattern, message):
ban(s, username)
# timeout(s, username) # Give user (10 min) timeout from chat
break
count = count + 1
sleep(1/config.RATE)
# if not sys.stdin.isatty():
# print "not sys.stdin.isatty"
# input = raw_input("")
# if input == "exit":
# print ("Closing...")
# sys.exit()
# else:
# msvcrt.getch()