compliments: add plugin

This commit is contained in:
dece 2020-11-04 19:09:18 +01:00
parent 7d0b1c5270
commit 889b8c2b28
3 changed files with 65 additions and 1 deletions

View file

@ -22,6 +22,10 @@
} }
} }
}, },
"compliments": {
"sentences": ["you're breathtaking"],
"calm_rate": 100
},
"horoscope": { "horoscope": {
"commands": ["horoscope"], "commands": ["horoscope"],
"meditation": "/me looks at the starts", "meditation": "/me looks at the starts",

View file

@ -86,11 +86,33 @@ class Plugin:
if self.name in self.bot.storage and key in self.bot.storage[self.name]: if self.name in self.bot.storage and key in self.bot.storage[self.name]:
self.bot.storage[self.name][key].remove(value) self.bot.storage[self.name][key].remove(value)
def should_read_message(self, message):
"""Return a message content if it has been addressed to me, else None.
If the message starts with one of the bot's names, the rest of the
message is returned. It might be an empty str if the message was only
someone saying a bot's name. In other cases, return None.
Note that you do not need to use this method for command/question
plugins, the following methods already check for this and do more
precise parsing of the message. This is rather for plugins reacting to a
message addressed to the bot that are neither commands or questions.
"""
first_word_and_rest = message.split(maxsplit=1)
num_parts = len(first_word_and_rest)
if num_parts == 0:
return None
elif first_word_and_rest[0] in self.bot.names:
if num_parts == 1:
return ""
else:
return first_word_and_rest[1].strip()
def should_answer_question(self, message): def should_answer_question(self, message):
"""Store Question in object and return True if it should answer it.""" """Store Question in object and return True if it should answer it."""
words = message.split() words = message.split()
# Is the message addressed to me? # Is the message addressed to me?
if words[0].lower() not in self.bot.names: if len(words) == 0 or words[0].lower() not in self.bot.names:
return False return False
# Are the config conditions to answer a question valid? # Are the config conditions to answer a question valid?
@ -178,6 +200,8 @@ class Plugin:
method. method.
""" """
words = message.split() words = message.split()
if len(words) == 0:
return None
command_suffix = self.config["command_suffix"] command_suffix = self.config["command_suffix"]
if words[0].lower() in self.bot.names and words[-1] == command_suffix: if words[0].lower() in self.bot.names and words[-1] == command_suffix:
raw = " ".join(words[1:-1]) raw = " ".join(words[1:-1])

View file

@ -0,0 +1,36 @@
import random
from edmond.plugin import Plugin
from edmond.plugins.mood import Mood
from edmond.utils import proc
class ComplimentsPlugin(Plugin):
REQUIRED_CONFIGS = ["sentences", "calm_rate"]
def __init__(self, bot):
super().__init__(bot)
self.priority = -3
self.mood_plugin = None
def on_welcome(self, event):
self.mood_plugin = self.bot.get_plugin("mood")
def on_pubmsg(self, event):
content = self.should_read_message(event.arguments[0])
if content is None:
return False
if content in self.config["sentences"]:
self.react_to_compliment(event.source.nick, event.target)
return True
return False
def react_to_compliment(self, sender, target):
mood = self.get_runtime_value("mood", ns="mood")
if self.mood_plugin is not None:
if mood == Mood.PISSED and proc(self.config["calm_rate"]):
self.mood_plugin.calm_down(target)
compliment = random.choice(self.config["sentences"])
reply = f"{sender} {compliment}"
self.bot.say(target, reply)