chore: 指令全部改为命令

This commit is contained in:
RockChinQ
2024-01-12 16:48:47 +08:00
parent aa768459c0
commit 41b30238c3
13 changed files with 75 additions and 75 deletions

View File

@@ -8,7 +8,7 @@ from ..utils import context
# __current__ = "default"
# """当前默认使用的情景预设的名称
# 由管理员使用`!default <名称>`令切换
# 由管理员使用`!default <名称>`令切换
# """
# __prompts_from_files__ = {}

View File

@@ -35,18 +35,18 @@ PersonNormalMessageReceived = "person_normal_message_received"
"""
PersonCommandSent = "person_command_sent"
"""判断为应该处理的私聊令时触发
"""判断为应该处理的私聊令时触发
kwargs:
launcher_type: str 发起对象类型(group/person)
launcher_id: int 发起对象ID(群号/QQ号)
sender_id: int 发送者ID(QQ号)
command: str
command: str
params: list[str] 参数列表
text_message: str 完整令文本
text_message: str 完整令文本
is_admin: bool 是否为管理员
returns (optional):
alter: str 修改后的完整令文本
alter: str 修改后的完整令文本
reply: list 回复消息组件列表
"""
@@ -64,18 +64,18 @@ GroupNormalMessageReceived = "group_normal_message_received"
"""
GroupCommandSent = "group_command_sent"
"""判断为应该处理的群聊令时触发
"""判断为应该处理的群聊令时触发
kwargs:
launcher_type: str 发起对象类型(group/person)
launcher_id: int 发起对象ID(群号/QQ号)
sender_id: int 发送者ID(QQ号)
command: str
command: str
params: list[str] 参数列表
text_message: str 完整令文本
text_message: str 完整令文本
is_admin: bool 是否为管理员
returns (optional):
alter: str 修改后的完整令文本
alter: str 修改后的完整令文本
reply: list 回复消息组件列表
"""

View File

