sycgram/plugins/update.py

122 lines
4.3 KiB
Python
Raw Normal View History

2022-04-08 06:53:17 +00:00
import asyncio
import sys
2022-04-10 02:24:44 +00:00
from subprocess import PIPE, Popen
2022-04-08 06:53:17 +00:00
from core import command
2022-04-08 14:01:22 +00:00
from loguru import logger
2022-04-08 06:53:17 +00:00
from pyrogram import Client
from pyrogram.types import Message
2022-04-09 12:44:01 +00:00
from tools.constants import (SYCGRAM, SYCGRAM_ERROR, SYCGRAM_INFO,
SYCGRAM_WARNING, UPDATE_CMD)
2022-04-10 02:24:44 +00:00
from tools.helpers import Parameters, show_cmd_tip, show_exception
2022-04-08 14:31:42 +00:00
from tools.updates import (get_alias_of_cmds, pull_and_update_command_yml,
reset_cmd_alias, update_cmd_alias,
update_cmd_prefix)
2022-04-08 06:53:17 +00:00
@Client.on_message(command("restart"))
async def restart(_: Client, msg: Message):
"""重启容器"""
2022-04-08 14:31:42 +00:00
text = f"**{SYCGRAM_INFO}**\n> # `Restarting {SYCGRAM} ...`"
2022-04-08 06:53:17 +00:00
await msg.edit_text(text=text, parse_mode='md')
sys.exit()
@Client.on_message(command("update"))
async def update(_: Client, msg: Message):
"""更新sycgram到主分支的最新版本"""
2022-04-08 14:31:42 +00:00
text = f"**{SYCGRAM_INFO}**\n> # `It's updating {SYCGRAM} ...`"
2022-04-08 06:53:17 +00:00
await msg.edit_text(text, parse_mode='md')
try:
2022-04-08 14:31:42 +00:00
await pull_and_update_command_yml()
2022-04-10 02:24:44 +00:00
p = Popen(UPDATE_CMD, stdout=PIPE, shell=True)
p.communicate()
2022-04-08 06:53:17 +00:00
except asyncio.exceptions.TimeoutError:
text = f"**{SYCGRAM_WARNING}**\n> # `Update Timeout`"
except Exception as e:
text = f"**{SYCGRAM_ERROR}**\n> # `{e}`"
else:
2022-04-08 14:31:42 +00:00
text = f"**{SYCGRAM_INFO}**\n> # `{SYCGRAM.title()} is already the latest version.`"
2022-04-08 06:53:17 +00:00
finally:
await msg.edit_text(text, parse_mode='md')
2022-04-08 14:01:22 +00:00
@Client.on_message(command("prefix"))
async def prefix(_: Client, msg: Message):
"""更改所有指令的前缀"""
_, pfx = Parameters.get(msg)
punctuation = list("""!#$%&*+,-./:;=?@^~!?。,;·\\""")
2022-04-09 10:04:08 +00:00
if pfx == "reset":
try:
2022-04-09 12:44:01 +00:00
await pull_and_update_command_yml(is_update=False)
2022-04-09 10:04:08 +00:00
except Exception as e:
return await show_exception(msg, e)
else:
2022-04-09 12:44:01 +00:00
await msg.edit_text("✅ Restore command.yml to default.")
2022-04-09 10:04:08 +00:00
sys.exit()
elif len(pfx) == 0 or len(pfx) > 1 or pfx not in punctuation:
2022-04-08 14:01:22 +00:00
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 <set> <source> <to> or -alias <reset> <source> or -alias <list>
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:
2022-04-09 05:47:04 +00:00
data = get_alias_of_cmds()
tmp = ''.join(f"`{k}` | `{v}`\n" for k, v in data.items())
text = f"**⭐️ 指令别名:**\n**源名** | **别名**\n{tmp}"
2022-04-08 14:01:22 +00:00
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:
2022-04-09 05:47:04 +00:00
await show_cmd_tip(msg, cmd)