diff --git a/plugins/update.py b/plugins/update.py index bc1dba8..0756204 100644 --- a/plugins/update.py +++ b/plugins/update.py @@ -2,16 +2,19 @@ import asyncio import sys from core import command +from loguru import logger from pyrogram import Client from pyrogram.types import Message -from tools.constants import SYCGRAM, SYCGRAM_ERROR, SYCGRAM_INFO, SYCGRAM_WARNING, UPDATE_CMD -from tools.helpers import basher +from tools.constants import (SYCGRAM, SYCGRAM_ERROR, SYCGRAM_INFO, + SYCGRAM_WARNING, UPDATE_CMD) +from tools.helpers import Parameters, basher, get_cmd_error +from tools.updates import get_alias_of_cmds, reset_cmd_alias, update_cmd_alias, update_cmd_prefix @Client.on_message(command("restart")) async def restart(_: Client, msg: Message): """重启容器""" - text = f"**{SYCGRAM_INFO}**\n> # `The {SYCGRAM} is restarting......`" + text = f"**{SYCGRAM_INFO}**\n> # `Restarting {SYCGRAM} ......`" await msg.edit_text(text=text, parse_mode='md') sys.exit() @@ -31,3 +34,72 @@ async def update(_: Client, msg: Message): text = f"**{SYCGRAM_INFO}**\n> # `Your {SYCGRAM} version is the latest.`" finally: await msg.edit_text(text, parse_mode='md') + + +@Client.on_message(command("prefix")) +async def prefix(_: Client, msg: Message): + """更改所有指令的前缀""" + _, pfx = Parameters.get(msg) + punctuation = list("""!#$%&*+,-./:;=?@^~!?。,;·\\""") + if len(pfx) == 0 or len(pfx) > 1 or pfx not in punctuation: + text = f"**{SYCGRAM_WARNING}**\n> # `Prefix must be one of {' '.join(punctuation)}`" + await msg.edit_text(text, parse_mode='md') + return + try: + update_cmd_prefix(pfx) + except Exception as e: + text = f"**{SYCGRAM_ERROR}**\n> # `{e}`" + logger.error(e) + await msg.edit_text(text, parse_mode='md') + else: + text = f"**{SYCGRAM_INFO}**\n> # `Restarting {SYCGRAM} prefix of all commands.`" + await msg.edit_text(text, parse_mode='md') + sys.exit() + + +@Client.on_message(command("alias")) +async def alias(_: Client, msg: Message): + """ + cmd: alias + format: -alias or -alias or -alias + usage: 修改指令别名 + """ + cmd, args = Parameters.get_more(msg) + if len(args) == 3 and args[0] == 'set': + _, source, to = args + try: + update_cmd_alias(source, to) + except Exception as e: + text = f"**{SYCGRAM_ERROR}**\n> # `{e}`" + logger.error(e) + await msg.edit_text(text, parse_mode='md') + else: + text = f"**{SYCGRAM_INFO}**\n> # `Updating alias of <{source}> to <{to}> ...`" + await msg.edit_text(text, parse_mode='md') + sys.exit() + + elif len(args) == 2 and args[0] == 'reset': + _, source = args + try: + reset_cmd_alias(source) + except Exception as e: + text = f"**{SYCGRAM_ERROR}**\n> # `{e}`" + logger.error(e) + await msg.edit_text(text, parse_mode='md') + else: + text = f"**{SYCGRAM_INFO}**\n> # `Reset alias of <{source}> ...`" + await msg.edit_text(text, parse_mode='md') + sys.exit() + + elif len(args) == 1 and args[0] == 'list': + try: + text = get_alias_of_cmds() + except Exception as e: + text = f"**{SYCGRAM_ERROR}**\n> # `{e}`" + logger.error(e) + await msg.edit_text(text, parse_mode='md') + else: + await msg.edit_text(text, parse_mode='md') + + else: + await msg.edit_text(get_cmd_error(cmd)) diff --git a/tools/updates.py b/tools/updates.py new file mode 100644 index 0000000..fcab944 --- /dev/null +++ b/tools/updates.py @@ -0,0 +1,78 @@ +import re +from typing import Any, Dict +import yaml + +from tools.constants import COMMAND_YML + + +def update_cmd_yml(cmd_yml: Dict[str, Any]): + with open(COMMAND_YML, 'w', encoding='utf-8') as f: + yaml.dump(cmd_yml, f, allow_unicode=True) + + +def modify_cmd_prefix(pfx: str) -> Dict[str, Any]: + with open(COMMAND_YML, "rb") as f: + cmd_yml: Dict[str, Any] = yaml.full_load(f) + old_pfx = cmd_yml['help']['all_prefixes'] + cmd_yml['help']['all_prefixes'] = pfx + # 读取每个指令的kv + for every_cmd in cmd_yml.values(): + get_cmd = every_cmd['cmd'] + old_cmd = rf"[{old_pfx}]{get_cmd}" + new_cmd = f"{pfx}{get_cmd}" + every_cmd['format'] = re.sub(old_cmd, new_cmd, every_cmd['format']) + + # 返回已修改过所有指令前缀的一个大字典 + return cmd_yml + + +def update_cmd_prefix(pfx: str) -> None: + try: + cmd_yml = modify_cmd_prefix(pfx) + except Exception as e: + raise e + else: + update_cmd_yml(cmd_yml=cmd_yml) + + +def modify_cmd_alias(source: str, new_cmd: str) -> Dict[str, Any]: + with open(COMMAND_YML, "rb") as f: + cmd_yml: Dict[str, Any] = yaml.full_load(f) + if not cmd_yml.get(source): + raise ValueError(f"The {source} Command Not Found") + pfx = cmd_yml.get('help').get('all_prefixes') + old_cmd = cmd_yml[source]['cmd'] + old_fmt = rf"[{pfx}]{old_cmd}" + new_fmt = f"{pfx}{new_cmd}" + + cmd_yml[source]['cmd'] = new_cmd + cmd_yml[source]['format'] = re.sub( + old_fmt, new_fmt, cmd_yml[source]['format']) + return cmd_yml + + +def update_cmd_alias(source: str, new_cmd: str) -> None: + try: + cmd_yml = modify_cmd_alias(source, new_cmd) + except Exception as e: + raise e + else: + update_cmd_yml(cmd_yml=cmd_yml) + + +def reset_cmd_alias(source: str) -> None: + try: + cmd_yml = modify_cmd_alias(source, new_cmd=source) + except Exception as e: + raise e + else: + update_cmd_yml(cmd_yml=cmd_yml) + + +def get_alias_of_cmds() -> str: + with open(COMMAND_YML, "rb") as f: + cmd_yml: Dict[str, Dict[str, str]] = yaml.full_load(f) + tmp = ''.join( + f"`{k}` | `{v.get('cmd')}`\n" for k, v in cmd_yml.items() + ) + return f"**⭐️ 指令别名:**\n**源名** | **别名**\n{tmp}"