@@ -71,7 +71,7 @@ __tree_index__: dict[str, list] = {}
结构:
{
'pkg.qqbot.cmds.cmd1.CommandCmd1': 'cmd1', # 顶级
'pkg.qqbot.cmds.cmd1.CommandCmd1': 'cmd1', # 顶级
'pkg.qqbot.cmds.cmd1.CommandCmd1_1': 'cmd1.cmd1-1', # 类名: 节点路径
'pkg.qqbot.cmds.cmd2.CommandCmd2': 'cmd2',
'pkg.qqbot.cmds.cmd2.CommandCmd2_1': 'cmd2.cmd2-1',
@@ -83,79 +83,79 @@ __tree_index__: dict[str, list] = {}
class Context:
"""命令执行上下文"""
command: str
"""顶级令文本"""
"""顶级令文本"""
crt_command: str
"""当前子令文本"""
"""当前子令文本"""
params: list
"""完整参数列表"""
crt_params: list
"""当前子令参数列表"""
"""当前子令参数列表"""
session_name: str
"""会话名"""
text_message: str
"""令完整文本"""
"""令完整文本"""
launcher_type: str
"""令发起者类型"""
"""令发起者类型"""
launcher_id: int
"""令发起者ID"""
"""令发起者ID"""
sender_id: int
"""令发送者ID"""
"""令发送者ID"""
is_admin: bool
"""[过时]令发送者是否为管理员"""
"""[过时]令发送者是否为管理员"""
privilege: int
"""令发送者权限等级"""
"""令发送者权限等级"""
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
class AbstractCommandNode:
"""令抽象类"""
"""令抽象类"""
parent: type
"""令类"""
"""令类"""
name: str
"""令名"""
"""令名"""
description: str
"""令描述"""
"""令描述"""
usage: str
"""令用法"""
"""令用法"""
aliases: list[str]
"""令别名"""
"""令别名"""
privilege: int
"""令权限等级, 权限大于等于此值的用户才能执行"""
"""令权限等级, 权限大于等于此值的用户才能执行"""
@classmethod
def process(cls, ctx: Context) -> tuple[bool, list]:
"""令处理函数
"""令处理函数
:param ctx: 令执行上下文
:param ctx: 令执行上下文
:return: (是否执行, 回复列表(若执行))
若未执行,将自动以下一个参数查找并执行子
若未执行,将自动以下一个参数查找并执行子
"""
raise NotImplementedError
@classmethod
def help(cls) -> str:
"""获取令帮助信息"""
return '令: {}\n描述: {}\n用法: \n{}\n别名: {}\n权限: {}'.format(
"""获取令帮助信息"""
return '令: {}\n描述: {}\n用法: \n{}\n别名: {}\n权限: {}'.format(
cls.name,
cls.description,
cls.usage,
@@ -172,11 +172,11 @@ class AbstractCommandNode:
aliases: list[str] = None,
privilege: int = 0
):
"""注册
"""注册
:param cls: 令类
:param name: 令名
:param parent: 父令类
:param cls: 令类
:param name: 令名
:param parent: 父令类
"""
global __command_list__, __tree_index__
@@ -191,7 +191,7 @@ class AbstractCommandNode:
logging.debug("cls: {}, name: {}, parent: {}".format(cls, name, parent))
if parent is None:
# 顶级令注册
# 顶级令注册
__command_list__[name] = {
'description': cls.description,
'usage': cls.usage,
@@ -208,9 +208,9 @@ class AbstractCommandNode:
path = __tree_index__[parent.__module__ + '.' + parent.__name__]
parent_node = __command_list__[path]
# 链接父子
# 链接父子
__command_list__[path]['sub'].append(name)
# 注册子
# 注册子
__command_list__[path + '.' + name] = {
'description': cls.description,
'usage': cls.usage,
@@ -229,18 +229,18 @@ class AbstractCommandNode:
class CommandPrivilegeError(Exception):
"""令权限不足或不存在异常"""
"""令权限不足或不存在异常"""
pass
# 传入Context对象广搜命令树返回执行结果
# 若命令被处理返回reply列表
# 若命令未被处理,继续执行下一级
# 若命令未被处理,继续执行下一级
# 若命令不存在,报异常
def execute(context: Context) -> list:
"""执行
"""执行
:param ctx: 令执行上下文
:param ctx: 令执行上下文
:return: 回复列表
"""
@@ -249,7 +249,7 @@ def execute(context: Context) -> list:
# 拷贝ctx
ctx: Context = copy.deepcopy(context)
# 从树取出顶级
# 从树取出顶级
node = __command_list__
path = ctx.command
@@ -257,7 +257,7 @@ def execute(context: Context) -> list:
while True:
try:
node = __command_list__[path]
logging.debug('执行令: {}'.format(path))
logging.debug('执行令: {}'.format(path))
# 检查权限
if ctx.privilege < node['privilege']:
@@ -278,7 +278,7 @@ def execute(context: Context) -> list:
def register_all():
"""启动时调用此函数注册所有
"""启动时调用此函数注册所有
递归处理pkg.qqbot.cmds包下及其子包下所有模块的所有继承于AbstractCommand的类
"""
@@ -304,7 +304,7 @@ def register_all():
else:
m = __import__(module.__name__ + '.' + item.name, fromlist=[''])
# for name, cls in inspect.getmembers(m, inspect.isclass):
# # 检查是否为令类
# # 检查是否为令类
# if cls.__module__ == m.__name__ and issubclass(cls, AbstractCommandNode) and cls != AbstractCommandNode:
# cls.register(cls, cls.name, cls.parent)
@@ -313,7 +313,7 @@ def register_all():
def apply_privileges():
"""读取cmdpriv.json并应用令权限"""
"""读取cmdpriv.json并应用令权限"""
# 读取内容
json_str = ""
with open('cmdpriv.json', 'r', encoding="utf-8") as f:

View File

@@ -68,7 +68,7 @@ class PluginGetCommand(aamgr.AbstractCommandNode):
def closure():
try:
plugin_host.install_plugin(ctx.crt_params[0])
pkg.utils.context.get_qqbot_manager().notify_admin("插件安装成功,请发送 !reload 令重载插件")
pkg.utils.context.get_qqbot_manager().notify_admin("插件安装成功,请发送 !reload 令重载插件")
except Exception as e:
logging.error("插件安装失败:{}".format(e))
pkg.utils.context.get_qqbot_manager().notify_admin("插件安装失败:{}".format(e))
@@ -149,7 +149,7 @@ class PluginDelCommand(aamgr.AbstractCommandNode):
unin_path = plugin_host.uninstall_plugin(plugin_name)
reply = ["[bot]已删除插件: {} ({}), 请发送 !reload 重载插件".format(plugin_name, unin_path)]
else:
reply = ["[bot]err:未找到插件: {}, 请使用!plugin令查看插件列表".format(plugin_name)]
reply = ["[bot]err:未找到插件: {}, 请使用!plugin令查看插件列表".format(plugin_name)]
return True, reply
@@ -195,7 +195,7 @@ class PluginOnOffCommand(aamgr.AbstractCommandNode):
plugin_switch.dump_switch()
reply = ["[bot]已{}插件: {}".format("启用" if new_status else "禁用", plugin_name)]
else:
reply = ["[bot]err:未找到插件: {}, 请使用!plugin令查看插件列表".format(plugin_name)]
reply = ["[bot]err:未找到插件: {}, 请使用!plugin令查看插件列表".format(plugin_name)]
return True, reply

View File

@@ -4,8 +4,8 @@ from .. import aamgr
@aamgr.AbstractCommandNode.register(
parent=None,
name="cmd",
description="显示令列表",
usage="!cmd\n!cmd <令名称>",
description="显示令列表",
usage="!cmd\n!cmd <令名称>",
aliases=[],
privilege=1
)
@@ -17,15 +17,15 @@ class CmdCommand(aamgr.AbstractCommandNode):
reply = []
if len(ctx.params) == 0:
reply_str = "[bot]当前所有令:\n\n"
reply_str = "[bot]当前所有令:\n\n"
# 遍历顶级
# 遍历顶级
for key in command_list:
command = command_list[key]
if command['parent'] is None:
reply_str += "!{} - {}\n".format(key, command['description'])
reply_str += "\n请使用 !cmd <令名称> 来查看令的详细信息"
reply_str += "\n请使用 !cmd <令名称> 来查看令的详细信息"
reply = [reply_str]
else:
@@ -33,7 +33,7 @@ class CmdCommand(aamgr.AbstractCommandNode):
if command_name in command_list:
reply = [command_list[command_name]['cls'].help()]
else:
reply = ["[bot]{} 不存在".format(command_name)]
reply = ["[bot]{} 不存在".format(command_name)]
return True, reply

View File

@@ -13,7 +13,7 @@ class HelpCommand(aamgr.AbstractCommandNode):
@classmethod
def process(cls, ctx: aamgr.Context) -> tuple[bool, list]:
import tips
reply = ["[bot] "+tips.help_message + "\n请输入 !cmd 查看令列表"]
reply = ["[bot] "+tips.help_message + "\n请输入 !cmd 查看令列表"]
# 警告config.help_message过时
import config

View File

@@ -1,4 +1,4 @@
# 令处理模块
# 令处理模块
import logging
from ..qqbot.cmds import aamgr as cmdmgr
@@ -9,7 +9,7 @@ def process_command(session_name: str, text_message: str, mgr, config: dict,
reply = []
try:
logging.info(
"[{}]发起令:{}".format(session_name, text_message[:min(20, len(text_message))] + (
"[{}]发起令:{}".format(session_name, text_message[:min(20, len(text_message))] + (
"..." if len(text_message) > 20 else "")))
cmd = text_message[1:].strip().split(' ')[0]
@@ -42,7 +42,7 @@ def process_command(session_name: str, text_message: str, mgr, config: dict,
return reply
except Exception as e:
mgr.notify_admin("{}令执行失败:{}".format(session_name, e))
mgr.notify_admin("{}令执行失败:{}".format(session_name, e))
logging.exception(e)
reply = ["[bot]err:{}".format(e)]

View File

@@ -84,7 +84,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
processing.append(session_name)
try:
msg_type = ''
if text_message.startswith('!') or text_message.startswith(""): #
if text_message.startswith('!') or text_message.startswith(""): #
msg_type = 'command'
# 触发插件事件
args = {

View File

@@ -28,7 +28,7 @@ def reload_all(notify=True):
import main
main.stop()
# 删除所有已注册的
# 删除所有已注册的
import pkg.qqbot.cmds.aamgr as cmdsmgr
cmdsmgr.__command_list__ = {}
cmdsmgr.__tree_index__ = {}