From 95cb24c11d949777bf575d45b0650b330f49cc46 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Mon, 9 Jun 2025 19:15:51 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=96=B0=E5=A2=9E=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E5=91=BD=E4=BB=A4=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/example_commands/__init__.py | 14 +++++ .../example_commands/commands/__init__.py | 4 ++ .../commands/custom_prefix_command.py | 58 +++++++++++++++++++ .../example_commands/commands/echo_command.py | 36 ++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 src/plugins/example_commands/__init__.py create mode 100644 src/plugins/example_commands/commands/__init__.py create mode 100644 src/plugins/example_commands/commands/custom_prefix_command.py create mode 100644 src/plugins/example_commands/commands/echo_command.py diff --git a/src/plugins/example_commands/__init__.py b/src/plugins/example_commands/__init__.py new file mode 100644 index 000000000..4f644bd2b --- /dev/null +++ b/src/plugins/example_commands/__init__.py @@ -0,0 +1,14 @@ +"""示例命令插件包 + +这是一个演示如何使用命令系统的示例插件。 + +功能特性: +- 提供简单的命令示例 +- 演示命令参数提取 +- 展示命令帮助信息 + +使用场景: +- 用户输入特定格式的命令时触发 +- 通过命令前缀(如/)快速执行特定功能 +- 提供快速响应的交互方式 +""" \ No newline at end of file diff --git a/src/plugins/example_commands/commands/__init__.py b/src/plugins/example_commands/commands/__init__.py new file mode 100644 index 000000000..e8dce0578 --- /dev/null +++ b/src/plugins/example_commands/commands/__init__.py @@ -0,0 +1,4 @@ +"""示例命令包 + +包含示例命令的实现 +""" \ No newline at end of file diff --git a/src/plugins/example_commands/commands/custom_prefix_command.py b/src/plugins/example_commands/commands/custom_prefix_command.py new file mode 100644 index 000000000..4169c10d8 --- /dev/null +++ b/src/plugins/example_commands/commands/custom_prefix_command.py @@ -0,0 +1,58 @@ +from src.common.logger_manager import get_logger +from src.chat.message_receive.command_handler import BaseCommand, register_command +from typing import Tuple, Optional +import random + +logger = get_logger("custom_prefix_command") + +@register_command +class DiceCommand(BaseCommand): + """骰子命令,使用!前缀而不是/前缀""" + + command_name = "dice" + command_description = "骰子命令,随机生成1-6的数字" + command_pattern = r"^[!!](?:dice|骰子)(?:\s+(?P\d+))?$" # 匹配 !dice 或 !骰子,可选参数为骰子数量 + command_help = "使用方法: !dice [数量] 或 !骰子 [数量] - 掷骰子,默认掷1个" + command_examples = ["!dice", "!骰子", "!dice 3", "!骰子 5"] + enable_command = True + + async def execute(self) -> Tuple[bool, Optional[str]]: + """执行骰子命令 + + Returns: + Tuple[bool, Optional[str]]: (是否执行成功, 回复消息) + """ + try: + # 获取骰子数量,默认为1 + count_str = self.matched_groups.get("count") + + # 确保count_str不为None + if count_str is None: + count = 1 # 默认值 + else: + try: + count = int(count_str) + if count <= 0: + return False, "骰子数量必须大于0" + if count > 10: # 限制最大数量 + return False, "一次最多只能掷10个骰子" + except ValueError: + return False, "骰子数量必须是整数" + + # 生成随机数 + results = [random.randint(1, 6) for _ in range(count)] + + # 构建回复消息 + if count == 1: + message = f"🎲 掷出了 {results[0]} 点" + else: + dice_results = ", ".join(map(str, results)) + total = sum(results) + message = f"🎲 掷出了 {count} 个骰子: [{dice_results}],总点数: {total}" + + logger.info(f"{self.log_prefix} 执行骰子命令: {message}") + return True, message + + except Exception as e: + logger.error(f"{self.log_prefix} 执行骰子命令时出错: {e}") + return False, f"执行命令时出错: {str(e)}" \ No newline at end of file diff --git a/src/plugins/example_commands/commands/echo_command.py b/src/plugins/example_commands/commands/echo_command.py new file mode 100644 index 000000000..7db731cbf --- /dev/null +++ b/src/plugins/example_commands/commands/echo_command.py @@ -0,0 +1,36 @@ +from src.common.logger_manager import get_logger +from src.chat.message_receive.command_handler import BaseCommand, register_command +from typing import Tuple, Optional + +logger = get_logger("echo_command") + +@register_command +class EchoCommand(BaseCommand): + """回显命令,将用户输入的内容回显""" + + command_name = "echo" + command_description = "回显命令,将用户输入的内容回显" + command_pattern = r"^/echo\s+(?P.+)$" # 匹配 /echo 后面的所有内容 + command_help = "使用方法: /echo <内容> - 回显你输入的内容" + command_examples = ["/echo 你好,世界!", "/echo 这是一个测试"] + enable_command = True + + async def execute(self) -> Tuple[bool, Optional[str]]: + """执行回显命令 + + Returns: + Tuple[bool, Optional[str]]: (是否执行成功, 回复消息) + """ + try: + # 获取匹配到的内容 + content = self.matched_groups.get("content") + + if not content: + return False, "请提供要回显的内容" + + logger.info(f"{self.log_prefix} 执行回显命令: {content}") + return True, f"🔄 {content}" + + except Exception as e: + logger.error(f"{self.log_prefix} 执行回显命令时出错: {e}") + return False, f"执行命令时出错: {str(e)}" \ No newline at end of file