feat: Add command enable/disable configuration for various commands

- Implemented a configuration option for enabling or disabling commands across multiple command classes.
- Each command now checks its enabled state before execution, improving control over command availability.
- Updated the configuration loading mechanism to retrieve the enabled state from the config file for commands like Advert, AQI, Catfact, and others.
This commit is contained in:
agessaman
2026-01-10 09:33:15 -08:00
parent 7ee77c16c0
commit cc3fffeb54
25 changed files with 417 additions and 10 deletions
+5
View File
@@ -33,6 +33,7 @@ class AdvertCommand(BaseCommand):
bot: The bot instance.
"""
super().__init__(bot)
self.advert_enabled = self.get_config_value('Advert_Command', 'enabled', fallback=True, value_type='bool')
def get_help_text(self) -> str:
"""Get help text for the advert command.
@@ -54,6 +55,10 @@ class AdvertCommand(BaseCommand):
Returns:
bool: True if the command can be executed, False otherwise.
"""
# Check if advert command is enabled
if not self.advert_enabled:
return False
# Use the base class cooldown check
if not super().can_execute(message):
return False
+14
View File
@@ -37,6 +37,7 @@ class AqiCommand(BaseCommand):
def __init__(self, bot):
super().__init__(bot)
self.aqi_enabled = self.get_config_value('Aqi_Command', 'enabled', fallback=True, value_type='bool')
self.url_timeout = 10 # seconds
# Get default state from config for city disambiguation
@@ -88,6 +89,19 @@ class AqiCommand(BaseCommand):
def get_help_text(self) -> str:
return f"Usage: aqi <city|neighborhood|city country|lat,lon|help> - Get AQI for city/neighborhood in {self.default_state}, international cities, coordinates, or pollutant help"
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.aqi_enabled:
return False
return super().can_execute(message)
def get_pollutant_help(self) -> str:
"""Get help text explaining pollutant types within 130 characters.
+14
View File
@@ -31,6 +31,7 @@ class CatfactCommand(BaseCommand):
bot: The bot instance.
"""
super().__init__(bot)
self.catfact_enabled = self.get_config_value('Catfact_Command', 'enabled', fallback=True, value_type='bool')
# Collection of cat facts - fallback if translations not available
self.cat_facts_fallback = [
@@ -124,6 +125,19 @@ class CatfactCommand(BaseCommand):
# Return empty string so it doesn't appear in help
return ""
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.catfact_enabled:
return False
return super().can_execute(message)
async def execute(self, message: MeshMessage) -> bool:
"""Execute the cat fact command.
+22
View File
@@ -24,6 +24,28 @@ class ChannelsCommand(BaseCommand):
description = "Lists hashtag channels with sub-categories. Use 'channels' for general, 'channels list' for all categories, 'channels <category>' for specific categories, 'channels #channel' for specific channel info."
category = "basic"
def __init__(self, bot):
"""Initialize the channels command.
Args:
bot: The bot instance.
"""
super().__init__(bot)
self.channels_enabled = self.get_config_value('Channels_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.channels_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
return self.translate('commands.channels.help')
+22
View File
@@ -18,6 +18,28 @@ class CmdCommand(BaseCommand):
description = "Lists available commands in compact format"
category = "basic"
def __init__(self, bot):
"""Initialize the cmd command.
Args:
bot: The bot instance.
"""
super().__init__(bot)
self.cmd_enabled = self.get_config_value('Cmd_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.cmd_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the cmd command.
+22
View File
@@ -29,6 +29,28 @@ class DiceCommand(BaseCommand):
'd20': 20
}
def __init__(self, bot):
"""Initialize the dice command.
Args:
bot: The bot instance.
"""
super().__init__(bot)
self.dice_enabled = self.get_config_value('Dice_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.dice_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the dice command.
+19
View File
@@ -27,6 +27,9 @@ class HelloCommand(BaseCommand):
"""
super().__init__(bot)
# Load configuration
self.hello_enabled = self.get_config_value('Hello_Command', 'enabled', fallback=True, value_type='bool')
# Fallback arrays if translations not available
self._init_fallback_arrays()
@@ -353,6 +356,22 @@ class HelloCommand(BaseCommand):
return bool(re.match(defined_emoji_pattern, cleaned_text))
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
# Check if hello command is enabled
if not self.hello_enabled:
return False
# Call parent can_execute() which includes channel checking, cooldown, etc.
return super().can_execute(message)
def get_emoji_response(self, text: str, bot_name: str) -> str:
"""Get appropriate response for emoji-only message"""
import random
+22
View File
@@ -25,6 +25,28 @@ class HelpCommand(BaseCommand):
description = "Shows commands. Use 'help <command>' for details."
category = "basic"
def __init__(self, bot):
"""Initialize the help command.
Args:
bot: The bot instance.
"""
super().__init__(bot)
self.help_enabled = self.get_config_value('Help_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.help_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the help command.
+14
View File
@@ -29,6 +29,20 @@ class HfcondCommand(BaseCommand):
bot: The MeshCoreBot instance.
"""
super().__init__(bot)
self.hfcond_enabled = self.get_config_value('Hfcond_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.hfcond_enabled:
return False
return super().can_execute(message)
async def execute(self, message: MeshMessage) -> bool:
"""Execute the hfcond command.
+22
View File
@@ -27,6 +27,28 @@ class Magic8Command(BaseCommand):
description = "Emulates the classic Magic 8-ball toy'"
category = "games"
def __init__(self, bot):
"""Initialize the magic8 command.
Args:
bot: The bot instance.
"""
super().__init__(bot)
self.magic8_enabled = self.get_config_value('Magic8_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.magic8_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the magic8 command.
+14
View File
@@ -24,6 +24,20 @@ class MoonCommand(BaseCommand):
bot: The bot instance.
"""
super().__init__(bot)
self.moon_enabled = self.get_config_value('Moon_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.moon_enabled:
return False
return super().can_execute(message)
async def execute(self, message: MeshMessage) -> bool:
"""Execute the moon command.
+14
View File
@@ -23,6 +23,7 @@ class MultitestCommand(BaseCommand):
def __init__(self, bot):
super().__init__(bot)
self.multitest_enabled = self.get_config_value('Multitest_Command', 'enabled', fallback=True, value_type='bool')
self.listening = False
self.collected_paths: Set[str] = set()
self.listening_start_time = 0
@@ -31,6 +32,19 @@ class MultitestCommand(BaseCommand):
self.triggering_timestamp: float = 0.0 # Timestamp of the triggering message
self._load_config()
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.multitest_enabled:
return False
return super().can_execute(message)
def _load_config(self):
"""Load configuration for multitest command"""
response_format = self.get_config_value('Multitest_Command', 'response_format', fallback='')
+14
View File
@@ -26,6 +26,7 @@ class PathCommand(BaseCommand):
def __init__(self, bot):
super().__init__(bot)
self.path_enabled = self.get_config_value('Path_Command', 'enabled', fallback=True, value_type='bool')
# Get bot location from config for geographic proximity calculations
# Check if geographic guessing is enabled (bot has location configured)
self.geographic_guessing_enabled = False
@@ -85,6 +86,19 @@ class PathCommand(BaseCommand):
except Exception as e:
self.logger.warning(f"Error reading bot location from config: {e} - geographic proximity guessing disabled")
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.path_enabled:
return False
return super().can_execute(message)
def matches_keyword(self, message: MeshMessage) -> bool:
"""Check if message starts with 'path' keyword or 'p' shortcut (if enabled)"""
content = message.content.strip()
+22
View File
@@ -22,6 +22,28 @@ class PingCommand(BaseCommand):
description = "Responds to 'ping' with 'Pong!'"
category = "basic"
def __init__(self, bot):
"""Initialize the ping command.
Args:
bot: The bot instance.
"""
super().__init__(bot)
self.ping_enabled = self.get_config_value('Ping_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.ping_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the ping command.
+14
View File
@@ -34,6 +34,7 @@ class PrefixCommand(BaseCommand):
bot: The bot instance.
"""
super().__init__(bot)
self.prefix_enabled = self.get_config_value('Prefix_Command', 'enabled', fallback=True, value_type='bool')
# Get API URL from config, no fallback to regional API
self.api_url = self.bot.config.get('External_Data', 'repeater_prefix_api_url', fallback="")
@@ -67,6 +68,19 @@ class PrefixCommand(BaseCommand):
self.max_prefix_range > 0
)
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.prefix_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the prefix command.
+14
View File
@@ -32,6 +32,20 @@ class RepeaterCommand(BaseCommand):
def __init__(self, bot):
super().__init__(bot)
self.repeater_enabled = self.get_config_value('Repeater_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.repeater_enabled:
return False
return super().can_execute(message)
def _truncate_for_lora(self, message: str, max_size: int = None) -> str:
"""Truncate message to fit within LoRa size limits.
+22
View File
@@ -24,6 +24,28 @@ class RollCommand(BaseCommand):
description = "Roll a random number between 1 and X (default 100). Use 'roll' for 1-100, 'roll 50' for 1-50, etc."
category = "games"
def __init__(self, bot):
"""Initialize the roll command.
Args:
bot: The bot instance.
"""
super().__init__(bot)
self.roll_enabled = self.get_config_value('Roll_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.roll_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the roll command.
+14
View File
@@ -35,6 +35,20 @@ class SatpassCommand(BaseCommand):
bot: The bot instance.
"""
super().__init__(bot)
self.satpass_enabled = self.get_config_value('Satpass_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.satpass_enabled:
return False
return super().can_execute(message)
async def execute(self, message: MeshMessage) -> bool:
"""Execute the satpass command.
+14
View File
@@ -29,6 +29,20 @@ class SolarCommand(BaseCommand):
bot: The MeshCoreBot instance.
"""
super().__init__(bot)
self.solar_enabled = self.get_config_value('Solar_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.solar_enabled:
return False
return super().can_execute(message)
async def execute(self, message: MeshMessage) -> bool:
"""Execute the solar command.
+14
View File
@@ -41,6 +41,7 @@ class SolarforecastCommand(BaseCommand):
def __init__(self, bot):
super().__init__(bot)
self.solarforecast_enabled = self.get_config_value('Solarforecast_Command', 'enabled', fallback=True, value_type='bool')
self.url_timeout = 15 # seconds
# Forecast cache: {cache_key: {'data': dict, 'timestamp': float}}
@@ -55,6 +56,19 @@ class SolarforecastCommand(BaseCommand):
# Get database manager for geocoding cache
self.db_manager = bot.db_manager
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.solarforecast_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
return self.translate('commands.solarforecast.usage')
+14
View File
@@ -28,6 +28,20 @@ class SunCommand(BaseCommand):
bot: The MeshCoreBot instance.
"""
super().__init__(bot)
self.sun_enabled = self.get_config_value('Sun_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.sun_enabled:
return False
return super().can_execute(message)
async def execute(self, message: MeshMessage) -> bool:
"""Execute the sun command.
+14
View File
@@ -28,6 +28,7 @@ class TestCommand(BaseCommand):
def __init__(self, bot):
super().__init__(bot)
self.test_enabled = self.get_config_value('Test_Command', 'enabled', fallback=True, value_type='bool')
# Get bot location from config for geographic proximity calculations
self.geographic_guessing_enabled = False
self.bot_latitude = None
@@ -56,6 +57,19 @@ class TestCommand(BaseCommand):
except Exception as e:
self.logger.warning(f"Error reading bot location from config: {e}")
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.test_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the command.
+14
View File
@@ -26,6 +26,20 @@ class WebViewerCommand(BaseCommand):
bot: The bot instance.
"""
super().__init__(bot)
self.webviewer_enabled = self.get_config_value('WebViewer_Command', 'enabled', fallback=True, value_type='bool')
def can_execute(self, message: MeshMessage) -> bool:
"""Check if this command can be executed with the given message.
Args:
message: The message triggering the command.
Returns:
bool: True if command is enabled and checks pass, False otherwise.
"""
if not self.webviewer_enabled:
return False
return super().can_execute(message)
def get_help_text(self) -> str:
"""Get help text for the webviewer command.
+5
View File
@@ -44,6 +44,7 @@ class WxCommand(BaseCommand):
def __init__(self, bot):
super().__init__(bot)
self.wx_enabled = self.get_config_value('Wx_Command', 'enabled', fallback=True, value_type='bool')
# Check weather provider setting - delegate to international command if using Open-Meteo
weather_provider = bot.config.get('Weather', 'weather_provider', fallback='noaa').lower()
@@ -129,6 +130,10 @@ class WxCommand(BaseCommand):
def can_execute(self, message: MeshMessage) -> bool:
"""Override to delegate or use base class cooldown"""
# Check if wx command is enabled
if not self.wx_enabled:
return False
if self.delegate_command:
return self.delegate_command.can_execute(message)