mirror of
https://github.com/agessaman/meshcore-bot.git
synced 2026-11-04 03:52:34 +00:00
32 commands overrode can_execute only to return False while their own <name>_enabled flag was off and otherwise defer to the base. BaseCommand now has an enabled_attr class attribute naming that flag, which can_execute checks first. Each command keeps reading its flag exactly as before (same section, default and legacy aliases), and code and tests that set help_enabled, webviewer_enabled and the like still work, since the flag is read at call time. Only overrides that were exactly 'if not self.<flag>: return False; return super().can_execute(...)' were converted, checked by AST. Commands with extra conditions (wx's delegate, joke's dark-joke DM rule, admin-only and DM-only checks, dadjoke's reversed order) keep their overrides. Several of the removed overrides called super().can_execute(message) without passing skip_channel_check; nothing calls them with it set, so honoring it now changes nothing.
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""BaseCommand.enabled_attr: the per-command enable switch checked by can_execute."""
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
from modules.commands.base_command import BaseCommand
|
|
from modules.models import MeshMessage
|
|
|
|
|
|
class _Switchable(BaseCommand):
|
|
name = "switchable"
|
|
enabled_attr = "switchable_enabled"
|
|
keywords = ["switchable"]
|
|
|
|
def __init__(self, bot, enabled):
|
|
super().__init__(bot)
|
|
self.switchable_enabled = enabled
|
|
|
|
async def execute(self, message): # pragma: no cover - not exercised
|
|
return True
|
|
|
|
|
|
def _bot():
|
|
bot = MagicMock()
|
|
bot.config.has_section.return_value = False
|
|
bot.config.has_option.return_value = False
|
|
return bot
|
|
|
|
|
|
def _dm():
|
|
return MeshMessage(content="switchable", sender_id="Ann", is_dm=True)
|
|
|
|
|
|
def test_disabled_switch_refuses_before_other_checks():
|
|
cmd = _Switchable(_bot(), enabled=False)
|
|
assert cmd.can_execute(_dm()) is False
|
|
assert cmd.can_execute(_dm(), skip_channel_check=True) is False
|
|
|
|
|
|
def test_enabled_switch_falls_through_to_base_checks():
|
|
cmd = _Switchable(_bot(), enabled=True)
|
|
assert cmd.can_execute(_dm()) is True
|
|
cmd.requires_dm = True
|
|
assert cmd.can_execute(MeshMessage(content="switchable", sender_id="Ann", channel="x", is_dm=False)) is False
|
|
|
|
|
|
def test_switch_is_read_at_call_time():
|
|
cmd = _Switchable(_bot(), enabled=True)
|
|
cmd.switchable_enabled = False
|
|
assert cmd.can_execute(_dm()) is False
|