73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
import random
|
|
from enum import Enum
|
|
|
|
from edmond.plugin import Plugin
|
|
|
|
|
|
class Mood(Enum):
|
|
CALM = "calm"
|
|
PISSED = "pissed"
|
|
|
|
|
|
class MoodPlugin(Plugin):
|
|
"""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.
|
|
|
|
There is only one question available: what's the bot mood? Multiple
|
|
questions in the `questions` list is equivalent to using aliases.
|
|
"""
|
|
|
|
REQUIRED_CONFIGS = [
|
|
"commands", "questions", "greetings", "answer", "calmed_message",
|
|
]
|
|
|
|
def __init__(self, bot):
|
|
super().__init__(bot)
|
|
|
|
@property
|
|
def mood(self):
|
|
return self.get_runtime_value("mood")
|
|
|
|
def on_welcome(self, event):
|
|
mood = random.choice(list(Mood))
|
|
self.set_runtime_value("mood", mood)
|
|
|
|
def on_join(self, event):
|
|
if event.source.nick != self.bot.nick:
|
|
return
|
|
mood = self.get_runtime_value("mood")
|
|
greetings = self.config["greetings"].get(mood.value)
|
|
if greetings:
|
|
self.bot.say(event.target, random.choice(greetings))
|
|
|
|
def on_pubmsg(self, event):
|
|
# Only one command: calm down.
|
|
if self.should_handle_command(event.arguments[0], no_content=True):
|
|
self.calm_down(event.target)
|
|
return True
|
|
# Only one question: what's your mood?
|
|
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"])
|
|
|
|
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"])
|
|
|
|
def say_mood(self, target):
|
|
answer = self.config["answer"].get(self.mood.value)
|
|
if answer:
|
|
self.bot.say(target, answer)
|