Skip to content

Commit

Permalink
Make autojoin callback work with matrixcore
Browse files Browse the repository at this point in the history
  • Loading branch information
nexy7574 committed Jan 11, 2025
1 parent 46b4f73 commit 410488b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/niobot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ async def sync(self, *args, **kwargs) -> U[nio.SyncResponse, nio.SyncError]:
sync = await self.core.sync(self.sync_filter)
if self.sync_store:
await self.sync_store.handle_sync(sync)
self.log.debug("Sync: %r", sync)
return sync

def _populate_dm_rooms(self, sync: nio.SyncResponse):
Expand All @@ -380,15 +381,23 @@ def _populate_dm_rooms(self, sync: nio.SyncResponse):
self.log.debug("Found DM room in sync: %s", room_id)
self.direct_rooms[event.state_key] = self.rooms[room_id]

async def _auto_join_room_callback(self, room: nio.MatrixRoom, event: nio.InviteMemberEvent):
async def _auto_join_room_callback(self, room_id: str, room: matrixcore.InvitedRoom):
"""Callback for auto-joining rooms"""
if self.auto_join_rooms:
self.log.info("Joining room %s", room.room_id)
result = await self.join(room.room_id, reason=f"Auto-joining from invite sent by {event.sender}")
if isinstance(result, nio.JoinError):
self.log.error("Failed to join room %s: %s", room.room_id, result.message)
else:
self.log.info("Joined room %s", room.room_id)
room_obj = matrixcore.Room(room_id, client=self.core)
sender = "Unknown"
for event in room.invite_state.events:
room_obj.process_state_event(event)
if (
event.state_key == self.user_id
and event.type == "m.room.member"
and isinstance(event.content, matrixcore.MRoomMember)
and event.content.membership == "invite"
):
sender = event.sender

self.log.info("Joining room %s", room)
await self.core.http.join_room(room_id, reason=f"Auto-joining from invite sent by {sender}")

async def _auto_join_room_backlog_callback(self, room: nio.MatrixRoom, event: nio.InviteMemberEvent):
"""Callback for auto-joining rooms that are backlogged on startup"""
Expand Down Expand Up @@ -461,6 +470,7 @@ async def update_read_receipts(self, room: U[str, nio.MatrixRoom], event: nio.Ev

async def process_message(self, room: matrixcore.Room, event: matrixcore.ClientEventWithoutRoomID) -> None:
"""Processes a message and runs the command it is trying to invoke if any."""
self.log.debug("Processing message %r in room %r", event.event_id, room)
if self.start_time is None:
raise RuntimeError("Bot has not started yet!")
self.dispatch("message", room, event)
Expand Down

0 comments on commit 410488b

Please sign in to comment.