Changes related to rewriting the command handling system
This commit is contained in:
parent
69de2c9d85
commit
f104595217
10 changed files with 182 additions and 243 deletions
|
@ -13,19 +13,16 @@
|
|||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from typing import Dict, List, Union, Callable, Awaitable, Optional, Tuple
|
||||
from typing import Union, Awaitable, Optional, Tuple
|
||||
from markdown.extensions import Extension
|
||||
import markdown as md
|
||||
import attr
|
||||
|
||||
from mautrix import Client as MatrixClient
|
||||
from mautrix.util.formatter import parse_html
|
||||
from mautrix.client import EventHandler
|
||||
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
||||
MessageType, TextMessageEventContent, Format, RelatesTo)
|
||||
|
||||
from .command_spec import ParsedCommand, CommandSpec
|
||||
|
||||
|
||||
class EscapeHTML(Extension):
|
||||
def extendMarkdown(self, md):
|
||||
|
@ -71,14 +68,6 @@ class MaubotMessageEvent(MessageEvent):
|
|||
|
||||
|
||||
class MaubotMatrixClient(MatrixClient):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.command_handlers: Dict[str, List[EventHandler]] = {}
|
||||
self.commands: List[ParsedCommand] = []
|
||||
self.command_specs: Dict[str, CommandSpec] = {}
|
||||
|
||||
self.add_event_handler(self._command_event_handler, EventType.ROOM_MESSAGE)
|
||||
|
||||
def send_markdown(self, room_id: RoomID, markdown: str, msgtype: MessageType = MessageType.TEXT,
|
||||
relates_to: Optional[RelatesTo] = None, **kwargs) -> Awaitable[EventID]:
|
||||
content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML)
|
||||
|
@ -87,53 +76,6 @@ class MaubotMatrixClient(MatrixClient):
|
|||
content.relates_to = relates_to
|
||||
return self.send_message(room_id, content, **kwargs)
|
||||
|
||||
def set_command_spec(self, plugin_id: str, spec: CommandSpec) -> None:
|
||||
self.command_specs[plugin_id] = spec
|
||||
self._reparse_command_specs()
|
||||
|
||||
def _reparse_command_specs(self) -> None:
|
||||
self.commands = [parsed_command
|
||||
for spec in self.command_specs.values()
|
||||
for parsed_command in spec.parse()]
|
||||
|
||||
def remove_command_spec(self, plugin_id: str) -> None:
|
||||
try:
|
||||
del self.command_specs[plugin_id]
|
||||
self._reparse_command_specs()
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
async def _command_event_handler(self, evt: MessageEvent) -> None:
|
||||
if evt.sender == self.mxid or evt.content.msgtype == MessageType.NOTICE:
|
||||
return
|
||||
for command in self.commands:
|
||||
if command.match(evt):
|
||||
await self._trigger_command(command, evt)
|
||||
return
|
||||
|
||||
async def _trigger_command(self, command: ParsedCommand, evt: MessageEvent) -> None:
|
||||
for handler in self.command_handlers.get(command.name, []):
|
||||
await handler(evt)
|
||||
|
||||
def on(self, var: Union[EventHandler, EventType, str]
|
||||
) -> Union[EventHandler, Callable[[EventHandler], EventHandler]]:
|
||||
if isinstance(var, str):
|
||||
def decorator(func: EventHandler) -> EventHandler:
|
||||
self.add_command_handler(var, func)
|
||||
return func
|
||||
|
||||
return decorator
|
||||
return super().on(var)
|
||||
|
||||
def add_command_handler(self, command: str, handler: EventHandler) -> None:
|
||||
self.command_handlers.setdefault(command, []).append(handler)
|
||||
|
||||
def remove_command_handler(self, command: str, handler: EventHandler) -> None:
|
||||
try:
|
||||
self.command_handlers[command].remove(handler)
|
||||
except (KeyError, ValueError):
|
||||
pass
|
||||
|
||||
async def call_handlers(self, event: Event) -> None:
|
||||
if isinstance(event, MessageEvent):
|
||||
event = MaubotMessageEvent(event, self)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue