-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhecklebot.py
395 lines (312 loc) · 9.82 KB
/
hecklebot.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import socket
import threading
import datetime
from urllib2 import urlopen
import json
from random import randint
import time
from urllib2 import HTTPError
from urllib2 import URLError
import traceback
from commands.koth import Koth
from commands.help import Help
from commands.info import Info
from commands.heckleme import Heckleme
from commands.greetFollowers import GreetFollowers
from commands.giveaway import Giveaway
from commands.money import Money
from commands.loyalty import Loyalty
from commands.followViewers import FollowViewers
from commands.evalCmd import EvalCMD
true = True
false = False
class Hecklebot:
bot_owner = 'Numbuhfour'
streamer = 'misteratombomb'
nick = 'Hecklebot'
channel = '#misteratombomb'
server = 'irc.twitch.tv'
password = 'rawr';
logFileName = 'log.log'
chatLogFileName = 'chatlog.txt'
queue = 13
conf = []
oauthFile = ""
commands = []
stop = False
doLog = False
def __init__(self):
self.oauthFile = open('oauth.txt','r');
self.password = self.oauthFile.read();
self.oauthFile.close();
self.initCommands()
self.loadSettings()
self.logfile = open(self.logFileName,'a')
self.ops = []
self.viewers = []
self.stop = False
self.log("Connecting...")
self.irc = socket.socket()
self.irc.connect((self.server,6667))
self.irc.send('PASS oauth:' + self.password + '\r\n')
self.irc.send('USER ' + self.nick + ' 0 * : ' + self.bot_owner + '\r\n')
self.irc.send('NICK ' + self.nick + '\r\n')
self.irc.send('JOIN ' + self.channel + '\r\n')
self.irc.send('JOIN #' + self.nick.lower() + '\r\n')
self.log("Connected.")
self.isStreaming = True
def initCommands(self):
self.money = Money(self)
self.koth = Koth(self)
self.heckleme = Heckleme(self)
self.info = Info(self)
self.commands.append(Help(self))
self.commands.append(self.heckleme)
self.commands.append(self.koth)
self.commands.append(GreetFollowers(self))
self.commands.append(Giveaway(self))
self.commands.append(self.money)
self.commands.append(Loyalty(self))
self.commands.append(FollowViewers(self))
self.commands.append(EvalCMD(self))
self.commands.append(self.info) #perhaps last will prevent it from overriding actual commands?
def start(self):
self.queuetimer()
for c in self.commands:
c.start() #Start threads (if applicable)
try:
self.run(self.conf)
except KeyboardInterrupt:
self.stop = True
self.log("Quitting.")
self.stop = True
self.logfile.close()
def loadSettings(self):
f = open('heckle.config','r')
self.conf = json.loads(f.read());
self.streamer = self.conf['bot']['streamer']
self.bot_owner = self.conf['bot']['owner']
self.nick = self.conf['bot']['nick']
self.channel = '#' + self.streamer.lower()
self.server = self.conf['bot']['server']
self.doLog = self.conf['bot']['log']
self.logFileName = self.conf['files']['log']
self.chatLogFileName = self.conf['files']['chatlog']
for c in self.commands:
c.readFromConf(self.conf)
def saveSettings(self):
self.conf = { 'bot':{ 'owner':self.bot_owner, 'streamer':self.streamer, 'nick':self.nick, 'server':self.server }, 'files':{ 'log':self.logFileName, 'chatlog':self.chatLogFileName } }
for c in self.commands:
c.writeConf(self.conf)
with open('heckle.config', 'w') as outfile:
json.dump(self.conf,outfile)
def log(self,msg):
if(self.doLog):
time = datetime.datetime.now().strftime("[%m/%d/%Y %H:%M:%S]: ")
self.logfile.write(time + msg + "\n")
print time+msg
def writeToChatLog(self,msg):
if(self.doLog):
chatfile = open(self.chatLogFileName,'a')
time = datetime.datetime.now().strftime("[%m/%d/%Y %H:%M:%S]: ")
chatfile.write(time + msg + "<br />")
chatfile.close()
def fetchJSON(self, url):
try:
res = urlopen(url)
data = json.loads(res.read())
return data
except HTTPError as e:
self.log("HTTPError on fetch: "+ str(e) + " URL:" + str(url))
return None
except URLError as e:
self.log("URLError on fetch: "+ str(e) + " URL:" + str(url))
return None
except Error as e:
self.log("Unkown error: " + str(e) + " URL:" + str(url))
return None
return None
def message(self, msg): #function for sending messages to the IRC chat
self.queue = self.queue + 1
print self.queue
if self.queue < 20: #ensures does not send >20 msgs per 30 seconds.
tosend = 'PRIVMSG ' + self.channel + ' :' + msg + '\r\n'
self.irc.send(tosend)
self.log("SENDING: " + tosend)
self.writeToChatLog(self.nick + ": " + msg)
else:
self.log("Queue overflow. [" + msg + "] ignored.")
def checkStreamOnline(self):
data = self.fetchJSON('https://api.twitch.tv/kraken/streams/' + self.streamer)
if data == None:
if self.isStreaming == True:
self.writeToChatLog("**STREAM END**")
for c in self.commands:
c.onStreamEnd()
self.isStreaming = False
return False
if data['stream']:
if self.isStreaming == False:
self.writeToChatLog("**STREAM BEGIN**")
for c in self.commands:
c.onStreamBegin()
self.isStreaming = True
return True
else:
if self.isStreaming == True:
self.writeToChatLog("**STREAM END**")
for c in self.commands:
c.onStreamEnd()
self.isStreaming = False
return False
def checkOtherOnline(self, streamer):
data = self.fetchJSON('https://api.twitch.tv/kraken/streams/' + streamer)
if data == None:
return False
if data['stream']:
return True
else:
return False
def getFollowers(self):
i = 0
count = 1
getfollowers = []
attempts = 0
while True:
self.log("Follower Fetch Attempt: " + str(attempts))
attempts += 1
data = self.fetchJSON('https://api.twitch.tv/kraken/channels/' + self.streamer + '/follows?direction=DESC&limit=100&offset=' + str(i))
if data==None:
return None
count = data['_total']
for follower in data['follows']:
name = follower['user']['display_name'].strip()
getfollowers.append(name)
if i > count:
break;
i += 100;
time.sleep(2)
self.log('Follower Recieved Count: ' + str(len(getfollowers)))
return getfollowers
def getSubscribers(self):
i = 0
count = 1
getsubscribers = []
attempts = 0
while True:
self.log("Subscriber Fetch Attempt: " + str(attempts))
attempts += 1
data = self.fetchJSON('https://api.twitch.tv/kraken/channels/' + self.streamer + '/subscriptions?direction=DESC&limit=100&offset=' + str(i))
if data == None:
return None
count = data['_total']
for sub in data['subscriptions']:
name = sub['user']['display_name'].strip()
getsubscribers.append(name)
if i > count:
break;
i += 100;
time.sleep(2)
self.log('Subscriber Recieved Count: ' + str(len(getsubscribers)))
return getsubscribers
def queuetimer(self): #function for resetting the queue every 30 seconds and reloading log
self.logfile.close()
self.logfile = open(self.logFileName,'a')
self.queue = 0
if self.stop == False:
threading.Timer(10,self.queuetimer).start()
def isOp(self, user):
return user in self.ops or user.lower() == self.bot_owner.lower() or user.lower() == self.streamer.lower()
def addViewer(self, user):
self.writeToChatLog("**JOIN** " + user + " has joined.")
if ~(user in self.viewers) and user != 'hecklebot':
print("[off]: ADD USER " + user)
self.viewers.append(user)
for c in self.commands:
c.onJoin(user)
def remViewer(self, user):
self.writeToChatLog("**PART** " + user + " has left.")
if (user in self.viewers):
print("[off]: REM USER " + user)
self.viewers.remove(user)
for c in self.commands:
c.onPart(user)
def isOnline(self,user):
if (user in self.viewers):
return True
return False
def takeMessage(self, user, msg, conf):
self.writeToChatLog(user + ": " + msg)
lower = msg.lower()
for cmd in self.commands:
if cmd.checkMessage(msg,user) == True :
cmd.onMessage(msg,user)
return
def handleMode(self, data):
c = data.split(":jtv MODE " + self.channel + " ")[1]
act = c.split(' ')[0].strip()
user = c.split(' ')[1].strip()
if act == '+o' and ~self.isOp(user) :
self.log("Opping " + user)
self.ops.append(user)
elif act == '-o' and self.isOp(user):
self.log("Deopping " + user)
self.ops.remove(user)
def run(self, conf):
while True:
incoming = self.irc.recv(1204)
print incoming
for data in incoming.split('\n'):
try:
if len(data.strip()) == 0:
continue
if data.find('PONG') != -1:
self.log(data)
if data.find('PRIVMSG') != -1:
message = data.split(':')[2]
user = data.split(':')[1]
user = user.split('!')[0]
if(data.find('#' + self.nick.lower()) != -1 and user != 'numbuhfour'):
continue
self.takeMessage(user, message, conf)
elif data.find('PING') != -1:
# self.log('PONG')
self.irc.send(incoming.replace('PING','PONG')) #Responds to pings from server
elif data.find('#' + self.nick.lower()) != -1:
continue
elif data.find('MODE') != -1: #Someone is being opped or deopped
try:
self.handleMode(data)
except ValueError:
self.message(user + ": Invalid input")
elif data.find('PART') != -1: #Someone leaves
user = data.split(':')[1]
user = user.split('!')[0]
self.remViewer(user)
elif data.find('JOIN') != -1: #Someone joins
user = data.split(':')[1]
user = user.split('!')[0]
self.addViewer(user)
elif data.find('353') != -1: #User listing:
list = data.split(':')[2]
for name in list.split(' '):
self.addViewer(name.strip()) #Get username
except:
traceback.print_exc()
hb = Hecklebot()
hb.start()
'''
try:
main(conf)
except KeyboardInterrupt:
stop = True
log("Quitting.")
stop = True
logfile.close()
heckleFile.close()
kothFile.close()'''
'''
TODO
add an addInfo command for adding random informational commands
optimize api checks (namely stream-online)
'''