mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
refactor: 更改使用装饰器注册命令
This commit is contained in:
parent
e03585ad4d
commit
0b0271a1f4
|
@ -164,7 +164,14 @@ class AbstractCommandNode:
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def register(cls: type, name: str, parent: type = None):
|
def register(
|
||||||
|
parent: type = None,
|
||||||
|
name: str = None,
|
||||||
|
description: str = None,
|
||||||
|
usage: str = None,
|
||||||
|
aliases: list[str] = None,
|
||||||
|
privilege: int = 0
|
||||||
|
):
|
||||||
"""注册指令
|
"""注册指令
|
||||||
|
|
||||||
:param cls: 指令类
|
:param cls: 指令类
|
||||||
|
@ -172,39 +179,53 @@ class AbstractCommandNode:
|
||||||
:param parent: 父指令类
|
:param parent: 父指令类
|
||||||
"""
|
"""
|
||||||
global __command_list__, __tree_index__
|
global __command_list__, __tree_index__
|
||||||
|
|
||||||
|
def wrapper(cls):
|
||||||
|
cls.name = name
|
||||||
|
cls.parent = parent
|
||||||
|
cls.description = description
|
||||||
|
cls.usage = usage
|
||||||
|
cls.aliases = aliases
|
||||||
|
cls.privilege = privilege
|
||||||
|
|
||||||
if parent is None:
|
logging.debug("cls: {}, name: {}, parent: {}".format(cls, name, parent))
|
||||||
# 顶级指令注册
|
|
||||||
__command_list__[name] = {
|
if parent is None:
|
||||||
'description': cls.description,
|
# 顶级指令注册
|
||||||
'usage': cls.usage,
|
__command_list__[name] = {
|
||||||
'aliases': cls.aliases,
|
'description': cls.description,
|
||||||
'privilege': cls.privilege,
|
'usage': cls.usage,
|
||||||
'parent': None,
|
'aliases': cls.aliases,
|
||||||
'cls': cls,
|
'privilege': cls.privilege,
|
||||||
'sub': []
|
'parent': None,
|
||||||
}
|
'cls': cls,
|
||||||
# 更新索引
|
'sub': []
|
||||||
__tree_index__[cls.__module__ + '.' + cls.__name__] = name
|
}
|
||||||
else:
|
# 更新索引
|
||||||
# 获取父节点名称
|
__tree_index__[cls.__module__ + '.' + cls.__name__] = name
|
||||||
path = __tree_index__[parent.__module__ + '.' + parent.__name__]
|
else:
|
||||||
|
# 获取父节点名称
|
||||||
parent_node = __command_list__[path]
|
path = __tree_index__[parent.__module__ + '.' + parent.__name__]
|
||||||
# 链接父子指令
|
|
||||||
__command_list__[path]['sub'].append(name)
|
parent_node = __command_list__[path]
|
||||||
# 注册子指令
|
# 链接父子指令
|
||||||
__command_list__[path + '.' + name] = {
|
__command_list__[path]['sub'].append(name)
|
||||||
'description': cls.description,
|
# 注册子指令
|
||||||
'usage': cls.usage,
|
__command_list__[path + '.' + name] = {
|
||||||
'aliases': cls.aliases,
|
'description': cls.description,
|
||||||
'privilege': cls.privilege,
|
'usage': cls.usage,
|
||||||
'parent': path,
|
'aliases': cls.aliases,
|
||||||
'cls': cls,
|
'privilege': cls.privilege,
|
||||||
'sub': []
|
'parent': path,
|
||||||
}
|
'cls': cls,
|
||||||
# 更新索引
|
'sub': []
|
||||||
__tree_index__[cls.__module__ + '.' + cls.__name__] = path + '.' + name
|
}
|
||||||
|
# 更新索引
|
||||||
|
__tree_index__[cls.__module__ + '.' + cls.__name__] = path + '.' + name
|
||||||
|
|
||||||
|
return cls
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class CommandPrivilegeError(Exception):
|
class CommandPrivilegeError(Exception):
|
||||||
|
@ -235,6 +256,7 @@ def execute(context: Context) -> list:
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
logging.debug('执行指令: {}'.format(path))
|
||||||
node = node[path]
|
node = node[path]
|
||||||
|
|
||||||
# 检查权限
|
# 检查权限
|
||||||
|
@ -251,6 +273,7 @@ def execute(context: Context) -> list:
|
||||||
# 下一个path
|
# 下一个path
|
||||||
path = path + '.' + ctx.crt_command
|
path = path + '.' + ctx.crt_command
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
traceback.print_exc()
|
||||||
raise CommandPrivilegeError('找不到指令: {}'.format(path))
|
raise CommandPrivilegeError('找不到指令: {}'.format(path))
|
||||||
|
|
||||||
|
|
||||||
|
@ -281,10 +304,10 @@ def register_all():
|
||||||
walk(__import__(module.__name__ + '.' + item.name, fromlist=['']), prefix + item.name + '.', path_prefix + item.name + '/')
|
walk(__import__(module.__name__ + '.' + item.name, fromlist=['']), prefix + item.name + '.', path_prefix + item.name + '/')
|
||||||
else:
|
else:
|
||||||
m = __import__(module.__name__ + '.' + item.name, fromlist=[''])
|
m = __import__(module.__name__ + '.' + item.name, fromlist=[''])
|
||||||
for name, cls in inspect.getmembers(m, inspect.isclass):
|
# for name, cls in inspect.getmembers(m, inspect.isclass):
|
||||||
# 检查是否为指令类
|
# # 检查是否为指令类
|
||||||
if cls.__module__ == m.__name__ and issubclass(cls, AbstractCommandNode) and cls != AbstractCommandNode:
|
# if cls.__module__ == m.__name__ and issubclass(cls, AbstractCommandNode) and cls != AbstractCommandNode:
|
||||||
cls.register(cls, cls.name, cls.parent)
|
# cls.register(cls, cls.name, cls.parent)
|
||||||
|
|
||||||
walk(pkg.qqbot.cmds, '', '')
|
walk(pkg.qqbot.cmds, '', '')
|
||||||
logging.debug(__command_list__)
|
logging.debug(__command_list__)
|
||||||
|
|
|
@ -3,17 +3,18 @@ from ..mgr import AbstractCommandNode, Context
|
||||||
import pkg.openai.session
|
import pkg.openai.session
|
||||||
import pkg.utils.context
|
import pkg.utils.context
|
||||||
|
|
||||||
|
|
||||||
|
@AbstractCommandNode.register(
|
||||||
|
parent=None,
|
||||||
|
name='reset',
|
||||||
|
description='重置当前会话',
|
||||||
|
usage='!reset',
|
||||||
|
aliases=[],
|
||||||
|
privilege=1
|
||||||
|
)
|
||||||
class ResetCommand(AbstractCommandNode):
|
class ResetCommand(AbstractCommandNode):
|
||||||
parent: type = None
|
|
||||||
|
|
||||||
name = 'reset'
|
|
||||||
description = '重置当前会话'
|
|
||||||
usage = '!reset'
|
|
||||||
aliases = []
|
|
||||||
privilege = 1
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def process(cls, ctx: Context) -> tuple[bool, list, str]:
|
def process(cls, ctx: Context) -> tuple[bool, list]:
|
||||||
params = ctx.params
|
params = ctx.params
|
||||||
session_name = ctx.session_name
|
session_name = ctx.session_name
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user