klafirc/klafirc/loader.py

46 lines
1.4 KiB
Python
Raw Normal View History

2018-08-02 12:40:42 +00:00
import yaml
from .bot import Bot
from .settings import logger
class Loader:
def __init__(self, filename):
2019-04-21 21:14:28 +00:00
logger.info("Loading configuration from " + filename)
2018-08-02 12:40:42 +00:00
with open(filename) as f:
self.dict = yaml.load(f)
self.bots = []
def load_bot_template(self, name, channel, serv, port):
2019-04-21 21:14:28 +00:00
template = self.dict["bots"][name]
2018-08-02 12:40:42 +00:00
b = Bot(nickname=name)
b.server = serv
b.channel = channel
b.port = port
2019-04-21 21:14:28 +00:00
for ping in template.get("on_ping", []):
2018-08-02 12:40:42 +00:00
b.add_ping(ping)
2019-04-21 21:14:28 +00:00
for ping in template.get("on_ping_python", []):
b.add_python_ping(ping)
2018-08-02 12:40:42 +00:00
2019-04-21 21:14:28 +00:00
matches = template.get("on_match", [])
2018-08-02 12:40:42 +00:00
for match in matches:
b.add_reaction(match, matches[match])
2019-04-21 21:14:28 +00:00
for match in template.get("on_match_python", []):
b.add_python_reaction(match, matches[match])
2018-08-02 12:40:42 +00:00
2019-04-21 21:14:28 +00:00
b.on_join = template.get("on_join", None)
b.min_time = template.get("min_time", 20)
2018-08-02 12:40:42 +00:00
return b
def load_bots(self):
2019-04-21 21:14:28 +00:00
for channel in self.dict["channels"]:
name = channel["channel"]
serv = channel["server"]
port = channel.get("port", 6667)
bots_name = channel["bots"]
2018-08-02 12:40:42 +00:00
for nickname in bots_name:
b = self.load_bot_template(nickname, name, serv, port)
self.bots.append(b)