-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbotrunner.py
64 lines (52 loc) · 1.88 KB
/
botrunner.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
#!/usr/bin/env python
"""
This file is responsible for setting up the bot's event loop and
passing messages from the network loop into Plushie, which will
be run outside of the loop.
"""
import json
def loadConfig(name="config.json"):
conf = open(name, "r")
return json.loads(conf.read())
if __name__ == "__main__":
import argparse
import sys
from plushiecore import run_plushie
from botnetwork import run_network
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", help="run Plushie in debug mode (loopback server)", action="store_true")
parser.add_argument("-t", "--thread", help="run Plushie using threads instead of processes", action="store_true")
args = parser.parse_args()
subargs = {'debug': False}
if args.debug:
subargs['debug'] = True
if args.thread:
from multiprocessing import Process, Queue
else:
from multiprocessing.dummy import Process, Queue
try:
config = loadConfig()
except:
print("Loading config file failed. Dying.")
sys.exit(1)
# Messages should be as follows:
# ('type', 'message')
# Where 'type' will be one of: 'system', 'chat' and 'message' will be the message to be used
net_in = Queue() # Messages to send to NEaB
net_out = Queue() # Messages from NEaB
network_process = Process(target=run_network, args=(config, net_in, net_out, subargs))
plushie_process = Process(target=run_plushie, args=(config, net_in, net_out))
network_process.start()
plushie_process.start()
while True:
r = input("> ")
if r == "exit" or r == "stop":
print("Stopping PlushieBot...")
net_in.put(('system', 'stop'))
network_process.join()
plushie_process.join()
break
print("Stopped.")
network_process.join()
plushie_process.join()
sys.exit(0)