Add method to update local command.yml

This commit is contained in:
iwumingz 2022-04-08 22:31:42 +08:00
parent baf3b8e1dc
commit 49adcdd788
3 changed files with 26 additions and 5 deletions

View File

@ -8,13 +8,15 @@ from pyrogram.types import Message
from tools.constants import (SYCGRAM, SYCGRAM_ERROR, SYCGRAM_INFO, from tools.constants import (SYCGRAM, SYCGRAM_ERROR, SYCGRAM_INFO,
SYCGRAM_WARNING, UPDATE_CMD) SYCGRAM_WARNING, UPDATE_CMD)
from tools.helpers import Parameters, basher, get_cmd_error 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 from tools.updates import (get_alias_of_cmds, pull_and_update_command_yml,
reset_cmd_alias, update_cmd_alias,
update_cmd_prefix)
@Client.on_message(command("restart")) @Client.on_message(command("restart"))
async def restart(_: Client, msg: Message): async def restart(_: Client, msg: Message):
"""重启容器""" """重启容器"""
text = f"**{SYCGRAM_INFO}**\n> # `Restarting {SYCGRAM} ......`" text = f"**{SYCGRAM_INFO}**\n> # `Restarting {SYCGRAM} ...`"
await msg.edit_text(text=text, parse_mode='md') await msg.edit_text(text=text, parse_mode='md')
sys.exit() sys.exit()
@ -22,16 +24,17 @@ async def restart(_: Client, msg: Message):
@Client.on_message(command("update")) @Client.on_message(command("update"))
async def update(_: Client, msg: Message): async def update(_: Client, msg: Message):
"""更新sycgram到主分支的最新版本""" """更新sycgram到主分支的最新版本"""
text = f"**{SYCGRAM_INFO}**\n> # `It's updating container to the latest version......`" text = f"**{SYCGRAM_INFO}**\n> # `It's updating {SYCGRAM} ...`"
await msg.edit_text(text, parse_mode='md') await msg.edit_text(text, parse_mode='md')
try: try:
await pull_and_update_command_yml()
_ = await basher(UPDATE_CMD, timeout=60) _ = await basher(UPDATE_CMD, timeout=60)
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
text = f"**{SYCGRAM_WARNING}**\n> # `Update Timeout`" text = f"**{SYCGRAM_WARNING}**\n> # `Update Timeout`"
except Exception as e: except Exception as e:
text = f"**{SYCGRAM_ERROR}**\n> # `{e}`" text = f"**{SYCGRAM_ERROR}**\n> # `{e}`"
else: else:
text = f"**{SYCGRAM_INFO}**\n> # `Your {SYCGRAM} version is the latest.`" text = f"**{SYCGRAM_INFO}**\n> # `{SYCGRAM.title()} is already the latest version.`"
finally: finally:
await msg.edit_text(text, parse_mode='md') await msg.edit_text(text, parse_mode='md')

View File

@ -6,6 +6,7 @@ SYCGRAM_INFO: str = f"{SYCGRAM.title()} | INFO"
SYCGRAM_ERROR: str = f"{SYCGRAM.title()} | ERROR" SYCGRAM_ERROR: str = f"{SYCGRAM.title()} | ERROR"
SYCGRAM_WARNING: str = f"{SYCGRAM.title()} | WARNING" SYCGRAM_WARNING: str = f"{SYCGRAM.title()} | WARNING"
COMMAND_YML: str = './data/command.yml' COMMAND_YML: str = './data/command.yml'
CMD_YML_REMOTE: str = "https://raw.githubusercontent.com/iwumingz/sycgram/main/data/command.yml"
UPDATE_CMD: str = f""" UPDATE_CMD: str = f"""
docker run --rm \ docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock \ -v /var/run/docker.sock:/var/run/docker.sock \

View File

@ -1,8 +1,10 @@
import re import re
from typing import Any, Dict from typing import Any, Dict
import yaml import yaml
from tools.constants import COMMAND_YML from .constants import CMD_YML_REMOTE, COMMAND_YML
from .sessions import session
def update_cmd_yml(cmd_yml: Dict[str, Any]): def update_cmd_yml(cmd_yml: Dict[str, Any]):
@ -76,3 +78,18 @@ def get_alias_of_cmds() -> str:
f"`{k}` | `{v.get('cmd')}`\n" for k, v in cmd_yml.items() f"`{k}` | `{v.get('cmd')}`\n" for k, v in cmd_yml.items()
) )
return f"**⭐️ 指令别名:**\n**源名** | **别名**\n{tmp}" return f"**⭐️ 指令别名:**\n**源名** | **别名**\n{tmp}"
async def pull_and_update_command_yml() -> None:
# 读取远程command.yml
async with session.get(
CMD_YML_REMOTE, timeout=9.9,
) as resp:
if resp.status == 200:
data = yaml.full_load(await resp.text())
with open(COMMAND_YML, "rb") as f:
cmd_yml: Dict[str, Dict[str, str]] = yaml.full_load(f)
data.update(cmd_yml)
# 合并到本地,以本地为主
update_cmd_yml(data)
resp.raise_for_status()