minor cleanup and a fix for the type mapping of matrix_message parameters

This commit is contained in:
Frank Becker 2020-02-03 21:45:14 +00:00 committed by plocki
parent f42e759b4a
commit 1e35be27cd
3 changed files with 58 additions and 42 deletions

View File

@ -11,27 +11,31 @@ class MatrixModule(BotModule):
args = event.body.split()
if len(args) == 2:
if args[1] == 'quit':
bot.must_be_admin(room, event)
await bot.send_text(room, f'Quitting, as requested')
print(f'{event.sender} commanded bot to quit, so quitting..')
bot.bot_task.cancel()
await self.quit(bot, room, event)
elif args[1] == 'version':
await bot.send_text(room, f'Hemppa version {bot.version} - https://github.com/vranki/hemppa')
await self.version(bot, room)
elif args[1] == 'reload':
bot.must_be_admin(room, event)
await bot.send_text(room, f'Reloading modules..')
bot.stop()
bot.reload_modules()
bot.start()
await self.reload(bot, room, event)
elif args[1] == 'status':
uptime = datetime.now() - self.starttime
await bot.send_text(room,
f'Uptime {uptime} - system time is {datetime.now()} - loaded {len(bot.modules)} modules.')
await self.status(bot, room)
elif args[1] == 'stats':
await self.stats(bot, room)
elif args[1] == 'leave':
await self.leave(bot, room, event)
else:
await bot.send_text(room, 'Unknown command, sorry.')
async def leave(self, bot, room, event):
bot.must_be_admin(room, event)
print(f'{event.sender} asked bot to leave room {room.room_id}')
await bot.send_text(room, f'By your command.')
await bot.client.room_leave(room.room_id)
async def stats(self, bot, room):
roomcount = len(bot.client.rooms)
usercount = 0
homeservers = dict()
for croomid in bot.client.rooms:
roomobj = bot.client.rooms[croomid]
usercount = usercount + len(roomobj.users)
@ -41,22 +45,32 @@ class MatrixModule(BotModule):
homeservers[hs] = homeservers[hs] + 1
else:
homeservers[hs] = 1
homeservers = sorted(homeservers.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
if len(homeservers) > 10:
homeservers = homeservers[0:10]
await bot.send_text(room,
f'I\'m seeing {usercount} users in {roomcount} rooms. Top ten homeservers: {homeservers}')
elif args[1] == 'leave':
bot.must_be_admin(room, event)
print(f'{event.sender} asked bot to leave room {room.room_id}')
await bot.send_text(room, f'By your command.')
await bot.client.room_leave(room.room_id)
else:
await bot.send_text(room, 'Unknown command, sorry.')
async def status(self, bot, room):
uptime = datetime.now() - self.starttime
await bot.send_text(room,
f'Uptime {uptime} - system time is {datetime.now()} - loaded {len(bot.modules)} modules.')
async def reload(self, bot, room, event):
bot.must_be_admin(room, event)
await bot.send_text(room, f'Reloading modules..')
bot.stop()
bot.reload_modules()
bot.start()
async def version(self, bot, room):
await bot.send_text(room, f'Hemppa version {bot.version} - https://github.com/vranki/hemppa')
async def quit(self, bot, room, event):
bot.must_be_admin(room, event)
await bot.send_text(room, f'Quitting, as requested')
print(f'{event.sender} commanded bot to quit, so quitting..')
bot.bot_task.cancel()
def help(self):
return 'Bot management commands'

View File

@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from nio import RoomMessageText, Event
from nio import RoomMessageText, MatrixRoom
class BotModule(ABC):
@ -40,9 +40,9 @@ class BotModule(ABC):
:param bot: a reference to the bot
:type bot: Bot
:param room: a matrix room message
:type room: RoomMessageText
:type room: MatrixRoom
:param event: a handle to the event that triggered the callback
:type event: Event
:type event: RoomMessageText
"""
pass

View File

@ -1,8 +1,10 @@
from datetime import datetime, timedelta
from random import randrange
from modules.common.module import BotModule
class PollingService:
class PollingService(BotModule):
def __init__(self):
self.known_ids = set()
self.account_rooms = dict() # Roomid -> [account, account..]