diff --git a/README.md b/README.md index 8351fe8..bf19178 100644 --- a/README.md +++ b/README.md @@ -528,6 +528,8 @@ Typically set your own id into it. `OWNERS_ONLY` is an optional variable once defined only the owners can operate the bot (this is a form of whitelisting) +`LEAVE_EMPTY_ROOMS` (default true) if this is set to false, the bot will stay in empty rooms + __*ATTENTION:*__ Don't include bot itself in `BOT_OWNERS` if cron or any other module that can cause bot to send custom commands is used, as it could potentially be used to run owner commands as the bot itself. To enable debugging for the root logger set `DEBUG=True`. diff --git a/bot.py b/bot.py index ee8f7b4..6fdf63f 100755 --- a/bot.py +++ b/bot.py @@ -40,6 +40,7 @@ class Bot: self.client = None self.join_on_invite = False self.modules = dict() + self.leave_empty_rooms = True self.pollcount = 0 self.poll_task = None self.owners = [] @@ -385,11 +386,13 @@ class Bot: access_token = os.getenv('MATRIX_ACCESS_TOKEN') join_on_invite = os.getenv('JOIN_ON_INVITE') owners_only = os.getenv('OWNERS_ONLY') is not None + leave_empty_rooms = os.getenv('LEAVE_EMPTY_ROOMS') if matrix_server and self.matrix_user and bot_owners and access_token: self.client = AsyncClient(matrix_server, self.matrix_user, ssl = matrix_server.startswith("https://")) self.client.access_token = access_token self.join_on_invite = join_on_invite is not None + self.leave_empty_rooms = (leave_empty_rooms or 'true').lower() == 'true' self.owners = bot_owners.split(',') self.owners_only = owners_only self.get_modules() @@ -423,7 +426,7 @@ class Bot: await self.client.sync() for roomid, room in self.client.rooms.items(): self.logger.info(f"Bot is on '{room.display_name}'({roomid}) with {len(room.users)} users") - if len(room.users) == 1: + if len(room.users) == 1 and self.leave_empty_rooms: self.logger.info(f'Room {roomid} has no other users - leaving it.') self.logger.info(await self.client.room_leave(roomid))