Merge pull request #146 from xPMo/bot-disable
BotModule: remove can_be_disabled flag, use exception instead
This commit is contained in:
commit
54e986156b
|
@ -4,7 +4,7 @@ import requests
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from modules.common.module import BotModule
|
from modules.common.module import BotModule, ModuleCannotBeDisabled
|
||||||
|
|
||||||
class MatrixModule(BotModule):
|
class MatrixModule(BotModule):
|
||||||
|
|
||||||
|
@ -152,24 +152,24 @@ class MatrixModule(BotModule):
|
||||||
module.enable()
|
module.enable()
|
||||||
module.matrix_start(bot)
|
module.matrix_start(bot)
|
||||||
bot.save_settings()
|
bot.save_settings()
|
||||||
await bot.send_text(room, f"module {module_name} enabled")
|
return await bot.send_text(room, f"Module {module_name} enabled")
|
||||||
else:
|
return 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 disable_module(self, bot, room, event, module_name):
|
async def disable_module(self, bot, room, event, module_name):
|
||||||
bot.must_be_owner(event)
|
bot.must_be_owner(event)
|
||||||
self.logger.info(f"asked to disable {module_name}")
|
self.logger.info(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)
|
||||||
if module.can_be_disabled:
|
try:
|
||||||
module.disable()
|
module.disable()
|
||||||
module.matrix_stop(bot)
|
except ModuleCannotBeDisabled:
|
||||||
bot.save_settings()
|
return await bot.send_text(room, f"Module {module_name} cannot be disabled.")
|
||||||
await bot.send_text(room, f"module {module_name} disabled")
|
except Exception as e:
|
||||||
else:
|
return await bot.send_text(room, f"Module {module_name} was not disabled: {repr(e)}")
|
||||||
await bot.send_text(room, f"module {module_name} cannot be disabled")
|
module.matrix_stop(bot)
|
||||||
else:
|
bot.save_settings()
|
||||||
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} 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):
|
async def show_modules(self, bot, room):
|
||||||
modules_message = "Modules:\n"
|
modules_message = "Modules:\n"
|
||||||
|
@ -218,6 +218,9 @@ class MatrixModule(BotModule):
|
||||||
bot.save_settings()
|
bot.save_settings()
|
||||||
await bot.send_msg(event.sender, f'Private message from {bot.matrix_user}', 'Updated bot 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):
|
def help(self):
|
||||||
return 'Bot management commands. (quit, version, reload, status, stats, leave, modules, enable, disable, import, export, ping)'
|
return 'Bot management commands. (quit, version, reload, status, stats, leave, modules, enable, disable, import, export, ping)'
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,13 @@ from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from nio import RoomMessageText, MatrixRoom
|
from nio import RoomMessageText, MatrixRoom
|
||||||
|
|
||||||
|
class ModuleCannotBeDisabled(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class BotModule(ABC):
|
class BotModule(ABC):
|
||||||
"""Abtract bot module
|
"""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.
|
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.
|
No need to register it anywhere else.
|
||||||
|
@ -29,7 +31,6 @@ class BotModule(ABC):
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.enabled = True
|
self.enabled = True
|
||||||
self.can_be_disabled = True
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.logger = logging.getLogger("module " + self.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
|
:return: a dict object that can be converted to JSON
|
||||||
:rtype: dict
|
:rtype: dict
|
||||||
"""
|
"""
|
||||||
return {'enabled': self.enabled, 'can_be_disabled': self.can_be_disabled}
|
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
|
||||||
|
@ -104,8 +105,6 @@ class BotModule(ABC):
|
||||||
"""
|
"""
|
||||||
if data.get('enabled') is not None:
|
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 add_module_aliases(self, bot, args, force=False):
|
def add_module_aliases(self, bot, args, force=False):
|
||||||
"""Add a list of aliases for this module.
|
"""Add a list of aliases for this module.
|
||||||
|
|
Loading…
Reference in New Issue