Files
meshcore-bot/modules/commands/magic8_command.py
T
agessaman 2c971875c1 refactor(commands): share the pubkey ACL check and the [Keywords] response format
S4. _check_admin_access and announcements' _check_announcements_access ran the same pubkey validation and logging, differing only in wording; both now call BaseCommand._pubkey_in_acl(message, acl, kind, denied), which builds each log line from those two words, so every line keeps its exact text and level. The admin check still runs it inside its try, announcements outside, as before.

ping and magic8 had identical get_response_format overrides reading [Keywords] <name>; a keywords_format_key class attribute on BaseCommand now does that, and commands without one still get None. typing.Optional stays importable from both modules.

Old-vs-new over 20,000 randomized configs and senders (missing, malformed, mixed-case and listed keys; missing sections; quoted and empty formats) matched on results and log calls; three mutations were each caught.
2026-10-03 22:34:11 -07:00

80 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""
Magic 8-ball command for the MeshCore Bot
Handles the 'magic8' keyword response
"""
import random
from typing import Optional # noqa: F401 importable from this module on dev
from ..models import MeshMessage
from .base_command import BaseCommand
magic8_responses = ["It is certain.","It is decidedly so.","Without a doubt.","Yes definitely.","You may rely on it.","As I see it, yes.","Most likely.","Outlook good.","Yes.","Signs point to yes.","Reply hazy, try again.","Ask again later.","Better not tell you now.","Cannot predict now.","Concentrate and ask again.","Don't count on it.","My reply is no.","My sources say no.","Outlook not so good.","Very doubtful."]
def magic8():
answer=magic8_responses[random.randint(0,len(magic8_responses)-1)]
return answer
class Magic8Command(BaseCommand):
"""Handles the magic8 command.
Emulates a Magic 8-Ball, providing a random "fortune" response to a user's question.
"""
# Plugin metadata
# Read-only informational output; safe for scheduled {cmd:...} rendering.
render_safe = True
name = "magic8"
honors_skip_channel_check = False
enabled_attr = "magic8_enabled"
keywords_format_key = "magic8"
keywords = ['magic8']
description = "Emulates the classic Magic 8-ball toy'"
category = "games"
# Documentation
short_description = "Ask the Magic 8-Ball a yes/no question"
usage = "magic8 <question>"
examples = ["magic8 Will it rain tomorrow?"]
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 get_help_text(self, message: MeshMessage | None = None) -> str:
"""Get help text for the magic8 command.
Returns:
str: The help text for this command.
"""
return self.translate('commands.magic8.description')
async def execute(self, message: MeshMessage) -> bool:
"""Execute the magic8 command.
Selects a random response and sends it to the user.
Args:
message: The message that triggered the command.
Returns:
bool: True if executed successfully, False otherwise.
"""
answer = magic8()
# Format response with sender mention for channel messages, without for DMs
if message.is_dm:
response = f"🎱 {answer}"
else:
sender = message.sender_id or "Unknown"
response = f"🎱 @[{sender}] {answer}"
return await self.send_response(message, response)