enable / disable bot module
use !bot enable <module_name> oder !bot disable <module_name> to enabel or disable a module. to get a list of available modules and see if they are enabled run !bot modules
This commit is contained in:
parent
46fce1c3d6
commit
b1f428517e
10
bot.py
10
bot.py
|
@ -119,7 +119,7 @@ class Bot:
|
||||||
|
|
||||||
moduleobject = self.modules.get(command)
|
moduleobject = self.modules.get(command)
|
||||||
|
|
||||||
if "matrix_message" in dir(moduleobject):
|
if moduleobject.enabled and ("matrix_message" in dir(moduleobject)):
|
||||||
try:
|
try:
|
||||||
await moduleobject.matrix_message(bot, room, event)
|
await moduleobject.matrix_message(bot, room, event)
|
||||||
except CommandRequiresAdmin:
|
except CommandRequiresAdmin:
|
||||||
|
@ -183,6 +183,7 @@ class Bot:
|
||||||
while True:
|
while True:
|
||||||
self.pollcount = self.pollcount + 1
|
self.pollcount = self.pollcount + 1
|
||||||
for modulename, moduleobject in self.modules.items():
|
for modulename, moduleobject in self.modules.items():
|
||||||
|
if moduleobject.enabled:
|
||||||
if "matrix_poll" in dir(moduleobject):
|
if "matrix_poll" in dir(moduleobject):
|
||||||
try:
|
try:
|
||||||
await moduleobject.matrix_poll(bot, self.pollcount)
|
await moduleobject.matrix_poll(bot, self.pollcount)
|
||||||
|
@ -243,14 +244,18 @@ class Bot:
|
||||||
self.join_on_invite = join_on_invite is not None
|
self.join_on_invite = join_on_invite is not None
|
||||||
self.owners = bot_owners.split(',')
|
self.owners = bot_owners.split(',')
|
||||||
self.get_modules()
|
self.get_modules()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("The environment variables MATRIX_SERVER, MATRIX_USER and BOT_OWNERS are mandatory")
|
print("The environment variables MATRIX_SERVER, MATRIX_USER and BOT_OWNERS are mandatory")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
print(f'Starting {len(self.modules)} modules..')
|
self.load_settings(self.get_account_data())
|
||||||
|
enabled_modules = [module for module_name, module in self.modules.items() if module.enabled]
|
||||||
|
print(f'Starting {len(enabled_modules)} modules..')
|
||||||
for modulename, moduleobject in self.modules.items():
|
for modulename, moduleobject in self.modules.items():
|
||||||
|
if moduleobject.enabled:
|
||||||
print('Starting', modulename, '..')
|
print('Starting', modulename, '..')
|
||||||
if "matrix_start" in dir(moduleobject):
|
if "matrix_start" in dir(moduleobject):
|
||||||
try:
|
try:
|
||||||
|
@ -322,7 +327,6 @@ class Bot:
|
||||||
else:
|
else:
|
||||||
await self.client.client_session.close()
|
await self.client.client_session.close()
|
||||||
|
|
||||||
|
|
||||||
bot = Bot()
|
bot = Bot()
|
||||||
bot.init()
|
bot.init()
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -4,6 +4,10 @@ from modules.common.module import BotModule
|
||||||
|
|
||||||
class MatrixModule(BotModule):
|
class MatrixModule(BotModule):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.enable()
|
||||||
|
|
||||||
def matrix_start(self, bot):
|
def matrix_start(self, bot):
|
||||||
self.starttime = datetime.now()
|
self.starttime = datetime.now()
|
||||||
|
|
||||||
|
@ -22,6 +26,14 @@ class MatrixModule(BotModule):
|
||||||
await self.stats(bot, room)
|
await self.stats(bot, room)
|
||||||
elif args[1] == 'leave':
|
elif args[1] == 'leave':
|
||||||
await self.leave(bot, room, event)
|
await self.leave(bot, room, event)
|
||||||
|
elif args[1] == 'modules':
|
||||||
|
await self.show_modules(bot, room)
|
||||||
|
|
||||||
|
elif len(args) == 3:
|
||||||
|
if args[1] == 'enable':
|
||||||
|
await self.enable_module(bot, room, event, args[2])
|
||||||
|
elif args[1] == 'disable':
|
||||||
|
await self.disable_module(bot, room, event, args[2])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
await bot.send_text(room, 'Unknown command, sorry.')
|
await bot.send_text(room, 'Unknown command, sorry.')
|
||||||
|
@ -72,5 +84,35 @@ class MatrixModule(BotModule):
|
||||||
print(f'{event.sender} commanded bot to quit, so quitting..')
|
print(f'{event.sender} commanded bot to quit, so quitting..')
|
||||||
bot.bot_task.cancel()
|
bot.bot_task.cancel()
|
||||||
|
|
||||||
|
async def enable_module(self, bot, room, event, module_name):
|
||||||
|
bot.must_be_admin(room, event)
|
||||||
|
print(f"asked to enable {module_name}")
|
||||||
|
if bot.modules.get(module_name):
|
||||||
|
module = bot.modules.get(module_name)
|
||||||
|
module.enable()
|
||||||
|
module.matrix_start(bot)
|
||||||
|
bot.save_settings()
|
||||||
|
await bot.send_text(room, f"module {module_name} enabled")
|
||||||
|
else:
|
||||||
|
await bot.send_text(room, f"module with name {module_name} not found. execute !bot modules for a list of available modules")
|
||||||
|
|
||||||
|
async def disable_module(self, bot, room, event, module_name):
|
||||||
|
bot.must_be_admin(room, event)
|
||||||
|
print(f"asked to disable {module_name}")
|
||||||
|
if bot.modules.get(module_name):
|
||||||
|
module = bot.modules.get(module_name)
|
||||||
|
module.disable()
|
||||||
|
module.matrix_stop(bot)
|
||||||
|
bot.save_settings()
|
||||||
|
await bot.send_text(room, f"module {module_name} disabled")
|
||||||
|
else:
|
||||||
|
await bot.send_text(room, f"module with name {module_name} not found. execute !bot modules for a list of available modules")
|
||||||
|
|
||||||
|
|
||||||
|
async def show_modules(self, bot, room):
|
||||||
|
await bot.send_text(room, "Modules:\n")
|
||||||
|
for modulename, module in bot.modules.items():
|
||||||
|
await bot.send_text(room, f"Name: {modulename:20s} Enabled: {module.enabled}")
|
||||||
|
|
||||||
def help(self):
|
def help(self):
|
||||||
return 'Bot management commands'
|
return 'Bot management commands. (quit, version, reload, status, stats, leave, modules, enable, disable)'
|
||||||
|
|
|
@ -25,6 +25,9 @@ class BotModule(ABC):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.enabled = False
|
||||||
|
|
||||||
def matrix_start(self, bot):
|
def matrix_start(self, bot):
|
||||||
"""Called once on startup
|
"""Called once on startup
|
||||||
|
|
||||||
|
@ -75,7 +78,7 @@ class BotModule(ABC):
|
||||||
:return: a dict object that can be converted to JSON
|
:return: a dict object that can be converted to JSON
|
||||||
:rtype: dict
|
:rtype: dict
|
||||||
"""
|
"""
|
||||||
pass
|
return {'enabled': self.enabled}
|
||||||
|
|
||||||
def set_settings(self, data):
|
def set_settings(self, data):
|
||||||
"""Load these settings. It should be the same JSON you returned in previous get_settings
|
"""Load these settings. It should be the same JSON you returned in previous get_settings
|
||||||
|
@ -83,4 +86,11 @@ class BotModule(ABC):
|
||||||
:param data: a dict object containing the settings read from the account
|
:param data: a dict object containing the settings read from the account
|
||||||
:type data: dict
|
:type data: dict
|
||||||
"""
|
"""
|
||||||
pass
|
if data.get('enabled'):
|
||||||
|
self.enabled = data['enabled']
|
||||||
|
|
||||||
|
def enable(self):
|
||||||
|
self.enabled = True
|
||||||
|
|
||||||
|
def disable(self):
|
||||||
|
self.enabled = False
|
|
@ -34,9 +34,12 @@ class MatrixModule(BotModule):
|
||||||
return ('Runs scheduled commands')
|
return ('Runs scheduled commands')
|
||||||
|
|
||||||
def get_settings(self):
|
def get_settings(self):
|
||||||
return {'daily_commands': self.daily_commands}
|
data = super().get_settings()
|
||||||
|
data['daily_commands'] = self.daily_commands
|
||||||
|
return data
|
||||||
|
|
||||||
def set_settings(self, data):
|
def set_settings(self, data):
|
||||||
|
super().set_settings(data)
|
||||||
if data.get('daily_commands'):
|
if data.get('daily_commands'):
|
||||||
self.daily_commands = data['daily_commands']
|
self.daily_commands = data['daily_commands']
|
||||||
|
|
||||||
|
|
|
@ -166,9 +166,12 @@ class MatrixModule(BotModule):
|
||||||
return ('Google calendar. Lists 10 next events by default. today = list today\'s events.')
|
return ('Google calendar. Lists 10 next events by default. today = list today\'s events.')
|
||||||
|
|
||||||
def get_settings(self):
|
def get_settings(self):
|
||||||
return {'calendar_rooms': self.calendar_rooms}
|
data = super().get_settings()
|
||||||
|
data['calendar_rooms'] = self.calendar_rooms
|
||||||
|
return data
|
||||||
|
|
||||||
def set_settings(self, data):
|
def set_settings(self, data):
|
||||||
|
super().set_settings(data)
|
||||||
if data.get('calendar_rooms'):
|
if data.get('calendar_rooms'):
|
||||||
self.calendar_rooms = data['calendar_rooms']
|
self.calendar_rooms = data['calendar_rooms']
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,16 @@ from modules.common.module import BotModule
|
||||||
|
|
||||||
|
|
||||||
class MatrixModule(BotModule):
|
class MatrixModule(BotModule):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.enable()
|
||||||
|
|
||||||
async def matrix_message(self, bot, room, event):
|
async def matrix_message(self, bot, room, event):
|
||||||
msg = f'This is Hemppa {bot.version}, a generic Matrix bot. Known commands:\n\n'
|
msg = f'This is Hemppa {bot.version}, a generic Matrix bot. Known commands:\n\n'
|
||||||
|
|
||||||
for modulename, moduleobject in bot.modules.items():
|
for modulename, moduleobject in bot.modules.items():
|
||||||
|
if moduleobject.enabled:
|
||||||
msg = msg + '!' + modulename
|
msg = msg + '!' + modulename
|
||||||
try:
|
try:
|
||||||
msg = msg + ' - ' + moduleobject.help() + '\n'
|
msg = msg + ' - ' + moduleobject.help() + '\n'
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from geopy.geocoders import Nominatim
|
from geopy.geocoders import Nominatim
|
||||||
from nio import RoomMessageUnknown
|
from nio import RoomMessageUnknown, AsyncClient
|
||||||
from modules.common.module import BotModule
|
from modules.common.module import BotModule
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -146,9 +146,13 @@ class MatrixModule(BotModule):
|
||||||
self.calendars[calid].timestamp = int(time.time())
|
self.calendars[calid].timestamp = int(time.time())
|
||||||
|
|
||||||
def get_settings(self):
|
def get_settings(self):
|
||||||
return {'apikey': self.api_key or '', 'calendar_rooms': self.calendar_rooms}
|
data = super().get_settings()
|
||||||
|
data['apikey'] = self.api_key
|
||||||
|
data['calendar_rooms'] = self.calendar_rooms
|
||||||
|
return data
|
||||||
|
|
||||||
def set_settings(self, data):
|
def set_settings(self, data):
|
||||||
|
super().set_settings(data)
|
||||||
if data.get('calendar_rooms'):
|
if data.get('calendar_rooms'):
|
||||||
self.calendar_rooms = data['calendar_rooms']
|
self.calendar_rooms = data['calendar_rooms']
|
||||||
if data.get('apikey'):
|
if data.get('apikey'):
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
from twitterscraper import query_tweets_from_user
|
from twitterscraper import query_tweets_from_user
|
||||||
|
|
||||||
from modules.common.pollingservice import PollingService
|
from modules.common.pollingservice import PollingService
|
||||||
|
|
|
@ -146,9 +146,12 @@ class MatrixModule(BotModule):
|
||||||
return
|
return
|
||||||
|
|
||||||
def get_settings(self):
|
def get_settings(self):
|
||||||
return {"status": self.status}
|
data = super().get_settings()
|
||||||
|
data['status'] = self.status
|
||||||
|
return data
|
||||||
|
|
||||||
def set_settings(self, data):
|
def set_settings(self, data):
|
||||||
|
super().set_settings(data)
|
||||||
if data.get("status"):
|
if data.get("status"):
|
||||||
self.status = data["status"]
|
self.status = data["status"]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue