-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
99 lines (76 loc) · 2.95 KB
/
bot.py
File metadata and controls
99 lines (76 loc) · 2.95 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
#!/usr/bin/env python
import os
import sys
import adapters
import time
from datetime import datetime
from adapters import console,flowbot,gitter
from util import logger
import logging
import argparse
from plugins import personality
from core import reload, config
import traceback
def run_bot():
bot = Bot()
bot._config_mtime = 0
parser = argparse.ArgumentParser(description="It's a Bot. Nuff Said")
parser.add_argument('-a','--adapter', help='Adapter (console or flowbot). Default is console.')
args = parser.parse_args()
adapter_name = args.adapter
if not adapter_name or not hasattr(adapters, adapter_name.lower()):
logger.log("Adapter not found. Try console or flowbot. Using console")
adapter_name = "console"
adapter_class = getattr(adapters, adapter_name.lower())
sys.path += ['plugins'] # so 'import hook' works without duplication
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
print('Loading plugins')
config.config(bot)
reload.reload(bot, init=True)
if not hasattr(bot, 'config'):
logger.log("no config found for bot", logging.ERROR)
exit()
logger.log("Connecting")
bot.conns = {}
bot.credentials = {}
try:
if adapter_name in bot.config['adapters']:
for room, conf in bot.config['adapters'][adapter_name]["rooms"].items():
conf["responses"] = personality.load_personality(conf["personality"].lower())
bot.conns[room] = adapter_class.BotOutput(conf)
else:
error_message = "Adapter not found in config: {0}".format(adapter_name)
print(error_message)
logger.error(error_message)
sys.exit()
for name, conf in bot.config['credentials'].items():
bot.credentials[name] = conf
except Exception as e:
logger.log("malformed config file %s" % e, logging.ERROR)
sys.exit()
bot.persist_dir = os.path.abspath('persist')
if not os.path.exists(bot.persist_dir):
os.mkdir(bot.persist_dir)
logger.log("Running main loop")
last_error = datetime(2000,1,1)
last_run = datetime.now()
while (last_error - last_run).seconds > 10:
reload.reload(bot) # these functions only do things
config.config(bot) # if changes have occurred
for conn, adapter in bot.conns.items():
try:
last_run = datetime.now()
adapter.run(bot)
except SystemExit as ex:
last_error = last_run
except Exception as e:
logger.error(traceback.format_exc())
for info in sys.exc_info():
logger.log("error info: " + str(info))
#logger.log("Unexpected error: %s" % sys.exc_info())
last_error = datetime.now()
logger.log("So tired... sleeping for 5 seconds")
time.sleep(5)
class Bot(object):
pass
run_bot()