Merge pull request #117 from dylhack/optional-empty-rooms

Added LEAVE_EMPTY_ROOMS env var
This commit is contained in:
Ville Ranki 2021-01-23 22:00:33 +02:00 committed by GitHub
commit c4ebafc1e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -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`.

5
bot.py
View File

@ -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))