2022-05-23 18:47:28 -07:00
|
|
|
import logging
|
|
|
|
|
import random
|
|
|
|
|
from typing import Sequence
|
|
|
|
|
|
|
|
|
|
from asyncirc.protocol import IrcProtocol
|
2022-05-23 19:03:34 -07:00
|
|
|
from irclib.parser import Prefix
|
2022-05-23 18:47:28 -07:00
|
|
|
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
|