feat:新增示例命令插件
This commit is contained in:
14
src/plugins/example_commands/__init__.py
Normal file
14
src/plugins/example_commands/__init__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
"""示例命令插件包
|
||||
|
||||
这是一个演示如何使用命令系统的示例插件。
|
||||
|
||||
功能特性:
|
||||
- 提供简单的命令示例
|
||||
- 演示命令参数提取
|
||||
- 展示命令帮助信息
|
||||
|
||||
使用场景:
|
||||
- 用户输入特定格式的命令时触发
|
||||
- 通过命令前缀(如/)快速执行特定功能
|
||||
- 提供快速响应的交互方式
|
||||
"""
|
||||
4
src/plugins/example_commands/commands/__init__.py
Normal file
4
src/plugins/example_commands/commands/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
"""示例命令包
|
||||
|
||||
包含示例命令的实现
|
||||
"""
|
||||
@@ -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<count>\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)}"
|
||||
36
src/plugins/example_commands/commands/echo_command.py
Normal file
36
src/plugins/example_commands/commands/echo_command.py
Normal file
@@ -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<content>.+)$" # 匹配 /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)}"
|
||||
Reference in New Issue
Block a user