BotModule: remove can_be_disabled flag, use exception instead

This commit is contained in:
gammafn 2021-04-28 15:46:38 -05:00
parent 40b810bec8
commit afacb02d7e
2 changed files with 19 additions and 17 deletions

View File

@ -4,7 +4,7 @@ import requests
from datetime import timedelta
import time
from modules.common.module import BotModule
from modules.common.module import BotModule, ModuleCannotBeDisabled
class MatrixModule(BotModule):
@ -152,24 +152,24 @@ class MatrixModule(BotModule):
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")
return await bot.send_text(room, f"Module {module_name} enabled")
return 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_owner(event)
self.logger.info(f"asked to disable {module_name}")
if bot.modules.get(module_name):
module = bot.modules.get(module_name)
if module.can_be_disabled:
try:
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 {module_name} cannot be disabled")
else:
await bot.send_text(room, f"module with name {module_name} not found. execute !bot modules for a list of available modules")
except ModuleCannotBeDisabled:
return await bot.send_text(room, f"Module {module_name} cannot be disabled.")
except Exception as e:
return await bot.send_text(room, f"Module {module_name} was not disabled: {repr(e)}")
module.matrix_stop(bot)
bot.save_settings()
return await bot.send_text(room, f"Module {module_name} disabled")
return 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):
modules_message = "Modules:\n"
@ -218,6 +218,9 @@ class MatrixModule(BotModule):
bot.save_settings()
await bot.send_msg(event.sender, f'Private message from {bot.matrix_user}', 'Updated bot settings')
def disable(self):
raise ModuleCannotBeDisabled
def help(self):
return 'Bot management commands. (quit, version, reload, status, stats, leave, modules, enable, disable, import, export, ping)'

View File

@ -3,11 +3,13 @@ from abc import ABC, abstractmethod
from nio import RoomMessageText, MatrixRoom
class ModuleCannotBeDisabled(Exception):
pass
class BotModule(ABC):
"""Abtract bot module
A module derives from this class to process and interact on room messages. The subcluss must be named `MatrixModule`.
A module derives from this class to process and interact on room messages. The subclass must be named `MatrixModule`.
Just write a python file with desired command name and place it in modules. See current modules for examples.
No need to register it anywhere else.
@ -29,7 +31,6 @@ class BotModule(ABC):
def __init__(self, name):
self.enabled = True
self.can_be_disabled = True
self.name = name
self.logger = logging.getLogger("module " + self.name)
@ -94,7 +95,7 @@ class BotModule(ABC):
:return: a dict object that can be converted to JSON
:rtype: dict
"""
return {'enabled': self.enabled, 'can_be_disabled': self.can_be_disabled}
return {'enabled': self.enabled,}
def set_settings(self, data):
"""Load these settings. It should be the same JSON you returned in previous get_settings
@ -104,8 +105,6 @@ class BotModule(ABC):
"""
if data.get('enabled') is not None:
self.enabled = data['enabled']
if data.get('can_be_disabled') is not None:
self.can_be_disabled = data['can_be_disabled']
def add_module_aliases(self, bot, args, force=False):
"""Add a list of aliases for this module.