-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from qzqzcsclub/dev
Version 0.1.0.beta1
- Loading branch information
Showing
32 changed files
with
4,842 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ENVIRONMENT=prod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 基础配置 | ||
|
||
# 超级管理员QQ账号 | ||
# 可设置多个,如:SUPERUSERS=["1111111111","2222222222"] | ||
SUPERUSERS=[""] | ||
|
||
# 机器人命令前缀 | ||
COMMAND_START=[""] | ||
|
||
# 机器人昵称,可设置多个 | ||
NICKNAME=["表白墙", "墙墙" ] | ||
|
||
# 服务器和端口 | ||
# 若默认的8080端口被占用可自行修改端口 | ||
HOST = 127.0.0.1 | ||
PORT = 8080 | ||
|
||
SESSION_EXPIRE_TIMEOUT=30 | ||
|
||
# 日志登记 | ||
LOG_LEVEL=INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/data | ||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"python.languageServer": "Pylance", | ||
"python.analysis.typeCheckingMode": "basic", | ||
"python.linting.enabled": false, | ||
"[python]": { | ||
"editor.defaultFormatter": "ms-python.autopep8" | ||
}, | ||
"python.formatting.provider": "none" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__: v0.1.0.beta1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from pathlib import Path | ||
|
||
import nonebot | ||
import nonebot.adapters | ||
|
||
nonebot.adapters.__path__.append( | ||
str((Path(__file__).parent / "adapters").resolve()) | ||
) | ||
|
||
|
||
from nonebot.adapters.onebot.v11 import Adapter as ONEBOT_V11Adapter | ||
from nonebot.adapters.qzone import Adapter as QzoneAdapter | ||
|
||
nonebot.init( | ||
driver="~fastapi+~httpx", | ||
qzone_bot_id="qzone_bot", | ||
qzone_cache_path=Path(__file__).parent / "cache", | ||
) | ||
|
||
driver = nonebot.get_driver() | ||
driver.register_adapter(ONEBOT_V11Adapter) | ||
driver.register_adapter(QzoneAdapter) | ||
|
||
nonebot.load_from_toml("pyproject.toml") | ||
|
||
if __name__ == "__main__": | ||
nonebot.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from nonebot import on_command, get_driver, logger | ||
from nonebot.adapters.onebot.v11 import Bot, Event | ||
from nonebot.permission import SUPERUSER | ||
from nonebot.params import ArgStr | ||
from nonebot.rule import to_me | ||
|
||
import platform | ||
import subprocess | ||
import os | ||
from pathlib import Path | ||
|
||
driver = get_driver() | ||
|
||
|
||
restart = on_command( | ||
"重启", | ||
permission=SUPERUSER, | ||
rule=to_me(), | ||
priority=1, | ||
block=True, | ||
) | ||
|
||
|
||
@restart.got("flag", prompt=f"确定是否重启机器人?确定请回复[是|好|确定](重启失败咱们将失去联系,请谨慎!)") | ||
async def _(event: Event, flag: str = ArgStr("flag")): | ||
if flag.lower() in ["true", "是", "好", "确定", "确定是"]: | ||
await restart.send("开始重启机器人..请稍等...") | ||
await bot_restart(event) | ||
else: | ||
await restart.send("已取消操作...") | ||
|
||
|
||
async def bot_restart(event=None): | ||
with open("is_restart", "w", encoding="utf-8") as f: | ||
if event: | ||
f.write(event.get_session_id()) | ||
if str(platform.system()).lower() == "windows": | ||
subprocess.run(["restart.bat"], cwd=Path()) | ||
else: | ||
subprocess.run(["sudo", "./restart.sh"], cwd=Path()) | ||
|
||
|
||
@driver.on_bot_connect | ||
async def restart_handle(bot: Bot): | ||
''' | ||
机器人连接时自动生成重启文件 | ||
''' | ||
if str(platform.system()).lower() == "windows": | ||
restart = Path() / "restart.bat" | ||
port = str(bot.config.port) | ||
script = f''' | ||
@echo off | ||
set PORT={port} | ||
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%PORT%"') do ( | ||
taskkill /PID %%a /F | ||
goto :RunPoetry | ||
) | ||
:RunPoetry | ||
call poetry run nb run | ||
''' | ||
with open(restart, "w", encoding="utf-8") as f: | ||
f.write(script) | ||
logger.info("已自动生成 restart.bat(重启) 文件,请检查脚本是否与本地指令符合") | ||
else: | ||
restart = Path() / "restart.sh" | ||
port = str(bot.config.port) | ||
script = f''' | ||
pid=$(netstat -tunlp | grep {port} | awk '{{print $7}}') | ||
pid=${{pid%/*}} | ||
kill -9 $pid | ||
sleep 3 | ||
poetry run nb run | ||
''' | ||
with open(restart, "w", encoding="utf-8") as f: | ||
f.write(script) | ||
os.system("chmod +x ./restart.sh") | ||
logger.info("已自动生成 restart.sh(重启) 文件,请检查脚本是否与本地指令符合") | ||
is_restart_file = Path() / "is_restart" | ||
if is_restart_file.exists(): | ||
with open(is_restart_file, "r", encoding="utf-8") as f: | ||
user_id=f.read() | ||
if user_id: | ||
await bot.send_private_msg( | ||
user_id=int(user_id), | ||
message="机器人重启完毕", | ||
) | ||
is_restart_file.unlink() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from nonebot import get_bot, logger, on_command, require | ||
from nonebot.adapters.onebot.v11 import Bot, Event | ||
from nonebot.permission import SUPERUSER | ||
from nonebot.rule import to_me | ||
|
||
require("nonebot_plugin_apscheduler") | ||
|
||
from nonebot_plugin_apscheduler import scheduler | ||
|
||
from utils.config import Config | ||
from plugins.basic_plugins.restart import bot_restart | ||
|
||
from .source import check_update | ||
|
||
update_check = on_command( | ||
"检查更新", | ||
permission=SUPERUSER, | ||
rule=to_me(), | ||
priority=1, | ||
block=True | ||
) | ||
|
||
|
||
@update_check.handle() | ||
async def _(bot: Bot, event: Event): | ||
try: | ||
status_update, error_info = await check_update(bot) | ||
except Exception as e: | ||
logger.error(f"检查并更新机器人错误 {type(e)}: {e}") | ||
await update_check.finish(f"检查并更新机器人错误 {type(e)}: {e}") | ||
else: | ||
if status_update == 200: | ||
if Config.get_value("bot_update", "auto_restart"): | ||
logger.info("更新完毕,开始重启机器人") | ||
await update_check.send("更新完毕,开始自动重启机器人") | ||
await bot_restart(event) | ||
else: | ||
logger.info("更新完毕,等待重启") | ||
await update_check.send("更新完毕,请重启机器人\n重启命令: 重启") | ||
elif status_update == 999: | ||
logger.info(error_info) | ||
await update_check.finish(error_info) | ||
elif status_update: | ||
logger.error(f"检查并更新机器人错误,错误信息: {error_info}") | ||
await update_check.finish(f"检查并更新机器人错误\n错误信息:\n{error_info}") | ||
|
||
|
||
@scheduler.scheduled_job( | ||
"cron", | ||
hour=12, | ||
minute=0, | ||
) | ||
async def _(): | ||
if Config.get_value("bot_update", "auto_update"): | ||
qq_id = Config.get_value("bot_info", "command_qq_id") | ||
bot = get_bot(qq_id) | ||
try: | ||
status_update, error_info = await check_update(bot) | ||
except Exception as e: | ||
logger.error(f"检查并更新机器人错误 {type(e)}: {e}") | ||
for superuser in list(bot.config.superusers): | ||
await bot.send_private_msg( | ||
user_id=int(superuser), | ||
message=f"检查并更新机器人错误 {type(e)}: {e}" | ||
) | ||
else: | ||
if status_update == 200: | ||
if Config.get_value("bot_update", "auto_restart"): | ||
logger.info("更新完毕,开始重启机器人") | ||
for superuser in list(bot.config.superusers): | ||
await bot.send_private_msg( | ||
user_id=int(superuser), | ||
message="更新完毕,开始自动重启机器人" | ||
) | ||
await bot_restart() | ||
else: | ||
logger.info("更新完毕,等待重启") | ||
for superuser in list(bot.config.superusers): | ||
await bot.send_private_msg( | ||
user_id=int(superuser), | ||
message="更新完毕,请重启机器人\n重启命令: 重启" | ||
) | ||
elif status_update == 999: | ||
logger.info(error_info) | ||
elif status_update: | ||
logger.error(f"检查并更新机器人错误,错误信息: {error_info}") | ||
for superuser in list(bot.config.superusers): | ||
await bot.send_private_msg( | ||
user_id=int(superuser), | ||
message=f"检查并更新机器人错误\n错误信息:\n{error_info}" | ||
) |
Oops, something went wrong.