Merge pull request #40 from ancho/feature/enable-modules-default

enable modules per default
This commit is contained in:
Ville Ranki 2020-02-09 20:07:48 +02:00 committed by GitHub
commit 5684e3dade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 15 deletions

View File

@ -1,4 +1,6 @@
import collections
from datetime import datetime from datetime import datetime
from modules.common.module import BotModule from modules.common.module import BotModule
@ -6,7 +8,8 @@ class MatrixModule(BotModule):
def __init__(self, name): def __init__(self, name):
super().__init__(name) super().__init__(name)
self.enable() self.starttime = None
self.can_be_disabled = False
def matrix_start(self, bot): def matrix_start(self, bot):
super().matrix_start(bot) super().matrix_start(bot)
@ -15,6 +18,7 @@ class MatrixModule(BotModule):
async def matrix_message(self, bot, room, event): async def matrix_message(self, bot, room, event):
args = event.body.split() args = event.body.split()
if len(args) == 2: if len(args) == 2:
if args[1] == 'quit': if args[1] == 'quit':
await self.quit(bot, room, event) await self.quit(bot, room, event)
elif args[1] == 'version': elif args[1] == 'version':
@ -88,6 +92,7 @@ class MatrixModule(BotModule):
async def enable_module(self, bot, room, event, module_name): async def enable_module(self, bot, room, event, module_name):
bot.must_be_admin(room, event) bot.must_be_admin(room, event)
print(f"asked to enable {module_name}") print(f"asked to enable {module_name}")
if bot.modules.get(module_name): if bot.modules.get(module_name):
module = bot.modules.get(module_name) module = bot.modules.get(module_name)
module.enable() module.enable()
@ -100,20 +105,26 @@ class MatrixModule(BotModule):
async def disable_module(self, bot, room, event, module_name): async def disable_module(self, bot, room, event, module_name):
bot.must_be_admin(room, event) bot.must_be_admin(room, event)
print(f"asked to disable {module_name}") print(f"asked to disable {module_name}")
if bot.modules.get(module_name): if bot.modules.get(module_name):
module = bot.modules.get(module_name) module = bot.modules.get(module_name)
module.disable() if module.can_be_disabled:
module.matrix_stop(bot) module.disable()
bot.save_settings() module.matrix_stop(bot)
await bot.send_text(room, f"module {module_name} disabled") bot.save_settings()
await bot.send_text(room, f"module {module_name} disabled")
else:
await bot.send_text(room, f"module {module_name} cannot be disabled")
else: else:
await bot.send_text(room, f"module with name {module_name} not found. execute !bot modules for a list of available modules") 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): async def show_modules(self, bot, room):
await bot.send_text(room, "Modules:\n") await bot.send_text(room, "Modules:\n")
for modulename, module in bot.modules.items(): for modulename, module in collections.OrderedDict(sorted(bot.modules.items())).items():
await bot.send_text(room, f"Name: {modulename:20s} Enabled: {module.enabled}") module_message = f"Name: {modulename}\n"\
f"Enabled: {module.enabled} (Can be disabled: {module.can_be_disabled})\n"\
f"Description: {module.help()}\n"
await bot.send_text(room, module_message)
def help(self): def help(self):
return 'Bot management commands. (quit, version, reload, status, stats, leave, modules, enable, disable)' return 'Bot management commands. (quit, version, reload, status, stats, leave, modules, enable, disable)'

View File

@ -27,7 +27,8 @@ class BotModule(ABC):
""" """
def __init__(self, name): def __init__(self, name):
self.enabled = False self.enabled = True
self.can_be_disabled = True
self.name = name self.name = name
def matrix_start(self, bot): def matrix_start(self, bot):
@ -80,7 +81,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
""" """
return {'enabled': self.enabled} return {'enabled': self.enabled, 'can_be_disabled': self.can_be_disabled}
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
@ -88,8 +89,10 @@ 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
""" """
if data.get('enabled'): if data.get('enabled') is not None:
self.enabled = data['enabled'] self.enabled = data['enabled']
if data.get('can_be_disabled') is not None:
self.can_be_disabled = data['can_be_disabled']
def enable(self): def enable(self):
self.enabled = True self.enabled = True

View File

@ -3,10 +3,6 @@ from modules.common.module import BotModule
class MatrixModule(BotModule): class MatrixModule(BotModule):
def __init__(self, name):
super().__init__(name)
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'