Files
omnibot22/omnibot/plugin.py
Alek Ratzloff 061cf9ee7b Add get_message_types() to plugin API
This allows plugins to specify the types of messages they handle. This
will be used specifically for the nickserv plugin, but could be useful
for other things too.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2022-05-30 18:14:48 -07:00

91 lines
2.4 KiB
Python

import importlib
import logging
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, bot: "Bot", plugin_config: PluginConfig):
self.__plugin_config = plugin_config
self.__bot = bot
def get_message_types(self) -> Sequence[str]:
"""
Gets the message types that this plugin listens for.
This is usually going to be just PRIVMSG by itself, i.e. `["PRIVMSG"]`.
However, if you want to handle different messages (such as PART, JOIN,
KICK, etc) then you can change that here.
"""
return ["PRIVMSG"]
@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 enabled(self) -> bool:
return self.plugin_config.get("enabled", True)
@property
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.bot.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[:1024])
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
async def on_load(self):
pass
async def on_connect(self, conn: IrcProtocol):
pass
async def on_unload(self, conn: IrcProtocol):
pass
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(bot, plugin_config)