Initial commit with functional framework(!) and example plugin

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-05-23 18:47:28 -07:00
commit a901c2351a
12 changed files with 803 additions and 0 deletions

41
plugins/fortune.py Normal file
View File

@@ -0,0 +1,41 @@
import logging
import random
from typing import Sequence
from asyncirc.protocol import IrcProtocol
from irclib.parser import Message, Prefix
from omnibot.plugin import Plugin
from omnibot.config import ConfigError
log = logging.getLogger(__name__)
class Fortune(Plugin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "fortunes" not in self.plugin_config:
raise ConfigError("fortunes", "must be supplied to Fortune plugin")
fortunes = self.plugin_config["fortunes"]
if isinstance(fortunes, str):
with open(fortunes) as fp:
self.fortunes = [line.strip() for line in fp if line]
elif isinstance(fortunes, Sequence):
self.fortunes = fortunes
else:
raise ConfigError(
"fortunes",
"must be either a list or a path to a file containing the fortunes",
)
async def on_message(self, conn: IrcProtocol, channel: str, who: Prefix, line: str):
parts = line.split()
if not parts:
return
word = parts[0]
if word == "!fortune":
fortune = random.choice(self.fortunes)
self.send_to(conn, channel, fortune)
PLUGIN_TYPE = Fortune