diff --git a/appservice/appservice.py b/appservice/appservice.py index 654c365..c8e9a73 100644 --- a/appservice/appservice.py +++ b/appservice/appservice.py @@ -23,7 +23,6 @@ class AppService(bottle.Bottle): self.http = http self.logger = logging.getLogger("appservice") - # TODO better method. # Map events to functions. self.mapping = { "m.room.member": "on_member", diff --git a/appservice/gateway.py b/appservice/gateway.py index 36421ee..e1f70c3 100644 --- a/appservice/gateway.py +++ b/appservice/gateway.py @@ -33,8 +33,11 @@ class Gateway(object): while True: try: await self.gateway_handler(self.get_gateway_url()) - except websockets.ConnectionClosedError: - # TODO reconnect ? + except ( + websockets.ConnectionClosedError, + websockets.InvalidMessage, + ): + # TODO reconnect self.logger.exception("Quitting, connection lost.") break @@ -63,9 +66,6 @@ class Gateway(object): self.query_ev.set() def handle_otype(self, data: dict, otype: str) -> None: - if data.get("embeds"): - return # TODO embeds - if otype == "MESSAGE_CREATE" or otype == "MESSAGE_UPDATE": obj = discord.Message(data) elif otype == "MESSAGE_DELETE": @@ -193,6 +193,8 @@ class Gateway(object): json.dumps(self.Payloads.QUERY(guild_id, name)) ) + # TODO clean this mess. + # Wait for our websocket to receive the chunk. await asyncio.wait_for(self.query_ev.wait(), timeout=5) diff --git a/appservice/main.py b/appservice/main.py index aab6678..8818c45 100644 --- a/appservice/main.py +++ b/appservice/main.py @@ -30,9 +30,6 @@ class MatrixClient(AppService): self.emote_cache: Dict[str, str] = {} self.format = "_discord_" # "{@,#}_discord_1234:localhost" - def to_return(self, event: matrix.Event) -> bool: - return event.sender.startswith(("@_discord", self.user_id)) - def handle_bridge(self, message: matrix.Event) -> None: # Ignore events that aren't for us. if message.sender.split(":")[ @@ -77,7 +74,10 @@ class MatrixClient(AppService): self.join_room(event.room_id) def on_message(self, message: matrix.Event) -> None: - if self.to_return(message): + if ( + message.sender.startswith((f"@{self.format}", self.user_id)) + or not message.body + ): return # Handle bridging commands. @@ -106,9 +106,6 @@ class MatrixClient(AppService): ) else: - if not message.body: - return - message.body = self.process_message(channel_id, message.body) message_cache[message.event_id] = { "message_id": self.discord.send_webhook( @@ -428,6 +425,7 @@ class DiscordClient(Gateway): def to_return(self, message: discord.Message) -> bool: return ( message.channel_id not in self.app.db.list_channels() + or not message.content or not message.author or message.author.discriminator == "0000" )