Skip to content

Commit

Permalink
Merge pull request #1 from qzqzcsclub/dev
Browse files Browse the repository at this point in the history
Version 0.1.0.beta1
  • Loading branch information
itsevin authored Nov 12, 2023
2 parents fab482a + 5353f37 commit 46c4e0d
Show file tree
Hide file tree
Showing 32 changed files with 4,842 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENVIRONMENT=prod
21 changes: 21 additions & 0 deletions .env.prod
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/data
TODO
9 changes: 9 additions & 0 deletions .vscode/settings.json
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"
}
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<a href="https://github.com/qzqzcsclub/ConfessionBot"><img src="https://avatars.githubusercontent.com/u/114800876?s=400" width="200" height="200" alt="ConfessionBot"></a>
<a href="https://github.com/qzqzcsclub/ConfessionBot"><img src="https://raw.githubusercontent.com/qzqzcsclub/ConfessionBot/dev/logo.png" width="200" height="200" alt="ConfessionBot"></a>
</p>

<div align="center">
Expand Down Expand Up @@ -29,21 +29,34 @@

一款QQ空间表白墙QQ机器人。基于[NoneBot2](https://v2.nonebot.dev/)开发。

> 内测完善中
## 特色

- QQ空间:表白墙建设在QQ空间,方便用户使用
- QQ私聊:使用QQ私聊发布和审核条目
- 审核功能:有多人共同审核功能,拒绝垃圾墙
- 高性能:基于[NoneBot2](https://v2.nonebot.dev/)异步开发
- 隐私保护:匿名帖子隐私性强,审核组无法获取用户信息(数据库留底)
- 权限管理:四级权限,方便管理
- 方便维护:有自动更新等基本功能
- 高性能:基于[NoneBot2](https://v2.nonebot.dev/)异步开发,使用 sql 数据库

## 特别感谢
## 文档

- [Nonebot2](https://github.com/nonebot/nonebot2):本机器人使用的开发框架
- [go-cqhttp](https://github.com/Mrs4s/go-cqhttp):提供稳定的QQ私聊的接口
> 后续推出
## TO DO

- 完善帖子后期处理功能,如举报下架帖子
- 实现web端,多角度呈现帖子

## 贡献

本项目由泉州七中网络安全社团共同开发和维护。
本项目由泉州七中网络安全社团和开源社区共同开发和维护。

如果你有想法、有能力,欢迎:
- [提交 Issue](https://github.com/qzqzcsclub/ConfessionBot/issues)
- [提交 Pull request](https://github.com/qzqzcsclub/ConfessionBot/pulls)

## 声明

Expand All @@ -52,3 +65,18 @@
## 许可证

本项目使用 [MIT](https://github.com/qzqzcsclub/ConfessionBot/blob/main/LICENSE) 作为开源许可证。

## 鸣谢

### 部分依赖

- [Nonebot2](https://github.com/nonebot/nonebot2):本机器人使用的开发框架
- [go-cqhttp](https://github.com/Mrs4s/go-cqhttp):提供稳定的QQ私聊的接口

### 开发者

感谢以下开发者对 ConfessionBot 作出的贡献:

<a href="https://github.com/qzqzcsclub/ConfessionBot/graphs/contributors">
<img src="https://contrib.rocks/image?repo=qzqzcsclub/ConfessionBot&max=1000" />
</a>
1 change: 1 addition & 0 deletions __version__
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__: v0.1.0.beta1
27 changes: 27 additions & 0 deletions bot.py
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()
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions plugins/basic_plugins/restart.py
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()

91 changes: 91 additions & 0 deletions plugins/basic_plugins/update/__init__.py
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}"
)
Loading

0 comments on commit 46c4e0d

Please sign in to comment.