Update Plugins to keep a reference to the Bot object instead of server_config
Plugins now use Bots instead of server_configs, this is useful for checking the currently joined channels and perhaps using the connection when there isn't one available in the current method. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -16,14 +16,13 @@ log = logging.getLogger(__name__)
|
||||
class Bot:
|
||||
def __init__(self, server_config: ServerConfig):
|
||||
self.__server_config = server_config
|
||||
self.__channels: Set[str] = set()
|
||||
self.__quitting = asyncio.Event()
|
||||
self.__plugins = [
|
||||
plugin.load_plugin(server_config, config)
|
||||
plugin.load_plugin(self, config)
|
||||
for config in server_config.plugins
|
||||
if config.get("enabled", True)
|
||||
]
|
||||
# TODO - this may not be needed
|
||||
self.__channels: Set[str] = set()
|
||||
self.__quitting = asyncio.Event()
|
||||
|
||||
@property
|
||||
def server_config(self) -> ServerConfig:
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
import importlib
|
||||
import logging
|
||||
from typing import Sequence
|
||||
from typing import Sequence, TYPE_CHECKING
|
||||
|
||||
from asyncirc.protocol import IrcProtocol
|
||||
from irclib.parser import Message, Prefix
|
||||
|
||||
from .config import PluginConfig, ServerConfig
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .bot import Bot
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Plugin:
|
||||
def __init__(self, server_config: ServerConfig, plugin_config: PluginConfig):
|
||||
self.__server_config = server_config
|
||||
def __init__(self, bot: "Bot", plugin_config: PluginConfig):
|
||||
self.__plugin_config = plugin_config
|
||||
self.__bot = bot
|
||||
|
||||
@property
|
||||
def channels(self) -> Sequence[str]:
|
||||
@@ -31,9 +34,13 @@ class Plugin:
|
||||
def plugin_config(self) -> PluginConfig:
|
||||
return self.__plugin_config
|
||||
|
||||
@property
|
||||
def bot(self) -> "Bot":
|
||||
return self.__bot
|
||||
|
||||
@property
|
||||
def server_config(self) -> ServerConfig:
|
||||
return self.__server_config
|
||||
return self.bot.server_config
|
||||
|
||||
@property
|
||||
def nick(self) -> str:
|
||||
@@ -65,9 +72,9 @@ class Plugin:
|
||||
pass
|
||||
|
||||
|
||||
def load_plugin(server_config: ServerConfig, plugin_config: PluginConfig) -> Plugin:
|
||||
def load_plugin(bot: "Bot", 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)
|
||||
return PluginType(bot, plugin_config)
|
||||
|
||||
Reference in New Issue
Block a user