Edm0nd/edmond/plugins/mood.py

77 lines
2.1 KiB
Python
Raw Normal View History

2020-10-09 12:19:58 +02:00
import random
from enum import Enum
from edmond.plugin import Plugin
class Mood(Enum):
2020-10-09 12:49:35 +02:00
CALM = "calm"
PISSED = "pissed"
2020-10-09 12:19:58 +02:00
class MoodPlugin(Plugin):
2020-11-01 19:29:51 +01:00
"""Handle the mood of the bot.
This plugin exposes the runtime value `mood` (Mood) which can be
used to act differently upon some actions according to the bot
current mood.
2020-11-02 16:54:32 +01:00
There is only one question available: what's the bot mood? Multiple
questions in the `questions` list is equivalent to using aliases.
2020-11-01 19:29:51 +01:00
"""
2020-10-09 12:19:58 +02:00
2020-11-02 16:39:57 +01:00
REQUIRED_CONFIGS = [
"commands",
"questions",
"greetings",
"answer",
"calmed_message",
2020-11-02 16:39:57 +01:00
]
2020-10-09 12:19:58 +02:00
def __init__(self, bot):
super().__init__(bot)
2020-11-02 16:39:57 +01:00
@property
def mood(self):
return self.get_runtime_value("mood")
2020-10-09 12:19:58 +02:00
def on_welcome(self, event):
mood = random.choice(list(Mood))
self.set_runtime_value("mood", mood)
2020-10-09 12:49:35 +02:00
def on_join(self, event):
if event.source.nick != self.bot.nick:
return
2020-10-09 12:49:35 +02:00
mood = self.get_runtime_value("mood")
greetings = self.config["greetings"].get(mood.value)
if greetings:
self.bot.say(event.target, random.choice(greetings))
2020-10-09 12:19:58 +02:00
def on_pubmsg(self, event):
2020-11-02 17:41:47 +01:00
# Only one command: calm down.
2020-11-02 16:39:57 +01:00
if self.should_handle_command(event.arguments[0], no_content=True):
self.calm_down(event.target)
return True
2020-11-02 17:41:47 +01:00
# Only one question: what's your mood?
2020-11-02 16:39:57 +01:00
if self.should_answer_question(event.arguments[0]):
self.say_mood(event.target)
return True
return False
def calm_down(self, target):
if self.mood != Mood.PISSED:
return
self.set_runtime_value("mood", Mood.CALM)
self.bot.say(target, self.config["calmed_message"])
2020-11-02 16:54:32 +01:00
def get_pissed(self, target):
if self.mood == Mood.PISSED:
return
self.set_runtime_value("mood", Mood.PISSED)
self.bot.say(target, self.config["pissed_message"])
2020-11-02 16:39:57 +01:00
def say_mood(self, target):
answer = self.config["answer"].get(self.mood.value)
2020-10-09 12:49:35 +02:00
if answer:
2020-11-02 16:39:57 +01:00
self.bot.say(target, answer)