Initial commit with functional framework(!) and example plugin
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
60
omnibot/plugin.py
Normal file
60
omnibot/plugin.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import importlib
|
||||
import logging
|
||||
from typing import Sequence
|
||||
|
||||
from asyncirc.protocol import IrcProtocol
|
||||
from irclib.parser import Message, Prefix
|
||||
|
||||
from .config import PluginConfig, ServerConfig
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Plugin:
|
||||
def __init__(self, server_config: ServerConfig, plugin_config: PluginConfig):
|
||||
self.__server_config = server_config
|
||||
self.__plugin_config = plugin_config
|
||||
|
||||
@property
|
||||
def channels(self) -> Sequence[str]:
|
||||
if "channels" in self.plugin_config:
|
||||
return self.plugin_config["channels"]
|
||||
else:
|
||||
return self.server_config.channels
|
||||
|
||||
@property
|
||||
def plugin_config(self) -> PluginConfig:
|
||||
return self.__plugin_config
|
||||
|
||||
@property
|
||||
def server_config(self) -> ServerConfig:
|
||||
return self.__server_config
|
||||
|
||||
@property
|
||||
def nick(self) -> str:
|
||||
return self.server_config.nick
|
||||
|
||||
def send_to(self, conn: IrcProtocol, who: str, message: str):
|
||||
message = Message(None, None, "PRIVMSG", who, message)
|
||||
conn.send(str(message))
|
||||
|
||||
async def on_join(self, conn: IrcProtocol, channel: str, who: Prefix):
|
||||
pass
|
||||
|
||||
async def on_part(self, conn: IrcProtocol, channel: str, who: Prefix):
|
||||
pass
|
||||
|
||||
async def on_kick(self, conn: IrcProtocol, channel: str, who: Prefix):
|
||||
pass
|
||||
|
||||
async def on_message(self, conn: IrcProtocol, channel: str, who: Prefix, line: str):
|
||||
pass
|
||||
|
||||
|
||||
def load_plugin(server_config: ServerConfig, plugin_config: PluginConfig) -> Plugin:
|
||||
name = plugin_config["module"]
|
||||
log.info("Loading plugin %s", name)
|
||||
plugin_module = importlib.import_module(name)
|
||||
PluginType = plugin_module.PLUGIN_TYPE
|
||||
return PluginType(server_config, plugin_config)
|
||||
Reference in New Issue
Block a user