aboutsummaryrefslogtreecommitdiffstats
path: root/jeeves/respond.py
blob: c39883b53b7c9c78c59393fdd90e436814edc193 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Sopel plugin: jeeves keyword responder

Responds to channel messages containing configured keywords with a random reply.
Can be paused/resumed via !pause / !resume (admin.py).

Config (in jeeves.cfg):
  [respond]
  keywords =
      word1
      word2
  responses =
      reply one
      reply two
"""

import random

from sopel import plugin
from sopel.config.types import ListAttribute, StaticSection


class RespondSection(StaticSection):
    keywords  = ListAttribute("keywords")
    responses = ListAttribute("responses")


def setup(bot):
    bot.config.define_section("respond", RespondSection, validate=False)


@plugin.rule(r"(?s:.*)")
@plugin.require_chanmsg
def on_message(bot, trigger):
    if bot.memory.get("jv_paused"):
        return

    try:
        keywords  = bot.config.respond.keywords  or []
        responses = bot.config.respond.responses or []
    except Exception:
        return

    if not keywords or not responses:
        return

    try:
        msg = trigger.group(0).casefold()
        if any(kw.casefold() in msg for kw in keywords):
            bot.say(random.choice(responses), trigger.sender)
    except (UnicodeError, AttributeError):
        pass