Move bot-oriented stuff from mautrix-python and add more logs

This commit is contained in:
Tulir Asokan 2018-10-17 20:17:14 +03:00
parent 4d2f2cd173
commit c342dc225f
3 changed files with 61 additions and 7 deletions

View File

@ -1,3 +1,3 @@
from .plugin_base import Plugin from .plugin_base import Plugin
from .command_spec import CommandSpec, Command, PassiveCommand, Argument from .command_spec import CommandSpec, Command, PassiveCommand, Argument
from .client import MaubotMatrixClient as Client from .matrix import MaubotMatrixClient as Client, MaubotMessageEvent as MessageEvent

View File

@ -13,15 +13,51 @@
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Dict, List, Union, Callable from typing import Dict, List, Union, Callable, Awaitable
import attr
import commonmark
from mautrix import Client as MatrixClient from mautrix import Client as MatrixClient
from mautrix.client import EventHandler from mautrix.client import EventHandler
from mautrix.types import EventType, MessageEvent from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
MessageType, TextMessageEventContent, Format)
from .command_spec import ParsedCommand, CommandSpec from .command_spec import ParsedCommand, CommandSpec
class MaubotMessageEvent(MessageEvent):
_client: MatrixClient
def __init__(self, base: MessageEvent, client: MatrixClient):
super().__init__(**{a.name.lstrip("_"): getattr(base, a.name)
for a in attr.fields(MessageEvent)})
self._client = client
def respond(self, content: Union[str, MessageEventContent],
event_type: EventType = EventType.ROOM_MESSAGE,
markdown: bool = True) -> Awaitable[EventID]:
if isinstance(content, str):
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
if markdown:
content.format = Format.HTML
content.formatted_body = commonmark.commonmark(content.body)
return self._client.send_message_event(self.room_id, event_type, content)
def reply(self, content: Union[str, MessageEventContent],
event_type: EventType = EventType.ROOM_MESSAGE,
markdown: bool = True) -> Awaitable[EventID]:
if isinstance(content, str):
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
if markdown:
content.format = Format.HTML
content.formatted_body = commonmark.commonmark(content.body)
content.set_reply(self)
return self._client.send_message_event(self.room_id, event_type, content)
def mark_read(self) -> Awaitable[None]:
return self._client.send_receipt(self.room_id, self.event_id, "m.read")
class MaubotMatrixClient(MatrixClient): class MaubotMatrixClient(MatrixClient):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -75,3 +111,16 @@ class MaubotMatrixClient(MatrixClient):
self.command_handlers[command].remove(handler) self.command_handlers[command].remove(handler)
except (KeyError, ValueError): except (KeyError, ValueError):
pass pass
def call_handlers(self, event: Event) -> Awaitable[None]:
if isinstance(event, MessageEvent):
if event.sender == self.mxid:
return
event = MaubotMessageEvent(event, self)
return super().call_handlers(event)
async def get_event(self, room_id: RoomID, event_id: EventID) -> Event:
event = await super().get_event(room_id, event_id)
if isinstance(event, MessageEvent):
return MaubotMessageEvent(event, self)
return event

View File

@ -45,22 +45,27 @@ class PluginInstance:
self.loader = PluginLoader.find(self.type) self.loader = PluginLoader.find(self.type)
except KeyError: except KeyError:
self.log.error(f"Failed to find loader for type {self.type}") self.log.error(f"Failed to find loader for type {self.type}")
self.db_instance.enabled = False self.enabled = False
return return
self.client = Client.get(self.primary_user) self.client = Client.get(self.primary_user)
if not self.client: if not self.client:
self.log.error(f"Failed to get client for user {self.primary_user}") self.log.error(f"Failed to get client for user {self.primary_user}")
self.db_instance.enabled = False self.enabled = False
self.log.debug("Plugin instance dependencies loaded")
async def start(self) -> None: async def start(self) -> None:
self.log.debug(f"Starting...") if not self.enabled:
self.log.warn(f"Plugin disabled, not starting.")
return
cls = self.loader.load() cls = self.loader.load()
self.plugin = cls(self.client.client, self.id, self.log) self.plugin = cls(self.client.client, self.id, self.log)
self.loader.references |= {self} self.loader.references |= {self}
await self.plugin.start() await self.plugin.start()
self.log.info(f"Started instance of {self.loader.id} v{self.loader.version} "
f"with user {self.client.id}")
async def stop(self) -> None: async def stop(self) -> None:
self.log.debug("Stopping...") self.log.debug("Stopping plugin instance...")
self.loader.references -= {self} self.loader.references -= {self}
await self.plugin.stop() await self.plugin.stop()
self.plugin = None self.plugin = None