40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
from .interface import IPlugin, IServer, ILine
|
|
from pathlib import Path
|
|
from importlib import import_module
|
|
import tomllib
|
|
|
|
|
|
class Yobot:
|
|
def __init__(self, config_path: str = "config.toml") -> None:
|
|
with Path(config_path).open("rb") as f:
|
|
self.config = tomllib.load(f)
|
|
# todo: casefold config
|
|
|
|
plugin_names: set[str] = set()
|
|
for net in self.config["irc"].values():
|
|
for chan in net["channels"].values():
|
|
plugin_names.update(chan["plugins"])
|
|
|
|
plugin_names.update(self.config["repl"]["plugins"])
|
|
|
|
self.plugins: dict[str, IPlugin] = {
|
|
plugin_name: import_module(f"yobot.plugins.{plugin_name}").Plugin(
|
|
self.config[plugin_name]
|
|
)
|
|
for plugin_name in plugin_names
|
|
}
|
|
|
|
async def handle_irc(
|
|
self, bot: IServer, network: str, channel: str, line: ILine
|
|
) -> None:
|
|
conf = self.config["irc"][network]
|
|
if channel in conf["channels"]:
|
|
channel_conf = conf["channels"][channel]
|
|
for plugin in channel_conf["plugins"]:
|
|
await self.plugins[plugin].handle(bot, line)
|
|
|
|
async def handle_repl(self, bot: IServer, line: ILine) -> None:
|
|
channel_conf = self.config["repl"]
|
|
for plugin in channel_conf["plugins"]:
|
|
await self.plugins[plugin].handle(bot, line)
|