forked from MilkSR/WooferedBot
/
main.py
84 lines (70 loc) · 2.64 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
import json
from time import sleep
from irc import WooferBotFactory
from handler import WooferHandler
from twisted.internet import reactor, threads
from settings import config
# global variables
bots = []
def keyboard_handler():
print 'Hi, keyboard input here plz'
while True:
cmd = raw_input()
if cmd == 'q': break
elif cmd == ' ':
command = raw_input("What do you want to do?:")
if command.lower() == "join":
newChannel = raw_input("Which channel do you want to join?:")
if newChannel in config['channels']:
print 'We\'re already in channel #{}'.format(newChannel)
else:
# add to last bot, or create new bot if none exist yet
if len(bots) > 0:
(bot, tcp) = bots[-1]
else:
bot = WooferBotFactory([newChannel])
tcp = reactor.connectTCP("irc.twitch.tv", 6667, bot)
bots.append((bot, tcp))
bot.addChannel(newChannel)
elif command.lower() == "chat":
channel = raw_input("Where do you want to chat?:")
# find bot associated to channel
try:
bot = next(bot for (bot,tcp) in bots if channel in bot.channels)
msg = raw_input("What do you want to say?:")
bot.irc.say(channel, msg)
except StopIteration:
print 'I don\'t know that channel'
print 'done'
def create_bots():
global config
# create bots, 10 channels per instance
split_by_n = lambda A, n: [A[i:i+n] for i in range(0, len(A), n)]
for x in split_by_n(config['channels'], n=10):
# create factory protocol and application
bot = WooferBotFactory(x)
tcp = reactor.connectTCP("irc.twitch.tv", 6667, bot)
bots.append((bot, tcp))
# throttle so twitch accepts us
sleep(5)
def shutdown():
# stop handler, close sockets
WooferHandler.stop()
for (bot, tcp) in bots:
bot.irc.quit()
tcp.disconnect()
reactor.stop()
if __name__ == '__main__':
# load config from json
config.load()
# register the thread func that handles all dispatched commands
WooferHandler.start()
# register keyboard handler, when it's down call shutdown()
threads.callMultipleInThread([(keyboard_handler, [], {}), (shutdown, [], {})])
# open sockets once we start the message loop
reactor.callInThread(create_bots)
# run bots
reactor.run()
# save settings
config.save()