From afacb02d7ea101df2815c660503e1f7c5b00f350 Mon Sep 17 00:00:00 2001 From: gammafn Date: Wed, 28 Apr 2021 15:46:38 -0500 Subject: [PATCH] BotModule: remove can_be_disabled flag, use exception instead --- modules/bot.py | 27 +++++++++++++++------------ modules/common/module.py | 9 ++++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/modules/bot.py b/modules/bot.py index 150781d..603777e 100644 --- a/modules/bot.py +++ b/modules/bot.py @@ -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)' diff --git a/modules/common/module.py b/modules/common/module.py index 97821de..efcb46b 100644 --- a/modules/common/module.py +++ b/modules/common/module.py @@ -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.