You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.6 KiB
71 lines
2.6 KiB
from typing import Type |
|
|
|
from tools.coco.client import CocoClient |
|
from tools.commons import Message, BotMessage |
|
from tools.constants import AUTHORIZED_USERIDS |
|
from .commons import * |
|
|
|
|
|
class DispatcherBotProcessor(MessageProcessor): |
|
"""A processor that matches a context, then forwards the message to a list of sub-processors. |
|
This enables the botprocessor-matching mechanism to behave kinda like a decision tree""" |
|
|
|
def __init__(self, processors_list: List[MessageProcessor]): |
|
self.dispatcher = MessageDispatcher(processors_list) |
|
|
|
def process(self, text: str, sender_id: str, users_list: UserList): |
|
return self.dispatcher.dispatch(text, sender_id, users_list) |
|
|
|
|
|
class CommandsDispatcherProcessor(DispatcherBotProcessor): |
|
"""Reacts to commands of the form '/botname command' or 'botname, command' """ |
|
|
|
def __init__(self, processors_list: List[MessageProcessor], trigger_word: str = None, default_response: str = None): |
|
super().__init__(processors_list) |
|
self.trigger = trigger_word |
|
self.default_response = default_response if default_response is not None else "Commande non reconnue, pd" |
|
|
|
def match(self, text: str, sender_id: str, users_list: UserList): |
|
trigger = self.trigger.upper() if self.trigger is not None else users_list.my_name.upper() |
|
return text.upper().startswith(trigger + ",") \ |
|
or text.upper().startswith("/" + trigger) |
|
|
|
def process(self, text: str, sender_id: str, users_list: UserList): |
|
without_cmd = text[len(self.trigger) + 1:] |
|
response = super().process(without_cmd, sender_id, users_list) |
|
return Message(self.default_response) if response is None else response |
|
|
|
|
|
def admin_command(klass: Type[MessageProcessor]): |
|
pass |
|
|
|
|
|
@admin_command |
|
class LockDownCommandProcessor: |
|
pass |
|
|
|
|
|
@admin_command |
|
class TaggerCommandProcessor: |
|
"""Adds tagging rules based on a word filtering rule""" |
|
pass |
|
|
|
|
|
class TaggerProcessor: |
|
"""Tags IP's based on word filtering rule""" |
|
pass |
|
|
|
|
|
class BotHelp(MessageProcessor): |
|
"""Displays the help string for all processors in the list that have a helpt string""" |
|
|
|
def __init__(self, processors_list: List[BaseCocobotCommand]): |
|
all_help_strs = [proc.HELP_STR |
|
for proc in processors_list if proc.HELP_STR is not None] |
|
self.help_str = ", ".join(all_help_strs) |
|
|
|
def match(self, text: str, sender_id: str, users_list: UserList): |
|
return text.lower().startswith("help") |
|
|
|
def process(self, text: str, sender_id: str, users_list: UserList): |
|
return BotMessage(self.help_str)
|
|
|