Files
omnibot22/plugins/fortune.py

42 lines
1.2 KiB
Python

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