journee_mondiale: add plugin
Add support for commands in multiple words by letting a plugin decide if the message content should be not split between ident/content.
This commit is contained in:
parent
828802fdbe
commit
f4e884e9d7
|
@ -16,7 +16,7 @@ Missing features
|
|||
- [ ] Handle compliments
|
||||
- [ ] Handle
|
||||
- [x] Horoscope
|
||||
- [ ] "Journee mondiale"
|
||||
- [x] "Journee mondiale"
|
||||
- [ ] Mug
|
||||
- [ ] Music
|
||||
- [ ] Opinions
|
||||
|
|
|
@ -22,6 +22,10 @@
|
|||
"url": "http://zwergf.elynx.fr/bots/horobot/",
|
||||
"delay": 2
|
||||
},
|
||||
"journeemondiale": {
|
||||
"commands": ["journée mondiale"],
|
||||
"url": "https://www.journee-mondiale.com/"
|
||||
},
|
||||
"mood": {
|
||||
"questions": ["how are you?"],
|
||||
"greetings": {
|
||||
|
|
|
@ -70,9 +70,9 @@ class Plugin:
|
|||
return True
|
||||
return False
|
||||
|
||||
def should_handle_command(self, message):
|
||||
def should_handle_command(self, message, no_content=False):
|
||||
"""Store Command in object and return True if it should handle it."""
|
||||
command = self.parse_command(message)
|
||||
command = self.parse_command(message, no_content=no_content)
|
||||
commands = self.config.get("commands", [])
|
||||
if command and any(c == command.ident for c in commands):
|
||||
self.command = command
|
||||
|
@ -80,11 +80,15 @@ class Plugin:
|
|||
return True
|
||||
return False
|
||||
|
||||
def parse_command(self, message):
|
||||
def parse_command(self, message, no_content=False):
|
||||
"""Return a command ID if this message is a command."""
|
||||
words = message.split()
|
||||
command_suffix = self.config["command_suffix"]
|
||||
if words[0].lower() in self.bot.names and words[-1] == command_suffix:
|
||||
if no_content:
|
||||
ident = " ".join(words[1:-1])
|
||||
content = ""
|
||||
else:
|
||||
ident = words[1]
|
||||
content = " ".join(words[2:-1])
|
||||
return Command(ident, content)
|
||||
|
|
33
edmond/plugins/journee_mondiale.py
Normal file
33
edmond/plugins/journee_mondiale.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
try:
|
||||
from bs4 import BeautifulSoup
|
||||
DEPENDENCIES_FOUND = True
|
||||
except ImportError:
|
||||
DEPENDENCIES_FOUND = False
|
||||
|
||||
from edmond.plugin import Plugin
|
||||
from edmond.utils import http_get
|
||||
|
||||
|
||||
class JourneeMondialePlugin(Plugin):
|
||||
"""This plugin gets today's international observance (french)."""
|
||||
|
||||
REQUIRED_CONFIGS = ["commands", "url"]
|
||||
|
||||
def __init__(self, bot):
|
||||
super().__init__(bot)
|
||||
|
||||
def on_pubmsg(self, event):
|
||||
if not self.should_handle_command(event.arguments[0], no_content=True):
|
||||
return False
|
||||
response = http_get(self.config["url"])
|
||||
if not response:
|
||||
self.signal_failure()
|
||||
return True
|
||||
soup = BeautifulSoup(response, "html.parser")
|
||||
try:
|
||||
jm = soup.find("div", id="journeesDuJour").find("h2").string
|
||||
except (ValueError, KeyError):
|
||||
self.signal_failure()
|
||||
return True
|
||||
self.bot.say(event.target, jm)
|
||||
return True
|
5
edmond/plugins/requirements.txt
Normal file
5
edmond/plugins/requirements.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Wikipedia
|
||||
wikipedia
|
||||
|
||||
# Journée Mondiale
|
||||
beautifulsoup4
|
Loading…
Reference in a new issue