46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from dataclasses import dataclass
|
|
|
|
|
|
class Plugin:
|
|
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
self.config = self.get_config()
|
|
|
|
@property
|
|
def callbacks(self):
|
|
"""List of callback types available for this plugin."""
|
|
return {
|
|
cb[3:]: getattr(self, cb)
|
|
for cb in dir(self)
|
|
if cb.startswith("on_") and callable(getattr(self, cb))
|
|
}
|
|
|
|
def get_config(self):
|
|
"""Return the plugin section from the bot config."""
|
|
name = self.__class__.__name__.lower()[:-6] # Remove Plugin suffix.
|
|
plugins_configs = self.bot.config["plugins"]
|
|
return plugins_configs.get(name, {})
|
|
|
|
def should_handle_command(self, message):
|
|
"""Store Command in object and return True if it should handle it. """
|
|
command = self.parse_command(message)
|
|
if command and any(c == command.ident for c in self.COMMANDS):
|
|
self.command = command
|
|
return True
|
|
return False
|
|
|
|
def parse_command(self, message):
|
|
"""Return a command ID if this message is a command."""
|
|
words = message.split()
|
|
if words[0].lower() in self.bot.names and words[-1] == "please":
|
|
ident = words[1]
|
|
content = " ".join(words[2:-1])
|
|
return Command(ident, content)
|
|
|
|
|
|
@dataclass
|
|
class Command:
|
|
ident: str
|
|
content: str
|