zhenxun_bot/plugins/update_gocqhttp/__init__.py

67 lines
2.5 KiB
Python
Raw Normal View History

2021-05-20 19:25:51 +08:00
from nonebot import on_command
from nonebot.typing import T_State
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
from .data_source import download_gocq_lasted, upload_gocq_lasted
import os
from nonebot.adapters.cqhttp.permission import GROUP
from services.log import logger
2021-06-30 19:50:55 +08:00
from utils.utils import scheduler, get_bot, UserExistLimiter
2021-05-20 19:25:51 +08:00
from configs.config import UPDATE_GOCQ_GROUP
from pathlib import Path
2021-07-30 21:21:51 +08:00
__plugin_name__ = "更新gocq"
2021-06-15 10:57:08 +08:00
2021-07-30 21:21:51 +08:00
__plugin_usage__ = "用法发送更新gocq指定群 自动检测最新版gocq下载并上传"
2021-06-15 10:57:08 +08:00
2021-07-30 21:21:51 +08:00
path = str((Path() / "resources" / "gocqhttp_file").absolute()) + "/"
2021-05-20 19:25:51 +08:00
lasted_gocqhttp = on_command("更新gocq", permission=GROUP, priority=5, block=True)
_ulmt = UserExistLimiter()
@lasted_gocqhttp.handle()
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
# try:
if event.group_id in UPDATE_GOCQ_GROUP:
2021-07-30 21:21:51 +08:00
await lasted_gocqhttp.send("检测中...")
2021-06-04 18:01:33 +08:00
info = await download_gocq_lasted(path)
2021-07-30 21:21:51 +08:00
if info == "gocqhttp没有更新":
await lasted_gocqhttp.finish("gocqhttp没有更新")
2021-05-20 19:25:51 +08:00
if _ulmt.check(event.group_id):
2021-07-30 21:21:51 +08:00
await lasted_gocqhttp.finish("gocqhttp正在更新请勿重复使用该命令", at_sender=True)
2021-05-20 19:25:51 +08:00
_ulmt.set_True(event.group_id)
try:
for file in os.listdir(path):
2021-06-04 18:01:33 +08:00
await upload_gocq_lasted(path, file, event.group_id)
2021-07-30 21:21:51 +08:00
logger.info(f"更新了cqhttp...{file}")
await lasted_gocqhttp.send(f"gocqhttp更新了已上传成功\n更新内容:\n{info}")
2021-05-20 19:25:51 +08:00
except Exception as e:
2021-07-30 21:21:51 +08:00
logger.error(f"更新gocq错误 e{e}")
2021-05-20 19:25:51 +08:00
_ulmt.set_False(event.group_id)
# 更新gocq
@scheduler.scheduled_job(
2021-07-30 21:21:51 +08:00
"cron",
2021-05-20 19:25:51 +08:00
hour=3,
minute=1,
)
async def _():
if UPDATE_GOCQ_GROUP:
bot = get_bot()
try:
2021-06-04 18:01:33 +08:00
info = await download_gocq_lasted(path)
2021-07-30 21:21:51 +08:00
if info == "gocqhttp没有更新":
logger.info("gocqhttp没有更新")
2021-05-20 19:25:51 +08:00
return
for group in UPDATE_GOCQ_GROUP:
for file in os.listdir(path):
2021-06-04 18:01:33 +08:00
await upload_gocq_lasted(path, file, group)
2021-07-30 21:21:51 +08:00
await bot.send_group_msg(
group_id=group, message=f"gocqhttp更新了已上传成功\n更新内容:\n{info}"
)
2021-05-20 19:25:51 +08:00
except Exception as e:
2021-07-30 21:21:51 +08:00
logger.error(f"自动更新gocq出错 e:{e}")