mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
30bcef32e4 | ||
|
|
33d5cd6b5c | ||
|
|
c574459485 | ||
|
|
ca9933a489 | ||
|
|
429b7e38b5 | ||
|
|
8c7110ae85 | ||
|
|
a43a7b5f75 | ||
|
|
5b9218251e | ||
|
|
461ca0af32 | ||
|
|
97443a11e2 | ||
|
|
5aff21aab2 | ||
|
|
1cbe807b05 | ||
|
|
d8ea0c6baf | ||
|
|
d6ef64c70a | ||
|
|
b31fbf5810 | ||
|
|
f2ab119397 | ||
|
|
33e42be32d | ||
|
|
8c92756493 | ||
|
|
c2ca09867a | ||
|
|
a1eb981fd4 | ||
|
|
e32f086b8a | ||
|
|
bdfdbbcaeb | ||
|
|
3cdabb7d81 | ||
|
|
422ed2ff51 | ||
|
|
6e76ffb60f | ||
|
|
eaa809931c | ||
|
|
4163435c84 | ||
|
|
99263ab36a | ||
|
|
0d76fa8560 | ||
|
|
c3644576b8 | ||
|
|
325e345b4e | ||
|
|
3b7972454d | ||
|
|
6af5e42bf0 | ||
|
|
edc68921c9 | ||
|
|
83c3f44ae7 | ||
|
|
6d7e79e9de | ||
|
|
401029bd27 | ||
|
|
33a0477fab | ||
|
|
6826c10a09 | ||
|
|
8133b61ebd | ||
|
|
898d7b40b3 | ||
|
|
5a9beb2e5c | ||
|
|
a63fda8a0d | ||
|
|
0baf043f74 | ||
|
|
cbc32a79d2 | ||
|
|
df5a68bcec | ||
|
|
1ca6a67a24 | ||
|
|
f27a537f70 | ||
|
|
aa76fae386 | ||
|
|
d35d53825c | ||
|
|
7f743141fb | ||
|
|
fa88a08d8e | ||
|
|
06d0bb1ab2 | ||
|
|
3c0f35b3b7 | ||
|
|
396d9c1a65 | ||
|
|
e41f3a4ce7 | ||
|
|
8f9e4d6e4f | ||
|
|
d0764f0865 | ||
|
|
ab32eedbb9 | ||
|
|
73f858e071 | ||
|
|
73b4a1254c | ||
|
|
fbf82d999f | ||
|
|
1f68481256 | ||
|
|
876cfb421a | ||
|
|
c97f1bd750 | ||
|
|
e4dec9f0de | ||
|
|
282b4c102f | ||
|
|
37e0003810 | ||
|
|
42683e4f09 | ||
|
|
6352ea7c6f | ||
|
|
40c10e672c | ||
|
|
fef85190c5 | ||
|
|
eea1be0fc1 | ||
|
|
633862d121 | ||
|
|
86ac770971 | ||
|
|
7685b95031 | ||
|
|
a6105e8a1b | ||
|
|
9feea3ba1d | ||
|
|
22f415abfa | ||
|
|
c99292d327 | ||
|
|
362ef519b7 | ||
|
|
a053c120c4 | ||
|
|
4d133ed2df | ||
|
|
d2cf18e11d | ||
|
|
5ddfd2b5b9 | ||
|
|
71264bffa1 | ||
|
|
f39005f5e1 | ||
|
|
9454308ba7 | ||
|
|
51dd622e20 | ||
|
|
05de55cc6b | ||
|
|
db441c8406 | ||
|
|
295c535002 | ||
|
|
b0606feff3 | ||
|
|
2070014021 | ||
|
|
def53aa22e | ||
|
|
a3d10385a0 | ||
|
|
cb7527a44b | ||
|
|
32e420efbb | ||
|
|
6db9bef115 | ||
|
|
a0b440efcd | ||
|
|
d2bb7ae493 | ||
|
|
b4e213518b | ||
|
|
e3b0da4717 | ||
|
|
155de5a032 | ||
|
|
57e166940d | ||
|
|
721efb79fd | ||
|
|
5338342208 | ||
|
|
c49ef3052f | ||
|
|
802f4df2ce | ||
|
|
1edcee9ce3 | ||
|
|
3acd7d7d41 | ||
|
|
83dc29a562 | ||
|
|
b25b9ca928 | ||
|
|
0dc11b7edb | ||
|
|
9b477e2eb8 | ||
|
|
272f6726ea | ||
|
|
181717001a | ||
|
|
43b654285f | ||
|
|
50e7d5036e | ||
|
|
b6b2f2e2a6 | ||
|
|
b9d08855d0 | ||
|
|
93deb2b1be | ||
|
|
b3871d550c | ||
|
|
ab8e87899c | ||
|
|
9d6f44849a | ||
|
|
380588c11d | ||
|
|
5810c28294 | ||
|
|
c039015e8f | ||
|
|
0dda0a228a | ||
|
|
940514b16b | ||
|
|
8adff43113 | ||
|
|
b59699dabe | ||
|
|
f496ce4861 | ||
|
|
5fb4eccd0f | ||
|
|
31e4cadecb | ||
|
|
994484f3b0 | ||
|
|
7b3a3451f6 | ||
|
|
14d075bc17 | ||
|
|
75369411b3 | ||
|
|
d97542a3fd | ||
|
|
2f70fcd92a | ||
|
|
182f93d618 | ||
|
|
34e28384dd | ||
|
|
f6f095f949 | ||
|
|
08609bdf53 | ||
|
|
5de5667f80 | ||
|
|
69b3ba47b3 | ||
|
|
11c1d0b827 | ||
|
|
8f97880754 | ||
|
|
ff435bea95 | ||
|
|
cd66b7f5b8 | ||
|
|
a60286c328 | ||
|
|
49e38862af | ||
|
|
75b8cc0766 | ||
|
|
a8e2f00e16 | ||
|
|
d099a85c59 | ||
|
|
55b2a86397 | ||
|
|
d39dfbe62f | ||
|
|
a41e62cefe | ||
|
|
c981562f90 | ||
|
|
e6cd57cc26 | ||
|
|
074152ae14 | ||
|
|
91ca7b40b8 | ||
|
|
5321c8d9c0 | ||
|
|
b7f753ebb6 | ||
|
|
c5e8ba3cdf | ||
|
|
658ef5309b | ||
|
|
823f642225 | ||
|
|
57ea6bad07 | ||
|
|
109c645e68 | ||
|
|
5ef0ce5aa8 | ||
|
|
f6d9e083b8 | ||
|
|
499517e882 | ||
|
|
648db532bb | ||
|
|
1ce4537605 | ||
|
|
701812ed9d | ||
|
|
8d9e81827b | ||
|
|
f51a2816b4 | ||
|
|
3d4a7950c5 | ||
|
|
ba6668f144 | ||
|
|
e1f7a77d15 | ||
|
|
64e2c512d3 | ||
|
|
6082e68a1f | ||
|
|
f135d48e55 | ||
|
|
751fc8fdb4 | ||
|
|
df73e79ee4 | ||
|
|
fa6cfdb609 | ||
|
|
b057a05520 | ||
|
|
cb0938721c | ||
|
|
4f3f8de734 | ||
|
|
5ba303aada | ||
|
|
bd42fefe4e | ||
|
|
d24c081973 | ||
|
|
1b9eddd905 | ||
|
|
0bfe398dc4 | ||
|
|
e9216a472f | ||
|
|
8b93ec0df6 | ||
|
|
7497706a0d | ||
|
|
6e41cb0630 | ||
|
|
62d99e7e90 | ||
|
|
43cddb3580 | ||
|
|
c9c1beda75 | ||
|
|
3ee9ae5fb8 | ||
|
|
ea6344fae1 | ||
|
|
6f54e91f5c | ||
|
|
27fb3aaa19 | ||
|
|
86d736745d | ||
|
|
821fe21c2b | ||
|
|
7f4aa518dc | ||
|
|
8cf589e433 | ||
|
|
105a29cd70 | ||
|
|
cb6a5e81e7 | ||
|
|
9c073383ad | ||
|
|
5e0756825d | ||
|
|
933523ae6d | ||
|
|
ed6b05605c | ||
|
|
71a9c34620 | ||
|
|
e29f2cb920 | ||
|
|
d47dd88d6c | ||
|
|
fb34bbce7a | ||
|
|
100236da83 | ||
|
|
a45a3002d5 | ||
|
|
9214a4c978 | ||
|
|
70761dc222 | ||
|
|
32e8136f71 | ||
|
|
8b20d99b2c | ||
|
|
1174b5cc7f | ||
|
|
ec448cfdfb | ||
|
|
f3e44b1baf | ||
|
|
0ea740aa7c | ||
|
|
5f118471cb | ||
|
|
504f78dd49 | ||
|
|
72723c7d1f | ||
|
|
6a74053853 | ||
|
|
af0b4987a4 | ||
|
|
775140cf79 | ||
|
|
6a8a796add | ||
|
|
8e4bda3760 | ||
|
|
a36e8145a8 | ||
|
|
3cbd899daa | ||
|
|
30f356a2c7 | ||
|
|
1138a115ce | ||
|
|
c397da75e7 | ||
|
|
1d141c9313 | ||
|
|
a4cc365af8 | ||
|
|
4e551bec0d | ||
|
|
e03086062d | ||
|
|
270a5a5e20 | ||
|
|
bda9133ed6 | ||
|
|
749f475f2c | ||
|
|
e38394948c | ||
|
|
51fc4cbb6a | ||
|
|
9a976cda37 | ||
|
|
e42521f0a3 | ||
|
|
0756d39ac5 | ||
|
|
4595bdcee4 | ||
|
|
38160065cc | ||
|
|
3d04f013cd | ||
|
|
8e1a416be3 | ||
|
|
a98bc42f1a | ||
|
|
6c93769838 | ||
|
|
d2dccfebc5 | ||
|
|
82fe59e469 | ||
|
|
afc05e1731 | ||
|
|
0206b0a522 | ||
|
|
0e1e5a5ebe | ||
|
|
9ad088ed6a | ||
|
|
34da04bba5 | ||
|
|
09ce812faf | ||
|
|
1dc8cb2a98 | ||
|
|
e73c509823 | ||
|
|
e70a85400f | ||
|
|
f7aecf8eed | ||
|
|
cc2645aa9d | ||
|
|
52d23c4a8d | ||
|
|
1e57ddb9ee | ||
|
|
7ae883d921 | ||
|
|
27e97214bd | ||
|
|
6321a9e7e0 | ||
|
|
a9eadca46c | ||
|
|
76bef8a725 | ||
|
|
308b76dc7d | ||
|
|
31d4d29f3e | ||
|
|
016a7e87ea | ||
|
|
4ce88c7bfc | ||
|
|
f570bffcf7 | ||
|
|
3bb9aaf637 | ||
|
|
cf7a64c934 | ||
|
|
c466603193 | ||
|
|
bc53f329a9 | ||
|
|
4a5cc5153f | ||
|
|
8697670fe1 | ||
|
|
64d8543af9 | ||
|
|
482cc242cb | ||
|
|
2051e8183c | ||
|
|
d183903917 | ||
|
|
5c695899a1 | ||
|
|
708e912069 | ||
|
|
db1f4b2b52 | ||
|
|
1508782f49 | ||
|
|
445b830683 | ||
|
|
1a17e88142 | ||
|
|
c0ec2e7856 | ||
|
|
9e962bd0d1 | ||
|
|
7efcff13bb | ||
|
|
e70d0df949 | ||
|
|
fcaa469edd | ||
|
|
51508182ef | ||
|
|
27e78c1626 | ||
|
|
0b4d37a0b6 | ||
|
|
ddbebba6f1 | ||
|
|
7f296348e4 | ||
|
|
ca78e32d97 | ||
|
|
4efc561f4d | ||
|
|
40fed798a1 | ||
|
|
0876afc08c | ||
|
|
ba4c4d06ff | ||
|
|
f64c30af48 | ||
|
|
bb0cea2dd3 | ||
|
|
3c120ce602 | ||
|
|
6151dfb161 | ||
|
|
f05d4ec931 | ||
|
|
e16afc3fc2 | ||
|
|
1b8753fead | ||
|
|
ba960d1ced | ||
|
|
41b60b6152 | ||
|
|
70ddce713c | ||
|
|
e15622748b | ||
|
|
96687021ff | ||
|
|
3c0a7ef510 | ||
|
|
4d84c9fb3f | ||
|
|
aa35a16610 | ||
|
|
a380fcb680 | ||
|
|
e544afe475 | ||
|
|
c089388323 | ||
|
|
e97a043031 | ||
|
|
d1b6dfe577 | ||
|
|
9189fd411e | ||
|
|
432eba80a9 | ||
|
|
db39612c54 | ||
|
|
5794c82a2d | ||
|
|
acf875a886 | ||
|
|
79185bd90f | ||
|
|
d695c30988 | ||
|
|
a1308fbdc9 | ||
|
|
e4f2b761d6 | ||
|
|
6ff6009a70 | ||
|
|
dadeb4006e | ||
|
|
ee0ff762e5 | ||
|
|
1f01ba1d10 | ||
|
|
62965fe1df | ||
|
|
e29eb4cce5 | ||
|
|
699791e318 | ||
|
|
946fbbec3c | ||
|
|
d2aae0103d | ||
|
|
ab06e38b7c | ||
|
|
bd531c85b1 | ||
|
|
8a0ca3c2e5 | ||
|
|
97317ed2c6 | ||
|
|
cda9aae8a3 | ||
|
|
a33e785043 | ||
|
|
833e266a1f | ||
|
|
1bb9b2cb3f | ||
|
|
7ba129b3bf | ||
|
|
1e2c9ca28a | ||
|
|
c6f37fd05f | ||
|
|
b6b569fff9 | ||
|
|
ac57c016a6 | ||
|
|
28fdd9de95 | ||
|
|
1ec946aaa9 | ||
|
|
d6110e3c6f | ||
|
|
4c5302bd4c | ||
|
|
21ee3d5c3e | ||
|
|
e5c036cee8 | ||
|
|
6f82488af7 | ||
|
|
57a7903c63 | ||
|
|
27b89b333a | ||
|
|
ce2c4e7492 | ||
|
|
cad98a2a33 | ||
|
|
40da48e685 | ||
|
|
1c393e5c3e | ||
|
|
667c143c74 | ||
|
|
bc089c18ce | ||
|
|
24b730c0e2 | ||
|
|
9ed9e9b973 | ||
|
|
5b0027aa3d | ||
|
|
810c5e4e8b | ||
|
|
907534958f | ||
|
|
f08a18ab07 | ||
|
|
40549d5ca7 | ||
|
|
3e67edc506 | ||
|
|
9c56a81f65 | ||
|
|
a9a636c9ae | ||
|
|
98374dcd74 | ||
|
|
10ca6eec91 | ||
|
|
216d32cf21 | ||
|
|
248fae4fe6 | ||
|
|
b479b6839d | ||
|
|
1a4e1e9515 | ||
|
|
e5160a4ee8 | ||
|
|
56ea3964a7 | ||
|
|
ad73778286 | ||
|
|
3e548f48c8 | ||
|
|
ae466931fa | ||
|
|
68052b526f | ||
|
|
903b5b88a7 | ||
|
|
007823d6b2 | ||
|
|
db5c4fb349 | ||
|
|
c37901290a | ||
|
|
da67f1f6f8 | ||
|
|
5172fa69e5 | ||
|
|
dd7280cd0d | ||
|
|
0d0f522c88 | ||
|
|
5429e6ed5c | ||
|
|
9db69fa05b | ||
|
|
3b8d52bcb5 | ||
|
|
807f39a336 | ||
|
|
fea2f30461 | ||
|
|
5d97e3f95f | ||
|
|
63e6ca3e99 | ||
|
|
b11590ec3c | ||
|
|
3a43ed6919 | ||
|
|
15ce9c91fb | ||
|
|
0e8d7537d4 | ||
|
|
1fca0787be | ||
|
|
af30285c00 | ||
|
|
a84cab7afd | ||
|
|
593573ddfb | ||
|
|
e9e9f81bdb | ||
|
|
61f624841b | ||
|
|
4bb38a2730 | ||
|
|
d4ec0df00c | ||
|
|
c8a7d855f8 | ||
|
|
73a126dcd5 | ||
|
|
0736533389 | ||
|
|
2695d658ab | ||
|
|
947166e793 | ||
|
|
80c49a7cf5 | ||
|
|
55bfa8f669 | ||
|
|
74ade14f5e | ||
|
|
c9b5218ebc | ||
|
|
b324b2bd65 | ||
|
|
35c038efa2 | ||
|
|
caa404d58d | ||
|
|
830103b9d5 | ||
|
|
6e18379597 | ||
|
|
6effbacb28 | ||
|
|
a20a1a0234 | ||
|
|
3667724061 | ||
|
|
785179e4a7 | ||
|
|
715338f821 | ||
|
|
7d5a82ab6d | ||
|
|
e764cfcc1c | ||
|
|
363acf37bc | ||
|
|
5fd687c1e4 | ||
|
|
dbedcbb93d | ||
|
|
9e9611f11f | ||
|
|
44002838da | ||
|
|
4daab8459f | ||
|
|
5a002f75cc | ||
|
|
a2180a1365 | ||
|
|
7b836acf36 | ||
|
|
a112a561cc | ||
|
|
6dcc2a69f4 |
@@ -1,38 +0,0 @@
|
||||
name: Generate poetry.lock
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "main"
|
||||
paths:
|
||||
- 'pyproject.toml'
|
||||
|
||||
jobs:
|
||||
poetry_lock:
|
||||
name: Generate
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: Set up Python
|
||||
uses: he0119/setup-python@main
|
||||
with:
|
||||
python-version: "3.9"
|
||||
- name: poetry lock
|
||||
run: |
|
||||
rm -f poetry.lock
|
||||
sed -i.bak "8,11d" pyproject.toml
|
||||
poetry lock
|
||||
mv pyproject.toml.bak pyproject.toml
|
||||
# commit
|
||||
- name: commit file
|
||||
run: |
|
||||
git config --local user.email "github-actions[bot]@users.noreply.github.com"
|
||||
git config --local user.name "github-actions[bot]"
|
||||
git add poetry.lock
|
||||
git commit -m "Auto generate poetry.lock" -a
|
||||
# push
|
||||
- name: push changes
|
||||
uses: ad-m/github-push-action@master
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
+23
@@ -138,9 +138,32 @@ dmypy.json
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
demo.py
|
||||
test.py
|
||||
server_ip.py
|
||||
member_activity_handle.py
|
||||
Yu-Gi-Oh/
|
||||
csgo/
|
||||
fantasy_card/
|
||||
data/
|
||||
log/
|
||||
backup/
|
||||
extensive_plugin/
|
||||
test/
|
||||
bot.py
|
||||
data/
|
||||
.env
|
||||
.env.dev
|
||||
/resources/text/
|
||||
/resources/image/
|
||||
/resources/temp/
|
||||
!/resources/template/
|
||||
/configs/config.py
|
||||
configs/config.yaml
|
||||
./.env
|
||||
./.env.dev
|
||||
plugins/csgo_server/
|
||||
plugins/activity/
|
||||
!/resources/image/genshin/alc/back.png
|
||||
!/data/genshin_alc/
|
||||
.vscode/launch.json
|
||||
+1
-1
@@ -1 +1 @@
|
||||
__version__: v0.1.5.9
|
||||
__version__: v0.1.6.7
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from configs.config import Config
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
|
||||
from configs.config import Config
|
||||
|
||||
Config.add_plugin_config(
|
||||
"admin_bot_manage:custom_welcome_message",
|
||||
@@ -9,6 +11,7 @@ Config.add_plugin_config(
|
||||
name="群管理员操作",
|
||||
help_="设置群欢迎消息权限",
|
||||
default_value=2,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -17,6 +20,7 @@ Config.add_plugin_config(
|
||||
2,
|
||||
help_="开关群功能权限",
|
||||
default_value=2,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -24,7 +28,8 @@ Config.add_plugin_config(
|
||||
"ADMIN_DEFAULT_AUTH",
|
||||
5,
|
||||
help_="默认群管理员权限",
|
||||
default_value=5
|
||||
default_value=5,
|
||||
type=int,
|
||||
)
|
||||
|
||||
nonebot.load_plugins("basic_plugins/admin_bot_manage")
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
|
||||
@@ -1,68 +1,95 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import ujson as json
|
||||
from nonebot.adapters.onebot.v11.message import MessageSegment
|
||||
|
||||
from configs.config import Config
|
||||
from configs.path_config import DATA_PATH, IMAGE_PATH
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from models.level_user import LevelUser
|
||||
from services.log import logger
|
||||
from configs.path_config import DATA_PATH
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from utils.image_utils import BuildImage
|
||||
from utils.manager import group_manager, plugins2settings_manager, plugins_manager
|
||||
from utils.message_builder import image
|
||||
from utils.utils import get_bot, get_matchers
|
||||
from pathlib import Path
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from datetime import datetime
|
||||
from services.db_context import db
|
||||
from models.level_user import LevelUser
|
||||
from configs.config import Config
|
||||
from utils.manager import group_manager, plugins2settings_manager, plugins_manager
|
||||
from utils.image_utils import BuildImage
|
||||
from utils.http_utils import AsyncHttpx
|
||||
import asyncio
|
||||
import time
|
||||
import os
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
|
||||
async def group_current_status(group_id: int) -> str:
|
||||
"""
|
||||
获取当前所有通知的开关
|
||||
:param group_id: 群号
|
||||
"""
|
||||
rst = "[被动技能 状态]\n"
|
||||
_data = group_manager.get_task_data()
|
||||
for task in _data.keys():
|
||||
rst += f'{_data[task]}: {"√" if await group_manager.check_group_task_status(group_id, task) else "×"}\n'
|
||||
return rst.strip()
|
||||
|
||||
|
||||
custom_welcome_msg_json = (
|
||||
Path() / "data" / "custom_welcome_msg" / "custom_welcome_msg.json"
|
||||
)
|
||||
|
||||
ICON_PATH = IMAGE_PATH / "other"
|
||||
|
||||
|
||||
async def group_current_status(group_id: int) -> str:
|
||||
"""
|
||||
说明:
|
||||
获取当前群聊所有通知的开关
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
_data = group_manager.get_task_data()
|
||||
image_list = []
|
||||
for i, task in enumerate(_data):
|
||||
name = _data[task]
|
||||
name_image = BuildImage(0, 0, plain_text=f"{i+1}.{name}", font_size=20)
|
||||
bk = BuildImage(
|
||||
name_image.w + 200, name_image.h + 20, color=(103, 177, 109), font_size=15
|
||||
)
|
||||
await bk.apaste(name_image, (10, 0), True, "by_height")
|
||||
a_icon = BuildImage(40, 40, background=ICON_PATH / "btn_false.png")
|
||||
if group_manager.check_group_task_status(group_id, task):
|
||||
a_icon = BuildImage(40, 40, background=ICON_PATH / "btn_true.png")
|
||||
b_icon = BuildImage(40, 40, background=ICON_PATH / "btn_false.png")
|
||||
if group_manager.check_task_super_status(task):
|
||||
b_icon = BuildImage(40, 40, background=ICON_PATH / "btn_true.png")
|
||||
await bk.atext((name_image.w + 20, 10), "状态")
|
||||
await bk.apaste(a_icon, (name_image.w + 50, 0), True)
|
||||
await bk.atext((name_image.w + 100, 10), "全局")
|
||||
await bk.apaste(b_icon, (name_image.w + 130, 0), True)
|
||||
image_list.append(bk)
|
||||
w = max([x.w for x in image_list])
|
||||
h = sum([x.h + 10 for x in image_list])
|
||||
A = BuildImage(w + 20, h + 70, font_size=30, color=(119, 97, 177))
|
||||
await A.atext((15, 20), "群被动状态")
|
||||
curr_h = 75
|
||||
for img in image_list:
|
||||
# await img.acircle_corner()
|
||||
await A.apaste(img, (0, curr_h), True)
|
||||
curr_h += img.h + 10
|
||||
return A.pic2bs4()
|
||||
|
||||
|
||||
async def custom_group_welcome(
|
||||
msg: str, imgs: List[str], user_id: int, group_id: int
|
||||
msg: str, img_list: List[str], user_id: int, group_id: int
|
||||
) -> str:
|
||||
"""
|
||||
替换群欢迎消息
|
||||
:param msg: 欢迎消息文本
|
||||
:param imgs: 欢迎消息图片,只取第一张
|
||||
:param user_id: 用户id,用于log记录
|
||||
:param group_id: 群号
|
||||
说明:
|
||||
替换群欢迎消息
|
||||
参数:
|
||||
:param msg: 欢迎消息文本
|
||||
:param img_list: 欢迎消息图片,只取第一张
|
||||
:param user_id: 用户id,用于log记录
|
||||
:param group_id: 群号
|
||||
"""
|
||||
img_result = ""
|
||||
img = imgs[0] if imgs else ""
|
||||
result = ""
|
||||
img = img_list[0] if img_list else ""
|
||||
if (DATA_PATH / f"custom_welcome_msg/{group_id}.jpg").exists():
|
||||
(DATA_PATH / f"custom_welcome_msg/{group_id}.jpg").unlink()
|
||||
data = {}
|
||||
if not custom_welcome_msg_json.exists():
|
||||
custom_welcome_msg_json.parent.mkdir(parents=True, exist_ok=True)
|
||||
data = {}
|
||||
else:
|
||||
try:
|
||||
data = json.load(open(custom_welcome_msg_json, "r"))
|
||||
except FileNotFoundError:
|
||||
data = {}
|
||||
pass
|
||||
try:
|
||||
if msg:
|
||||
data[str(group_id)] = str(msg)
|
||||
@@ -86,14 +113,48 @@ async def custom_group_welcome(
|
||||
task_data = None
|
||||
|
||||
|
||||
async def change_group_switch(cmd: str, group_id: int, is_super: bool = False):
|
||||
def change_global_task_status(cmd: str) -> str:
|
||||
"""
|
||||
说明:
|
||||
修改全局被动任务状态
|
||||
参数:
|
||||
:param cmd: 功能名称
|
||||
"""
|
||||
global task_data
|
||||
if not task_data:
|
||||
task_data = group_manager.get_task_data()
|
||||
status = cmd[:2]
|
||||
_cmd = cmd[4:]
|
||||
if "全部被动" in cmd:
|
||||
for task in task_data:
|
||||
if status == "开启":
|
||||
group_manager.open_global_task(task)
|
||||
else:
|
||||
group_manager.close_global_task(task)
|
||||
group_manager.save()
|
||||
return f"已 {status} 全局全部被动技能!"
|
||||
else:
|
||||
modules = [x for x in task_data if task_data[x].lower() == _cmd.lower()]
|
||||
if not modules:
|
||||
return "未查询到该被动任务"
|
||||
if status == "开启":
|
||||
group_manager.open_global_task(modules[0])
|
||||
else:
|
||||
group_manager.close_global_task(modules[0])
|
||||
group_manager.save()
|
||||
return f"已 {status} 全局{_cmd}"
|
||||
|
||||
|
||||
async def change_group_switch(cmd: str, group_id: int, is_super: bool = False) -> str:
|
||||
"""
|
||||
修改群功能状态
|
||||
:param cmd: 功能名称
|
||||
:param group_id: 群号
|
||||
:param is_super: 是否位超级用户,超级用户用于私聊开关功能状态
|
||||
说明:
|
||||
修改群功能状态
|
||||
参数:
|
||||
:param cmd: 功能名称
|
||||
:param group_id: 群号
|
||||
:param is_super: 是否位超级用户,超级用户用于私聊开关功能状态
|
||||
"""
|
||||
global task_data
|
||||
if not task_data:
|
||||
task_data = group_manager.get_task_data()
|
||||
group_help_file = DATA_PATH / "group_help" / f"{group_id}.png"
|
||||
@@ -104,43 +165,44 @@ async def change_group_switch(cmd: str, group_id: int, is_super: bool = False):
|
||||
if cmd == "全部被动":
|
||||
for task in task_data:
|
||||
if status == "开启":
|
||||
if not await group_manager.check_group_task_status(group_id, task):
|
||||
await group_manager.open_group_task(group_id, task)
|
||||
if not group_manager.check_group_task_status(group_id, task):
|
||||
group_manager.open_group_task(group_id, task)
|
||||
else:
|
||||
if await group_manager.check_group_task_status(group_id, task):
|
||||
await group_manager.close_group_task(group_id, task)
|
||||
if group_manager.check_group_task_status(group_id, task):
|
||||
group_manager.close_group_task(group_id, task)
|
||||
if group_help_file.exists():
|
||||
group_help_file.unlink()
|
||||
return f"已 {status} 全部被动技能!"
|
||||
if cmd == "全部功能":
|
||||
for f in plugins2settings_manager.get_data():
|
||||
if status == "开启":
|
||||
group_manager.unblock_plugin(f, group_id)
|
||||
group_manager.unblock_plugin(f, group_id, False)
|
||||
else:
|
||||
group_manager.block_plugin(f, group_id)
|
||||
group_manager.block_plugin(f, group_id, False)
|
||||
group_manager.save()
|
||||
if group_help_file.exists():
|
||||
group_help_file.unlink()
|
||||
return f"已 {status} 全部功能!"
|
||||
if cmd in [task_data[x] for x in task_data.keys()]:
|
||||
if cmd.lower() in [task_data[x].lower() for x in task_data.keys()]:
|
||||
type_ = "task"
|
||||
modules = [x for x in task_data.keys() if task_data[x] == cmd]
|
||||
modules = [x for x in task_data.keys() if task_data[x].lower() == cmd.lower()]
|
||||
for module in modules:
|
||||
if is_super:
|
||||
module = f"{module}:super"
|
||||
if status == "开启":
|
||||
if type_ == "task":
|
||||
if await group_manager.check_group_task_status(group_id, module):
|
||||
if group_manager.check_group_task_status(group_id, module):
|
||||
return f"被动 {task_data[module]} 正处于开启状态!不要重复开启."
|
||||
await group_manager.open_group_task(group_id, module)
|
||||
group_manager.open_group_task(group_id, module)
|
||||
else:
|
||||
if group_manager.get_plugin_status(module, group_id):
|
||||
return f"功能 {cmd} 正处于开启状态!不要重复开启."
|
||||
group_manager.unblock_plugin(module, group_id)
|
||||
else:
|
||||
if type_ == "task":
|
||||
if not await group_manager.check_group_task_status(group_id, module):
|
||||
if not group_manager.check_group_task_status(group_id, module):
|
||||
return f"被动 {task_data[module]} 正处于关闭状态!不要重复关闭."
|
||||
await group_manager.close_group_task(group_id, module)
|
||||
group_manager.close_group_task(group_id, module)
|
||||
else:
|
||||
if not group_manager.get_plugin_status(module, group_id):
|
||||
return f"功能 {cmd} 正处于关闭状态!不要重复关闭."
|
||||
@@ -160,9 +222,11 @@ async def change_group_switch(cmd: str, group_id: int, is_super: bool = False):
|
||||
|
||||
def set_plugin_status(cmd: str, block_type: str = "all"):
|
||||
"""
|
||||
设置插件功能状态(超级用户使用)
|
||||
:param cmd: 功能名称
|
||||
:param block_type: 限制类型, 'all': 私聊+群里, 'private': 私聊, 'group': 群聊
|
||||
说明:
|
||||
设置插件功能状态(超级用户使用)
|
||||
参数:
|
||||
:param cmd: 功能名称
|
||||
:param block_type: 限制类型, 'all': 私聊+群里, 'private': 私聊, 'group': 群聊
|
||||
"""
|
||||
status = cmd[:2]
|
||||
cmd = cmd[2:]
|
||||
@@ -178,14 +242,16 @@ def set_plugin_status(cmd: str, block_type: str = "all"):
|
||||
|
||||
async def get_plugin_status():
|
||||
"""
|
||||
获取功能状态
|
||||
说明:
|
||||
获取功能状态
|
||||
"""
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _get_plugin_status)
|
||||
|
||||
|
||||
def _get_plugin_status() -> MessageSegment:
|
||||
"""
|
||||
合成功能状态图片
|
||||
说明:
|
||||
合成功能状态图片
|
||||
"""
|
||||
rst = "\t功能\n"
|
||||
flag_str = "状态".rjust(4) + "\n"
|
||||
@@ -197,7 +263,7 @@ def _get_plugin_status() -> MessageSegment:
|
||||
flag = plugins_manager.get_plugin_block_type(module)
|
||||
flag = flag.upper() + " CLOSE" if flag else "OPEN"
|
||||
try:
|
||||
plugin_name = plugins_manager.get(module)["plugin_name"]
|
||||
plugin_name = plugins_manager.get(module).plugin_name
|
||||
if (
|
||||
"[Hidden]" in plugin_name
|
||||
or "[Admin]" in plugin_name
|
||||
@@ -207,7 +273,7 @@ def _get_plugin_status() -> MessageSegment:
|
||||
rst += f"{plugin_name}"
|
||||
except KeyError:
|
||||
rst += f"{module}"
|
||||
if plugins_manager.get(module)["error"]:
|
||||
if plugins_manager.get(module).error:
|
||||
rst += "[ERROR]"
|
||||
rst += "\n"
|
||||
flag_str += f"{flag}\n"
|
||||
@@ -224,9 +290,11 @@ def _get_plugin_status() -> MessageSegment:
|
||||
|
||||
async def update_member_info(group_id: int, remind_superuser: bool = False) -> bool:
|
||||
"""
|
||||
更新群成员信息
|
||||
:param group_id: 群号
|
||||
:param remind_superuser: 失败信息提醒超级用户
|
||||
说明:
|
||||
更新群成员信息
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
:param remind_superuser: 失败信息提醒超级用户
|
||||
"""
|
||||
bot = get_bot()
|
||||
_group_user_list = await bot.get_group_member_list(group_id=group_id)
|
||||
@@ -235,54 +303,53 @@ async def update_member_info(group_id: int, remind_superuser: bool = False) -> b
|
||||
# try:
|
||||
for user_info in _group_user_list:
|
||||
nickname = user_info["card"] or user_info["nickname"]
|
||||
async with db.transaction():
|
||||
# 更新权限
|
||||
if (
|
||||
user_info["role"]
|
||||
in [
|
||||
"owner",
|
||||
"admin",
|
||||
]
|
||||
and not await LevelUser.is_group_flag(user_info["user_id"], group_id)
|
||||
):
|
||||
await LevelUser.set_level(
|
||||
user_info["user_id"],
|
||||
user_info["group_id"],
|
||||
Config.get_config("admin_bot_manage", "ADMIN_DEFAULT_AUTH"),
|
||||
)
|
||||
if str(user_info["user_id"]) in bot.config.superusers:
|
||||
await LevelUser.set_level(
|
||||
user_info["user_id"], user_info["group_id"], 9
|
||||
)
|
||||
user = await GroupInfoUser.get_member_info(
|
||||
user_info["user_id"], user_info["group_id"]
|
||||
)
|
||||
if user:
|
||||
if user.user_name != nickname:
|
||||
await user.update(user_name=nickname).apply()
|
||||
logger.info(
|
||||
f"用户{user_info['user_id']} 所属{user_info['group_id']} 更新群昵称成功"
|
||||
)
|
||||
_exist_member_list.append(int(user_info["user_id"]))
|
||||
continue
|
||||
join_time = datetime.strptime(
|
||||
time.strftime(
|
||||
"%Y-%m-%d %H:%M:%S", time.localtime(user_info["join_time"])
|
||||
),
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
if await GroupInfoUser.add_member_info(
|
||||
# 更新权限
|
||||
if user_info["role"] in [
|
||||
"owner",
|
||||
"admin",
|
||||
] and not await LevelUser.is_group_flag(user_info["user_id"], group_id):
|
||||
await LevelUser.set_level(
|
||||
user_info["user_id"],
|
||||
user_info["group_id"],
|
||||
nickname,
|
||||
join_time,
|
||||
):
|
||||
_exist_member_list.append(int(user_info["user_id"]))
|
||||
logger.info(f"用户{user_info['user_id']} 所属{user_info['group_id']} 更新成功")
|
||||
else:
|
||||
_error_member_list.append(
|
||||
f"用户{user_info['user_id']} 所属{user_info['group_id']} 更新失败\n"
|
||||
Config.get_config("admin_bot_manage", "ADMIN_DEFAULT_AUTH"),
|
||||
)
|
||||
if str(user_info["user_id"]) in bot.config.superusers:
|
||||
await LevelUser.set_level(user_info["user_id"], user_info["group_id"], 9)
|
||||
user = await GroupInfoUser.get_or_none(
|
||||
user_qq=user_info["user_id"], group_id=user_info["group_id"]
|
||||
)
|
||||
if user:
|
||||
if user.user_name != nickname:
|
||||
user.user_name = nickname
|
||||
await user.save(update_fields=["user_name"])
|
||||
logger.info(
|
||||
f"用户{user_info['user_id']} 所属{user_info['group_id']} 更新群昵称成功"
|
||||
)
|
||||
_exist_member_list.append(int(user_info["user_id"]))
|
||||
continue
|
||||
join_time = datetime.strptime(
|
||||
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(user_info["join_time"])),
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
user, _ = await GroupInfoUser.get_or_create(
|
||||
user_qq=user_info["user_id"],
|
||||
group_id=user_info["group_id"],
|
||||
defaults={
|
||||
"user_name": nickname,
|
||||
},
|
||||
)
|
||||
# await GroupInfoUser.update_or_create(
|
||||
# user_qq=user_info["user_id"],
|
||||
# group_id=user_info["group_id"],
|
||||
# defaults={
|
||||
# "user_name": nickname,
|
||||
# # "user_join_time": datetime.now().replace(tzinfo=None),
|
||||
# },
|
||||
# )
|
||||
user.user_join_time = join_time
|
||||
await user.save()
|
||||
_exist_member_list.append(int(user_info["user_id"]))
|
||||
logger.info("更新成功", "更新成员信息", user_info["user_id"], user_info["group_id"])
|
||||
_del_member_list = list(
|
||||
set(_exist_member_list).difference(
|
||||
set(await GroupInfoUser.get_group_member_id_list(group_id))
|
||||
@@ -290,10 +357,8 @@ async def update_member_info(group_id: int, remind_superuser: bool = False) -> b
|
||||
)
|
||||
if _del_member_list:
|
||||
for del_user in _del_member_list:
|
||||
if await GroupInfoUser.delete_member_info(del_user, group_id):
|
||||
logger.info(f"退群用户{del_user} 所属{group_id} 已删除")
|
||||
else:
|
||||
logger.info(f"退群用户{del_user} 所属{group_id} 删除失败")
|
||||
await GroupInfoUser.filter(user_qq=del_user, group_id=group_id).delete()
|
||||
logger.info(f"退群用户{del_user} 所属{group_id} 已删除")
|
||||
if _error_member_list and remind_superuser:
|
||||
result = ""
|
||||
for error_user in _error_member_list:
|
||||
@@ -306,9 +371,11 @@ async def update_member_info(group_id: int, remind_superuser: bool = False) -> b
|
||||
|
||||
def set_group_bot_status(group_id: int, status: bool) -> str:
|
||||
"""
|
||||
设置群聊bot开关状态
|
||||
:param group_id: 群号
|
||||
:param status: 状态
|
||||
说明:
|
||||
设置群聊bot开关状态
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
:param status: 状态
|
||||
"""
|
||||
if status:
|
||||
if group_manager.check_group_bot_status(group_id):
|
||||
@@ -317,7 +384,4 @@ def set_group_bot_status(group_id: int, status: bool) -> str:
|
||||
return "呜..醒来了..."
|
||||
else:
|
||||
group_manager.shutdown_group_bot_status(group_id)
|
||||
# for x in group_manager.get_task_data():
|
||||
# group_manager.close_group_task(group_id, x)
|
||||
return "那我先睡觉了..."
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
from nonebot import on_notice
|
||||
from services.log import logger
|
||||
from nonebot.adapters.onebot.v11 import GroupAdminNoticeEvent
|
||||
from models.level_user import LevelUser
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from configs.config import Config
|
||||
|
||||
from configs.config import Config
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from models.level_user import LevelUser
|
||||
from services.log import logger
|
||||
|
||||
__zx_plugin_name__ = "群管理员变动监测 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
@@ -16,11 +16,11 @@ admin_notice = on_notice(priority=5)
|
||||
|
||||
@admin_notice.handle()
|
||||
async def _(event: GroupAdminNoticeEvent):
|
||||
try:
|
||||
nickname = (
|
||||
await GroupInfoUser.get_member_info(event.user_id, event.group_id)
|
||||
).user_name
|
||||
except AttributeError:
|
||||
if user := await GroupInfoUser.filter(
|
||||
user_qq=event.user_id, group_id=event.group_id
|
||||
).first():
|
||||
nickname = user.nickname
|
||||
else:
|
||||
nickname = event.user_id
|
||||
if event.sub_type == "set":
|
||||
await LevelUser.set_level(
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
from nonebot import on_command
|
||||
from utils.utils import get_message_img
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message
|
||||
from nonebot.params import CommandArg
|
||||
from ._data_source import custom_group_welcome
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from configs.config import Config
|
||||
from services.log import logger
|
||||
from typing import List
|
||||
|
||||
from configs.config import Config
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from nonebot.params import CommandArg
|
||||
from services.log import logger
|
||||
from utils.depends import ImageList
|
||||
|
||||
from ._data_source import custom_group_welcome
|
||||
|
||||
__zx_plugin_name__ = "自定义进群欢迎消息 [Admin]"
|
||||
__plugin_usage__ = """
|
||||
@@ -17,12 +19,14 @@ usage:
|
||||
Note:可以通过[at]来确认是否艾特新成员
|
||||
示例:自定义进群欢迎消息 欢迎你[at]
|
||||
""".strip()
|
||||
__plugin_des__ = '简易的自定义群欢迎消息'
|
||||
__plugin_cmd__ = ['自定义群欢迎消息 ?[文本] ?[图片]']
|
||||
__plugin_des__ = "简易的自定义群欢迎消息"
|
||||
__plugin_cmd__ = ["自定义群欢迎消息 ?[文本] ?[图片]"]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = 'HibiKier'
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
"admin_level": Config.get_config("admin_bot_manage", "SET_GROUP_WELCOME_MESSAGE_LEVEL"),
|
||||
"admin_level": Config.get_config(
|
||||
"admin_bot_manage", "SET_GROUP_WELCOME_MESSAGE_LEVEL"
|
||||
),
|
||||
}
|
||||
|
||||
custom_welcome = on_command(
|
||||
@@ -35,12 +39,13 @@ custom_welcome = on_command(
|
||||
|
||||
|
||||
@custom_welcome.handle()
|
||||
async def _(event: GroupMessageEvent, arg: Message = CommandArg()):
|
||||
async def _(
|
||||
event: GroupMessageEvent, arg: Message = CommandArg(), img: List[str] = ImageList()
|
||||
):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
if not msg and not img:
|
||||
await custom_welcome.finish(__plugin_usage__)
|
||||
try:
|
||||
msg = arg.extract_plain_text().strip()
|
||||
img = get_message_img(event.json())
|
||||
if not msg and not img:
|
||||
await custom_welcome.finish(__plugin_usage__)
|
||||
await custom_welcome.send(
|
||||
await custom_group_welcome(msg, img, event.user_id, event.group_id),
|
||||
at_sender=True,
|
||||
|
||||
@@ -2,30 +2,37 @@ from nonebot.adapters.onebot.v11 import Event
|
||||
from utils.manager import group_manager, plugins2settings_manager
|
||||
from utils.utils import get_message_text
|
||||
from services.log import logger
|
||||
import time
|
||||
|
||||
cmd = []
|
||||
|
||||
v = time.time()
|
||||
|
||||
|
||||
def switch_rule(event: Event) -> bool:
|
||||
"""
|
||||
检测文本是否是关闭功能命令
|
||||
:param event: pass
|
||||
说明:
|
||||
检测文本是否是关闭功能命令
|
||||
参数:
|
||||
:param event: pass
|
||||
"""
|
||||
global cmd
|
||||
global cmd, v
|
||||
try:
|
||||
if not cmd:
|
||||
if not cmd or time.time() - v > 60 * 60:
|
||||
cmd = ["关闭全部被动", "开启全部被动", "开启全部功能", "关闭全部功能"]
|
||||
_data = group_manager.get_task_data()
|
||||
for key in _data:
|
||||
cmd.append(f"开启{_data[key]}")
|
||||
cmd.append(f"关闭{_data[key]}")
|
||||
cmd.append(f"开启被动{_data[key]}")
|
||||
cmd.append(f"关闭被动{_data[key]}")
|
||||
cmd.append(f"开启 {_data[key]}")
|
||||
cmd.append(f"关闭 {_data[key]}")
|
||||
_data = plugins2settings_manager.get_data()
|
||||
for key in _data:
|
||||
for key in _data.keys():
|
||||
try:
|
||||
if isinstance(_data[key]["cmd"], list):
|
||||
for x in _data[key]["cmd"]:
|
||||
if isinstance(_data[key].cmd, list):
|
||||
for x in _data[key].cmd:
|
||||
cmd.append(f"开启{x}")
|
||||
cmd.append(f"关闭{x}")
|
||||
cmd.append(f"开启 {x}")
|
||||
@@ -37,6 +44,7 @@ def switch_rule(event: Event) -> bool:
|
||||
cmd.append(f"关闭 {key}")
|
||||
except KeyError:
|
||||
pass
|
||||
v = time.time()
|
||||
msg = get_message_text(event.json()).split()
|
||||
msg = msg[0] if msg else ""
|
||||
return msg in cmd
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, MessageEvent, GROUP
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, MessageEvent, GROUP, PrivateMessageEvent
|
||||
from nonebot import on_command, on_message, on_regex
|
||||
from nonebot.params import RegexGroup
|
||||
|
||||
from utils.message_builder import image
|
||||
from ._data_source import (
|
||||
change_group_switch,
|
||||
set_plugin_status,
|
||||
get_plugin_status,
|
||||
group_current_status,
|
||||
set_group_bot_status
|
||||
set_group_bot_status, change_global_task_status
|
||||
)
|
||||
from services.log import logger
|
||||
from configs.config import NICKNAME, Config
|
||||
@@ -31,11 +33,12 @@ usage:
|
||||
""".strip()
|
||||
__plugin_superuser_usage__ = """
|
||||
usage:
|
||||
功能总开关与指定群禁用
|
||||
(私聊)功能总开关与指定群禁用
|
||||
指令:
|
||||
功能状态
|
||||
开启/关闭[功能] [group]
|
||||
开启/关闭[功能] ['private'/'group']
|
||||
开启被动/关闭被动[被动名称] # 全局被动控制
|
||||
""".strip()
|
||||
__plugin_des__ = "群内功能开关"
|
||||
__plugin_cmd__ = [
|
||||
@@ -74,7 +77,9 @@ async def _(bot: Bot, event: MessageEvent):
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
block_type = " ".join(get_message_text(event.json()).split()[1:])
|
||||
block_type = block_type if block_type else "a"
|
||||
if is_number(block_type):
|
||||
if ("关闭被动" in _cmd or "开启被动" in _cmd) and isinstance(event, PrivateMessageEvent):
|
||||
await switch_rule_matcher.send(change_global_task_status(_cmd))
|
||||
elif is_number(block_type):
|
||||
if not int(block_type) in [
|
||||
g["group_id"]
|
||||
for g in await bot.get_group_list()
|
||||
@@ -85,7 +90,7 @@ async def _(bot: Bot, event: MessageEvent):
|
||||
"group_name"
|
||||
]
|
||||
await switch_rule_matcher.send(
|
||||
f"已禁用群聊 {group_name}({block_type}) 的 {_cmd[2:]} 功能"
|
||||
f"已{_cmd[:2]}群聊 {group_name}({block_type}) 的 {_cmd[2:]} 功能"
|
||||
)
|
||||
elif block_type in ["all", "private", "group", "a", "p", "g"]:
|
||||
block_type = "all" if block_type == "a" else block_type
|
||||
@@ -110,7 +115,7 @@ async def _():
|
||||
|
||||
@group_task_status.handle()
|
||||
async def _(event: GroupMessageEvent):
|
||||
await group_task_status.send(await group_current_status(event.group_id))
|
||||
await group_task_status.send(image(b64=await group_current_status(event.group_id)))
|
||||
|
||||
|
||||
@group_status.handle()
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
from utils.utils import scheduler, get_bot
|
||||
from ._data_source import update_member_info
|
||||
from services.log import logger
|
||||
from models.group_info import GroupInfo
|
||||
from asyncpg.exceptions import ConnectionDoesNotExistError, UndefinedColumnError
|
||||
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
from utils.utils import get_bot, scheduler
|
||||
|
||||
__zx_plugin_name__ = '管理方面定时任务 [Hidden]'
|
||||
__plugin_usage__ = '无'
|
||||
__plugin_des__ = '成员信息和管理权限的定时更新'
|
||||
from ._data_source import update_member_info
|
||||
|
||||
__zx_plugin_name__ = "管理方面定时任务 [Hidden]"
|
||||
__plugin_usage__ = "无"
|
||||
__plugin_des__ = "成员信息和管理权限的定时更新"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = 'HibiKier'
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
|
||||
# 自动更新群员信息
|
||||
@@ -42,7 +43,7 @@ async def _():
|
||||
if bot:
|
||||
gl = await bot.get_group_list()
|
||||
gl = [g["group_id"] for g in gl]
|
||||
all_group = [x.group_id for x in await GroupInfo.get_all_group()]
|
||||
all_group = [x.group_id for x in await GroupInfo.all()]
|
||||
for g in gl:
|
||||
if g not in all_group:
|
||||
await update_member_info(g, False)
|
||||
|
||||
@@ -3,7 +3,7 @@ from nonebot.typing import T_State
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent
|
||||
from utils.message_builder import image
|
||||
from .data_source import create_help_image, admin_help_image
|
||||
from .data_source import create_help_image, ADMIN_HELP_IMAGE
|
||||
|
||||
|
||||
__zx_plugin_name__ = '管理帮助 [Admin]'
|
||||
@@ -16,12 +16,12 @@ __plugin_settings__ = {
|
||||
|
||||
admin_help = on_command("管理员帮助", aliases={"管理帮助"}, priority=5, block=True)
|
||||
|
||||
if admin_help_image.exists():
|
||||
admin_help_image.unlink()
|
||||
if ADMIN_HELP_IMAGE.exists():
|
||||
ADMIN_HELP_IMAGE.unlink()
|
||||
|
||||
|
||||
@admin_help.handle()
|
||||
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
if not admin_help_image.exists():
|
||||
if not ADMIN_HELP_IMAGE.exists():
|
||||
await create_help_image()
|
||||
await admin_help.send(image('admin_help_img.png'))
|
||||
await admin_help.send(image(ADMIN_HELP_IMAGE))
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
from utils.image_utils import BuildImage
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from services.log import logger
|
||||
from utils.utils import get_matchers
|
||||
from utils.manager import group_manager
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
from nonebot import Driver
|
||||
import nonebot
|
||||
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from nonebot import Driver
|
||||
from services.log import logger
|
||||
from utils.image_template import help_template
|
||||
from utils.image_utils import (BuildImage, build_sort_image, group_image,
|
||||
text2image)
|
||||
from utils.manager import group_manager, plugin_data_manager
|
||||
from utils.manager.models import PluginType
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
background = IMAGE_PATH / "background" / "0.png"
|
||||
|
||||
admin_help_image = IMAGE_PATH / 'admin_help_img.png'
|
||||
ADMIN_HELP_IMAGE = IMAGE_PATH / "admin_help_img.png"
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def init_task(bot: Bot = None):
|
||||
async def init_task():
|
||||
if not group_manager.get_task_data():
|
||||
await group_manager.init_group_task()
|
||||
logger.info(f'已成功加载 {len(group_manager.get_task_data())} 个被动技能.')
|
||||
group_manager.load_task()
|
||||
logger.info(f"已成功加载 {len(group_manager.get_task_data())} 个被动技能.")
|
||||
|
||||
|
||||
async def create_help_image():
|
||||
@@ -33,58 +33,55 @@ async def _create_help_image():
|
||||
"""
|
||||
创建管理员帮助图片
|
||||
"""
|
||||
_matchers = get_matchers()
|
||||
_plugin_name_list = []
|
||||
width = 0
|
||||
_plugin_level = {}
|
||||
for matcher in _matchers:
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
_module = _plugin.module
|
||||
if ADMIN_HELP_IMAGE.exists():
|
||||
return
|
||||
plugin_data_ = plugin_data_manager.get_data()
|
||||
image_list = []
|
||||
task_list = []
|
||||
for plugin_data in [plugin_data_[x] for x in plugin_data_]:
|
||||
try:
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
except AttributeError:
|
||||
continue
|
||||
try:
|
||||
if (
|
||||
"[admin]" in plugin_name.lower()
|
||||
and plugin_name not in _plugin_name_list
|
||||
and plugin_name != "管理帮助 [Admin]"
|
||||
):
|
||||
_plugin_name_list.append(plugin_name)
|
||||
plugin_settings = _module.__getattribute__("__plugin_settings__")
|
||||
plugin_des = _module.__getattribute__("__plugin_des__")
|
||||
plugin_cmd = _module.__getattribute__("__plugin_cmd__")
|
||||
plugin_cmd = [x for x in plugin_cmd if "[_superuser]" not in x]
|
||||
admin_level = int(plugin_settings["admin_level"])
|
||||
if _plugin_level.get(admin_level):
|
||||
_plugin_level[admin_level].append(
|
||||
f"[{admin_level}] {plugin_des} -> " + " / ".join(plugin_cmd)
|
||||
)
|
||||
else:
|
||||
_plugin_level[admin_level] = [
|
||||
f"[{admin_level}] {plugin_des} -> " + " / ".join(plugin_cmd)
|
||||
]
|
||||
x = len(f"[{admin_level}] {plugin_des} -> " + " / ".join(plugin_cmd)) * 23
|
||||
width = width if width > x else x
|
||||
except AttributeError:
|
||||
logger.warning(f"获取管理插件 {matcher.plugin_name}: {plugin_name} 设置失败...")
|
||||
help_str = "* 注: ‘*’ 代表可有多个相同参数 ‘?’ 代表可省略该参数 *\n\n" \
|
||||
"[权限等级] 管理员帮助:\n\n"
|
||||
x = list(_plugin_level.keys())
|
||||
x.sort()
|
||||
for level in x:
|
||||
for help_ in _plugin_level[level]:
|
||||
help_str += f"\t{help_}\n\n"
|
||||
help_str += '-----[被动技能开关]-----\n\n'
|
||||
task_data = group_manager.get_task_data()
|
||||
for i, x in enumerate(task_data.keys()):
|
||||
help_str += f'{i+1}.开启/关闭{task_data[x]}\n\n'
|
||||
height = len(help_str.split("\n")) * 33
|
||||
A = BuildImage(width, height, font_size=24)
|
||||
_background = BuildImage(width, height, background=background)
|
||||
await A.apaste(_background, alpha=True)
|
||||
await A.atext((150, 110), help_str)
|
||||
await A.asave(admin_help_image)
|
||||
logger.info(f'已成功加载 {len(_plugin_name_list)} 条管理员命令')
|
||||
|
||||
|
||||
usage = None
|
||||
if plugin_data.plugin_type == PluginType.ADMIN and plugin_data.usage:
|
||||
usage = await text2image(
|
||||
plugin_data.usage, padding=5, color=(204, 196, 151)
|
||||
)
|
||||
if usage:
|
||||
await usage.acircle_corner()
|
||||
level = 5
|
||||
if plugin_data.plugin_setting:
|
||||
level = plugin_data.plugin_setting.level or level
|
||||
image = await help_template(plugin_data.name + f"[{level}]", usage)
|
||||
image_list.append(image)
|
||||
if plugin_data.task:
|
||||
for x in plugin_data.task.keys():
|
||||
task_list.append(plugin_data.task[x])
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"获取群管理员插件 {plugin_data.model}: {plugin_data.name} 设置失败... {type(e)}:{e}"
|
||||
)
|
||||
task_str = "\n".join(task_list)
|
||||
task_str = "通过 开启/关闭 来控制群被动\n----------\n" + task_str
|
||||
task_image = await text2image(task_str, padding=5, color=(204, 196, 151))
|
||||
task_image = await help_template("被动任务", task_image)
|
||||
image_list.append(task_image)
|
||||
image_group, _ = group_image(image_list)
|
||||
A = await build_sort_image(image_group, color="#f9f6f2", padding_top=180)
|
||||
await A.apaste(
|
||||
BuildImage(0, 0, font="CJGaoDeGuo.otf", plain_text="群管理员帮助", font_size=50),
|
||||
(50, 30),
|
||||
True,
|
||||
)
|
||||
await A.apaste(
|
||||
BuildImage(
|
||||
0,
|
||||
0,
|
||||
font="CJGaoDeGuo.otf",
|
||||
plain_text="注: ‘*’ 代表可有多个相同参数 ‘?’ 代表可省略该参数",
|
||||
font_size=30,
|
||||
font_color="red",
|
||||
),
|
||||
(50, 90),
|
||||
True,
|
||||
)
|
||||
await A.asave(ADMIN_HELP_IMAGE)
|
||||
logger.info(f"已成功加载 {len(image_list)} 条管理员命令")
|
||||
|
||||
@@ -1,48 +1,52 @@
|
||||
from utils.message_builder import image
|
||||
from utils.utils import scheduler, get_bot
|
||||
from nonebot import on_message
|
||||
from services.log import logger
|
||||
from models.group_info import GroupInfo
|
||||
from models.friend_user import FriendUser
|
||||
from nonebot.adapters.onebot.v11.exception import ActionFailed
|
||||
from configs.config import NICKNAME, Config
|
||||
from utils.manager import group_manager
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import nonebot
|
||||
from nonebot import on_message
|
||||
from nonebot.adapters.onebot.v11 import ActionFailed
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
from utils.message_builder import image
|
||||
from utils.utils import broadcast_group, get_bot, scheduler
|
||||
|
||||
__zx_plugin_name__ = "定时任务相关 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_task__ = {'zwa': '早晚安'}
|
||||
__plugin_task__ = {"zwa": "早晚安"}
|
||||
|
||||
|
||||
Config.add_plugin_config(
|
||||
"_task",
|
||||
"DEFAULT_ZWA",
|
||||
True,
|
||||
help_="被动 早晚安 进群默认开关状态",
|
||||
default_value=True,
|
||||
"_task", "DEFAULT_ZWA", True, help_="被动 早晚安 进群默认开关状态", default_value=True, type=bool
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
"_backup",
|
||||
"BACKUP_FLAG",
|
||||
True,
|
||||
help_="是否开启文件备份",
|
||||
default_value=True
|
||||
"_backup", "BACKUP_FLAG", True, help_="是否开启文件备份", default_value=True, type=bool
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
"_backup",
|
||||
"BACKUP_DIR_OR_FILE",
|
||||
['data/black_word', 'data/configs', 'data/statistics', 'data/word_bank', 'data/manager', 'configs'],
|
||||
[
|
||||
"data/black_word",
|
||||
"data/configs",
|
||||
"data/statistics",
|
||||
"data/word_bank",
|
||||
"data/manager",
|
||||
"configs",
|
||||
],
|
||||
name="文件备份",
|
||||
help_="备份的文件夹或文件",
|
||||
default_value=[]
|
||||
default_value=[],
|
||||
type=List[str],
|
||||
)
|
||||
|
||||
|
||||
cx = on_message(priority=9, block=False)
|
||||
cx = on_message(priority=9999, block=False, rule=lambda: False)
|
||||
|
||||
|
||||
# 早上好
|
||||
@@ -52,18 +56,9 @@ cx = on_message(priority=9, block=False)
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
bot = get_bot()
|
||||
gl = await bot.get_group_list()
|
||||
gl = [g["group_id"] for g in gl]
|
||||
for g in gl:
|
||||
result = image("zao.jpg", "zhenxun")
|
||||
try:
|
||||
await bot.send_group_msg(group_id=g, message="[[_task|zwa]]早上好" + result)
|
||||
except ActionFailed:
|
||||
logger.warning(f"{g} 群被禁言中,无法发送早安")
|
||||
except Exception as e:
|
||||
logger.error(f"早晚安错误 e:{e}")
|
||||
img = image(IMAGE_PATH / "zhenxun" / "zao.jpg")
|
||||
await broadcast_group("[[_task|zwa]]早上好" + img, log_cmd="被动早晚安")
|
||||
logger.info("每日早安发送")
|
||||
|
||||
|
||||
# 睡觉了
|
||||
@@ -73,20 +68,11 @@ async def _():
|
||||
minute=59,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
bot = get_bot()
|
||||
gl = await bot.get_group_list()
|
||||
gl = [g["group_id"] for g in gl]
|
||||
for g in gl:
|
||||
result = image("sleep.jpg", "zhenxun")
|
||||
try:
|
||||
await bot.send_group_msg(
|
||||
group_id=g, message=f"[[_task|zwa]]{NICKNAME}要睡觉了,你们也要早点睡呀" + result
|
||||
)
|
||||
except ActionFailed:
|
||||
logger.warning(f"{g} 群被禁言中,无法发送晚安")
|
||||
except Exception as e:
|
||||
logger.error(f"早晚安错误 e:{e}")
|
||||
img = image(IMAGE_PATH / "zhenxun" / "sleep.jpg")
|
||||
await broadcast_group(
|
||||
f"[[_task|zwa]]{NICKNAME}要睡觉了,你们也要早点睡呀" + img, log_cmd="被动早晚安"
|
||||
)
|
||||
logger.info("每日晚安发送")
|
||||
|
||||
|
||||
# 自动更新群组信息
|
||||
@@ -96,21 +82,27 @@ async def _():
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
bot = get_bot()
|
||||
gl = await bot.get_group_list()
|
||||
gl = [g["group_id"] for g in gl]
|
||||
for g in gl:
|
||||
group_info = await bot.get_group_info(group_id=g)
|
||||
await GroupInfo.add_group_info(
|
||||
group_info["group_id"],
|
||||
group_info["group_name"],
|
||||
group_info["max_member_count"],
|
||||
group_info["member_count"],
|
||||
)
|
||||
logger.info(f"自动更新群组 {g} 信息成功")
|
||||
except Exception as e:
|
||||
logger.error(f"自动更新群组信息错误 e:{e}")
|
||||
bots = nonebot.get_bots()
|
||||
_used_group = []
|
||||
for bot in bots.values():
|
||||
try:
|
||||
group_list = await bot.get_group_list()
|
||||
gl = [g["group_id"] for g in group_list if g["group_id"] not in _used_group]
|
||||
for g in gl:
|
||||
_used_group.append(g)
|
||||
group_info = await bot.get_group_info(group_id=g)
|
||||
await GroupInfo.update_or_create(
|
||||
group_id=group_info["group_id"],
|
||||
defaults={
|
||||
"group_name": group_info["group_name"],
|
||||
"max_member_count": group_info["max_member_count"],
|
||||
"member_count": group_info["member_count"],
|
||||
"group_flag": 1,
|
||||
},
|
||||
)
|
||||
logger.info(f"自动更新群组信息成功", group_id=g)
|
||||
except Exception as e:
|
||||
logger.error(f"Bot: {bot.self_id} 自动更新群组信息", e=e)
|
||||
|
||||
|
||||
# 自动更新好友信息
|
||||
@@ -124,10 +116,10 @@ async def _():
|
||||
bot = get_bot()
|
||||
fl = await bot.get_friend_list()
|
||||
for f in fl:
|
||||
if await FriendUser.add_friend_info(f["user_id"], f["nickname"]):
|
||||
logger.info(f'自动更新好友 {f["user_id"]} 信息成功')
|
||||
else:
|
||||
logger.warning(f'自动更新好友 {f["user_id"]} 信息失败')
|
||||
await FriendUser.create(user_id=f["user_id"], user_name=f["nickname"])
|
||||
logger.info(f'自动更新好友 {f["user_id"]} 信息成功')
|
||||
# else:
|
||||
# logger.warning(f'自动更新好友 {f["user_id"]} 信息失败')
|
||||
except Exception as e:
|
||||
logger.error(f"自动更新群组信息错误 e:{e}")
|
||||
|
||||
@@ -140,7 +132,7 @@ async def _():
|
||||
)
|
||||
async def _():
|
||||
if Config.get_config("_backup", "BACKUP_FLAG"):
|
||||
_backup_path = Path() / 'backup'
|
||||
_backup_path = Path() / "backup"
|
||||
_backup_path.mkdir(exist_ok=True, parents=True)
|
||||
for x in Config.get_config("_backup", "BACKUP_DIR_OR_FILE"):
|
||||
try:
|
||||
@@ -155,12 +147,13 @@ async def _():
|
||||
if _p.exists():
|
||||
_p.unlink()
|
||||
shutil.copy(x, _p)
|
||||
logger.info(f'已完成自动备份:{x}')
|
||||
logger.info(f"已完成自动备份:{x}")
|
||||
except Exception as e:
|
||||
logger.error(f"自动备份文件 {x} 发生错误 {type(e)}:{e}")
|
||||
|
||||
|
||||
# 一次性任务
|
||||
|
||||
|
||||
# 固定时间触发,仅触发一次:
|
||||
#
|
||||
# from datetime import datetime
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
from typing import Tuple
|
||||
from typing import List
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
GroupMessageEvent,
|
||||
Message,
|
||||
MessageEvent,
|
||||
PrivateMessageEvent,
|
||||
)
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from models.ban_user import BanUser
|
||||
from models.level_user import LevelUser
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import (Bot, GroupMessageEvent, Message,
|
||||
MessageEvent, PrivateMessageEvent)
|
||||
from nonebot.params import Command, CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
from services.log import logger
|
||||
from utils.utils import get_message_at, is_number
|
||||
from utils.depends import AtList, OneCommand
|
||||
from utils.utils import is_number
|
||||
|
||||
from .data_source import a_ban, parse_ban_time
|
||||
|
||||
@@ -49,6 +56,7 @@ __plugin_configs__ = {
|
||||
"value": 5,
|
||||
"help": "ban/unban所需要的管理员权限等级",
|
||||
"default_value": 5,
|
||||
"type": int
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,46 +75,38 @@ super_ban = on_command("b了", permission=SUPERUSER, priority=5, block=True)
|
||||
async def _(
|
||||
bot: Bot,
|
||||
event: GroupMessageEvent,
|
||||
cmd: Tuple[str, ...] = Command(),
|
||||
cmd: str = OneCommand(),
|
||||
arg: Message = CommandArg(),
|
||||
at_list: List[int] = AtList(),
|
||||
):
|
||||
cmd = cmd[0]
|
||||
result = ""
|
||||
qq = get_message_at(event.json())
|
||||
if qq:
|
||||
qq = qq[0]
|
||||
user_name = await bot.get_group_member_info(group_id=event.group_id, user_id=qq)
|
||||
user_name = user_name["card"] or user_name["nickname"]
|
||||
if at_list:
|
||||
qq = at_list[0]
|
||||
user = await bot.get_group_member_info(group_id=event.group_id, user_id=qq)
|
||||
user_name = user["card"] or user["nickname"]
|
||||
msg = arg.extract_plain_text().strip()
|
||||
time = parse_ban_time(msg)
|
||||
if isinstance(time, str):
|
||||
await ban.finish(time, at_sender=True)
|
||||
user_level = await LevelUser.get_user_level(event.user_id, event.group_id)
|
||||
is_not_superuser = str(event.user_id) not in bot.config.superusers
|
||||
if cmd in [".ban", "/ban"]:
|
||||
if (
|
||||
await LevelUser.get_user_level(event.user_id, event.group_id)
|
||||
<= await LevelUser.get_user_level(qq, event.group_id)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
):
|
||||
at_user_level = await LevelUser.get_user_level(qq, event.group_id)
|
||||
if user_level <= at_user_level and is_not_superuser:
|
||||
await ban.finish(
|
||||
f"您的权限等级比对方低或相等, {NICKNAME}不能为您使用此功能!",
|
||||
at_sender=True,
|
||||
)
|
||||
logger.info(f"用户封禁 时长: {time}", cmd, event.user_id, event.group_id, qq)
|
||||
result = await a_ban(qq, time, user_name, event)
|
||||
else:
|
||||
if (
|
||||
await BanUser.check_ban_level(
|
||||
qq, await LevelUser.get_user_level(event.user_id, event.group_id)
|
||||
)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
):
|
||||
if await BanUser.check_ban_level(qq, user_level) and is_not_superuser:
|
||||
await ban.finish(
|
||||
f"ban掉 {user_name} 的管理员权限比您高,无法进行unban", at_sender=True
|
||||
)
|
||||
if await BanUser.unban(qq):
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP {event.group_id} 将 USER {qq} 解禁"
|
||||
)
|
||||
result = f"已经把 {user_name} 从黑名单中删除了!"
|
||||
logger.info(f"解除用户封禁", cmd, event.user_id, event.group_id, qq)
|
||||
result = f"已经将 {user_name} 从黑名单中删除了!"
|
||||
else:
|
||||
result = f"{user_name} 不在黑名单!"
|
||||
else:
|
||||
@@ -118,55 +118,62 @@ async def _(
|
||||
async def _(
|
||||
bot: Bot,
|
||||
event: PrivateMessageEvent,
|
||||
cmd: Tuple[str, ...] = Command(),
|
||||
cmd: str = OneCommand(),
|
||||
arg: Message = CommandArg(),
|
||||
):
|
||||
cmd = cmd[0]
|
||||
msg = arg.extract_plain_text().strip()
|
||||
if msg:
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
msg_splt = msg.split()
|
||||
if is_number(msg_splt[0]):
|
||||
|
||||
qq = int(msg_splt[0])
|
||||
msg = msg_splt[1:]
|
||||
if cmd in [".ban", "/ban"]:
|
||||
time = parse_ban_time(" ".join(msg))
|
||||
if isinstance(time, str):
|
||||
await ban.finish(time)
|
||||
result = await a_ban(qq, time, str(qq), event, 9)
|
||||
else:
|
||||
if await BanUser.unban(qq):
|
||||
logger.info(f"USER {event.user_id} 将 USER {qq} 解禁")
|
||||
result = f"已经把 {qq} 从黑名单中删除了!"
|
||||
else:
|
||||
result = f"{qq} 不在黑名单!"
|
||||
await ban.send(result)
|
||||
if msg and str(event.user_id) in bot.config.superusers:
|
||||
msg_split = msg.split()
|
||||
if msg_split and is_number(msg_split[0]):
|
||||
qq = int(msg_split[0])
|
||||
param = msg_split[1:]
|
||||
if cmd in [".ban", "/ban"]:
|
||||
time = parse_ban_time(" ".join(param))
|
||||
if isinstance(time, str):
|
||||
logger.info(time, cmd, event.user_id, target=qq)
|
||||
await ban.finish(time)
|
||||
result = await a_ban(qq, time, str(qq), event, 9)
|
||||
else:
|
||||
await ban.finish(
|
||||
"qq号必须是数字!\n格式:.ban [qq] [hour]? [minute]?", at_sender=True
|
||||
)
|
||||
if await BanUser.unban(qq):
|
||||
result = f"已经把 {qq} 从黑名单中删除了!"
|
||||
else:
|
||||
result = f"{qq} 不在黑名单!"
|
||||
await ban.send(result)
|
||||
logger.info(result, cmd, event.user_id, target=qq)
|
||||
else:
|
||||
await ban.send("参数不正确!\n格式:.ban [qq] [hour]? [minute]?", at_sender=True)
|
||||
|
||||
|
||||
@super_ban.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
async def _(
|
||||
bot: Bot,
|
||||
event: MessageEvent,
|
||||
cmd: str = OneCommand(),
|
||||
arg: Message = CommandArg(),
|
||||
at_list: List[int] = AtList(),
|
||||
):
|
||||
user_name = ""
|
||||
qq = None
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
qq = get_message_at(event.json())
|
||||
if qq:
|
||||
qq = qq[0]
|
||||
if at_list:
|
||||
qq = at_list[0]
|
||||
user = await bot.get_group_member_info(group_id=event.group_id, user_id=qq)
|
||||
user_name = user["card"] or user["nickname"]
|
||||
else:
|
||||
qq = arg.extract_plain_text().strip()
|
||||
if not is_number(qq):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
if not is_number(msg):
|
||||
await super_ban.finish("对象qq必须为纯数字...")
|
||||
qq = int(qq)
|
||||
user_name = qq
|
||||
qq = int(msg)
|
||||
user_name = msg
|
||||
if qq:
|
||||
if not await BanUser.ban(qq, 10, 99999999):
|
||||
await BanUser.unban(qq)
|
||||
await BanUser.ban(qq, 10, 99999999)
|
||||
await BanUser.ban(qq, 10, 99999999)
|
||||
await ban.send(f"已将 {user_name} 拉入黑名单!")
|
||||
logger.info(
|
||||
f"已将 {user_name} 拉入黑名单!",
|
||||
cmd,
|
||||
event.user_id,
|
||||
event.group_id if isinstance(event, GroupMessageEvent) else None,
|
||||
qq,
|
||||
)
|
||||
else:
|
||||
await super_ban.send("需要添加被super ban的对象,可以使用at或者指定qq..")
|
||||
await super_ban.send("需要提供被super ban的对象,可以使用at或者指定qq...")
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent, GroupMessageEvent
|
||||
from typing import Optional, Union
|
||||
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
|
||||
|
||||
from configs.config import NICKNAME
|
||||
from models.level_user import LevelUser
|
||||
from utils.utils import is_number
|
||||
from models.ban_user import BanUser
|
||||
from models.level_user import LevelUser
|
||||
from services.log import logger
|
||||
from typing import Union
|
||||
from utils.utils import is_number
|
||||
|
||||
|
||||
def parse_ban_time(msg: str) -> Union[int, str]:
|
||||
@@ -12,21 +14,31 @@ def parse_ban_time(msg: str) -> Union[int, str]:
|
||||
解析ban时长
|
||||
:param msg: 文本消息
|
||||
"""
|
||||
if not msg:
|
||||
return -1
|
||||
msg = msg.split()
|
||||
if len(msg) == 1:
|
||||
if not is_number(msg[0].strip()):
|
||||
return "参数必须是数字!"
|
||||
return int(msg[0]) * 60 * 60
|
||||
else:
|
||||
if not is_number(msg[0].strip()) or not is_number(msg[1].strip()):
|
||||
return "参数必须是数字!"
|
||||
return int(msg[0]) * 60 * 60 + int(msg[1]) * 60
|
||||
try:
|
||||
if not msg:
|
||||
return -1
|
||||
msg_split = msg.split()
|
||||
if len(msg_split) == 1:
|
||||
if not is_number(msg_split[0].strip()):
|
||||
return "参数必须是数字!"
|
||||
return int(msg_split[0]) * 60 * 60
|
||||
else:
|
||||
if not is_number(msg_split[0].strip()) or not is_number(
|
||||
msg_split[1].strip()
|
||||
):
|
||||
return "参数必须是数字!"
|
||||
return int(msg_split[0]) * 60 * 60 + int(msg_split[1]) * 60
|
||||
except ValueError as e:
|
||||
logger.error("解析ban时长错误", ".ban", e=e)
|
||||
return "时长不可以带小数点!"
|
||||
|
||||
|
||||
async def a_ban(
|
||||
qq: int, time: int, user_name: str, event: MessageEvent, ban_level: int = None
|
||||
qq: int,
|
||||
time: int,
|
||||
user_name: str,
|
||||
event: MessageEvent,
|
||||
ban_level: Optional[int] = None,
|
||||
) -> str:
|
||||
"""
|
||||
ban
|
||||
@@ -36,12 +48,15 @@ async def a_ban(
|
||||
:param event: event
|
||||
:param ban_level: ban级别
|
||||
"""
|
||||
group_id = None
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
group_id = event.group_id
|
||||
ban_level = await LevelUser.get_user_level(event.user_id, event.group_id)
|
||||
if not ban_level:
|
||||
return "未查询到ban级用户权限"
|
||||
if await BanUser.ban(qq, ban_level, time):
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP"
|
||||
f" {event.group_id if isinstance(event, GroupMessageEvent) else ''} 将 USER {qq} 封禁 时长 {time / 60} 分钟"
|
||||
f"将 [Target]({qq})封禁 时长 {time / 60} 分钟", ".ban", event.user_id, group_id
|
||||
)
|
||||
result = f"已经将 {user_name} 加入{NICKNAME}的黑名单了!"
|
||||
if time != -1:
|
||||
@@ -49,14 +64,14 @@ async def a_ban(
|
||||
else:
|
||||
result += f"将在 ∞ 分钟后解封"
|
||||
else:
|
||||
time = await BanUser.check_ban_time(qq)
|
||||
if is_number(time):
|
||||
time = abs(int(time))
|
||||
if time < 60:
|
||||
time = str(time) + " 秒"
|
||||
ban_time = await BanUser.check_ban_time(qq)
|
||||
if isinstance(ban_time, int):
|
||||
ban_time = abs(float(ban_time))
|
||||
if ban_time < 60:
|
||||
ban_time = str(ban_time) + " 秒"
|
||||
else:
|
||||
time = str(int(time / 60)) + " 分钟"
|
||||
ban_time = str(int(ban_time / 60)) + " 分钟"
|
||||
else:
|
||||
time += " 分钟"
|
||||
result = f"{user_name} 已在黑名单!预计 {time}后解封"
|
||||
ban_time += " 分钟"
|
||||
result = f"{user_name} 已在黑名单!预计 {ban_time}后解封"
|
||||
return result
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
from nonebot.adapters.onebot.v11 import Bot, Message, MessageEvent
|
||||
from nonebot import on_command
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.params import CommandArg
|
||||
from utils.utils import get_message_img
|
||||
from services.log import logger
|
||||
from utils.message_builder import image
|
||||
from utils.manager import group_manager
|
||||
from configs.config import Config
|
||||
import asyncio
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Bot, Message, MessageEvent
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from configs.config import Config
|
||||
from services.log import logger
|
||||
from utils.manager import group_manager
|
||||
from utils.message_builder import image
|
||||
from utils.utils import get_message_img
|
||||
|
||||
__zx_plugin_name__ = "广播 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
@@ -28,6 +29,7 @@ Config.add_plugin_config(
|
||||
True,
|
||||
help_="被动 广播 进群默认开关状态",
|
||||
default_value=True,
|
||||
type=bool,
|
||||
)
|
||||
|
||||
broadcast = on_command("广播-", priority=1, permission=SUPERUSER, block=True)
|
||||
@@ -40,11 +42,10 @@ async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
rst = ""
|
||||
for img in img_list:
|
||||
rst += image(img)
|
||||
gl = await bot.get_group_list()
|
||||
gl = [
|
||||
g["group_id"]
|
||||
for g in gl
|
||||
if await group_manager.check_group_task_status(g["group_id"], "broadcast")
|
||||
for g in await bot.get_group_list()
|
||||
if group_manager.check_group_task_status(g["group_id"], "broadcast")
|
||||
]
|
||||
g_cnt = len(gl)
|
||||
cnt = 0
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import nonebot
|
||||
from pathlib import Path
|
||||
|
||||
nonebot.load_plugins("basic_plugins/chat_history")
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from configs.config import Config
|
||||
from models.chat_history import ChatHistory
|
||||
from nonebot import on_message
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
|
||||
from utils.utils import get_message_text
|
||||
|
||||
from configs.config import Config
|
||||
from models.chat_history import ChatHistory
|
||||
from services.log import logger
|
||||
from utils.depends import PlaintText
|
||||
from utils.utils import scheduler
|
||||
|
||||
from ._rule import rule
|
||||
|
||||
@@ -12,21 +15,51 @@ __plugin_author__ = "HibiKier"
|
||||
|
||||
|
||||
Config.add_plugin_config(
|
||||
"chat_history", "FLAG", True, help_="是否开启消息自从存储", name="消息存储", default_value=True
|
||||
"chat_history",
|
||||
"FLAG",
|
||||
True,
|
||||
help_="是否开启消息自从存储",
|
||||
name="消息存储",
|
||||
default_value=True,
|
||||
type=bool,
|
||||
)
|
||||
|
||||
|
||||
chat_history = on_message(rule=rule, priority=1, block=False)
|
||||
|
||||
|
||||
TEMP_LIST = []
|
||||
|
||||
|
||||
@chat_history.handle()
|
||||
async def _(event: MessageEvent):
|
||||
async def _(event: MessageEvent, msg: str = PlaintText()):
|
||||
group_id = None
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
await ChatHistory.add_chat_msg(
|
||||
event.user_id, event.group_id, str(event.get_message()), get_message_text(event.json())
|
||||
)
|
||||
else:
|
||||
await ChatHistory.add_chat_msg(event.user_id, None, str(event.get_message()), get_message_text(event.json()))
|
||||
group_id = event.group_id
|
||||
TEMP_LIST.append(
|
||||
{
|
||||
"user_qq": event.user_id,
|
||||
"group_id": group_id,
|
||||
"text": str(event.get_message()),
|
||||
"plain_text": msg,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
"interval",
|
||||
minutes=1,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
message_list = TEMP_LIST.copy()
|
||||
TEMP_LIST.clear()
|
||||
if message_list:
|
||||
model_list = [ChatHistory(**x) for x in message_list]
|
||||
await ChatHistory.bulk_create(model_list)
|
||||
logger.debug(f"批量添加聊天记录 {len(message_list)} 条", "定时任务")
|
||||
except Exception as e:
|
||||
logger.error(f"定时批量添加聊天记录", "定时任务", e=e)
|
||||
|
||||
|
||||
# @test.handle()
|
||||
|
||||
@@ -1,26 +1,27 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Tuple
|
||||
|
||||
import pytz
|
||||
from models.chat_history import ChatHistory
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from nonebot import on_regex
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent
|
||||
from nonebot.params import RegexGroup
|
||||
from utils.image_utils import BuildImage, text2image
|
||||
from utils.utils import is_number
|
||||
from utils.message_builder import image
|
||||
from typing import Tuple, Any
|
||||
|
||||
from models.chat_history import ChatHistory
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from utils.image_utils import BuildImage, text2image
|
||||
from utils.message_builder import image
|
||||
from utils.utils import is_number
|
||||
|
||||
__zx_plugin_name__ = "消息统计"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
发言记录统计
|
||||
regex:(周|月)?消息排行(des|DES)?(n=[0-9]{1,2})?
|
||||
regex:(周|月|日)?消息排行(des|DES)?(n=[0-9]{1,2})?
|
||||
指令:
|
||||
消息统计?(des)?(n=?)
|
||||
周消息统计?(des)?(n=?)
|
||||
月消息统计?(des)?(n=?)
|
||||
日消息统计?(des)?(n=?)
|
||||
示例:
|
||||
消息统计
|
||||
消息统计des
|
||||
@@ -28,11 +29,7 @@ usage:
|
||||
消息统计n=15
|
||||
""".strip()
|
||||
__plugin_des__ = "发言消息排行"
|
||||
__plugin_cmd__ = [
|
||||
"消息统计",
|
||||
"周消息统计",
|
||||
"月消息统计"
|
||||
]
|
||||
__plugin_cmd__ = ["消息统计", "周消息统计", "月消息统计", "日消息统计"]
|
||||
__plugin_type__ = ("数据统计", 1)
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
@@ -42,7 +39,9 @@ __plugin_settings__ = {
|
||||
}
|
||||
|
||||
|
||||
msg_handler = on_regex(r"^(周|月)?消息统计(des|DES)?(n=[0-9]{1,2})?$", priority=5, block=True)
|
||||
msg_handler = on_regex(
|
||||
r"^(周|月|日)?消息统计(des|DES)?(n=[0-9]{1,2})?$", priority=5, block=True
|
||||
)
|
||||
|
||||
|
||||
@msg_handler.handle()
|
||||
@@ -53,10 +52,16 @@ async def _(event: GroupMessageEvent, reg_group: Tuple[Any, ...] = RegexGroup())
|
||||
num = num.split("=")[-1] if num else 10
|
||||
if num and is_number(num) and 10 < int(num) < 50:
|
||||
num = int(num)
|
||||
if date in ["周"]:
|
||||
date_scope = (datetime.now() - timedelta(days=7), datetime.now())
|
||||
time_now = datetime.now()
|
||||
zero_today = time_now - timedelta(
|
||||
hours=time_now.hour, minutes=time_now.minute, seconds=time_now.second
|
||||
)
|
||||
if date in ["日"]:
|
||||
date_scope = (zero_today, time_now)
|
||||
elif date in ["周"]:
|
||||
date_scope = (time_now - timedelta(days=7), time_now)
|
||||
elif date in ["月"]:
|
||||
date_scope = (datetime.now() - timedelta(days=30), datetime.now())
|
||||
date_scope = (time_now - timedelta(days=30), time_now)
|
||||
if rank_data := await ChatHistory.get_group_msg_rank(
|
||||
gid, num, order or "DESC", date_scope
|
||||
):
|
||||
@@ -64,9 +69,9 @@ async def _(event: GroupMessageEvent, reg_group: Tuple[Any, ...] = RegexGroup())
|
||||
num_str = "发言次数:\n\n"
|
||||
idx = 1
|
||||
for uid, num in rank_data:
|
||||
try:
|
||||
user_name = (await GroupInfoUser.get_member_info(uid, gid)).user_name
|
||||
except AttributeError:
|
||||
if user := await GroupInfoUser.filter(user_qq=uid, group_id=gid).first():
|
||||
user_name = user.user_name
|
||||
else:
|
||||
user_name = uid
|
||||
name += f"\t{idx}.{user_name} \n\n"
|
||||
num_str += f"\t{num}\n\n"
|
||||
@@ -79,7 +84,7 @@ async def _(event: GroupMessageEvent, reg_group: Tuple[Any, ...] = RegexGroup())
|
||||
pytz.timezone("Asia/Shanghai")
|
||||
).replace(microsecond=0)
|
||||
else:
|
||||
date_scope = datetime.now().replace(microsecond=0)
|
||||
date_scope = time_now.replace(microsecond=0)
|
||||
date_str = f"日期:{date_scope} - 至今"
|
||||
else:
|
||||
date_str = f"日期:{date_scope[0].replace(microsecond=0)} - {date_scope[1].replace(microsecond=0)}"
|
||||
|
||||
@@ -1,30 +1,27 @@
|
||||
from nonebot import on_notice, on_request
|
||||
from configs.path_config import IMAGE_PATH, DATA_PATH
|
||||
from models.level_user import LevelUser
|
||||
from utils.message_builder import image
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from datetime import datetime
|
||||
from services.log import logger
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
GroupIncreaseNoticeEvent,
|
||||
GroupDecreaseNoticeEvent,
|
||||
)
|
||||
from nonebot.adapters.onebot.v11.exception import ActionFailed
|
||||
from utils.manager import group_manager, plugins2settings_manager, requests_manager
|
||||
from configs.config import NICKNAME
|
||||
from models.group_info import GroupInfo
|
||||
from utils.utils import FreqLimiter
|
||||
from configs.config import Config
|
||||
from pathlib import Path
|
||||
import random
|
||||
import os
|
||||
import random
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
import ujson as json
|
||||
from nonebot import on_notice, on_request
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
ActionFailed,
|
||||
Bot,
|
||||
GroupDecreaseNoticeEvent,
|
||||
GroupIncreaseNoticeEvent,
|
||||
)
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from configs.path_config import DATA_PATH, IMAGE_PATH
|
||||
from models.group_info import GroupInfo
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from models.level_user import LevelUser
|
||||
from services.log import logger
|
||||
from utils.depends import GetConfig
|
||||
from utils.manager import group_manager, plugins2settings_manager, requests_manager
|
||||
from utils.message_builder import image
|
||||
from utils.utils import FreqLimiter
|
||||
|
||||
__zx_plugin_name__ = "群事件处理 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
@@ -34,10 +31,10 @@ Config.add_plugin_config(
|
||||
"invite_manager", "message", f"请不要未经同意就拉{NICKNAME}入群!告辞!", help_="强制拉群后进群回复的内容.."
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"invite_manager", "flag", True, help_="被强制拉群后是否直接退出", default_value=True
|
||||
"invite_manager", "flag", True, help_="被强制拉群后是否直接退出", default_value=True, type=bool
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"invite_manager", "welcome_msg_cd", 5, help_="群欢迎消息cd", default_value=5
|
||||
"invite_manager", "welcome_msg_cd", 5, help_="群欢迎消息cd", default_value=5, type=int
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"_task",
|
||||
@@ -45,6 +42,7 @@ Config.add_plugin_config(
|
||||
True,
|
||||
help_="被动 进群欢迎 进群默认开关状态",
|
||||
default_value=True,
|
||||
type=bool,
|
||||
)
|
||||
Config.add_plugin_config(
|
||||
"_task",
|
||||
@@ -52,10 +50,11 @@ Config.add_plugin_config(
|
||||
True,
|
||||
help_="被动 退群提醒 进群默认开关状态",
|
||||
default_value=True,
|
||||
type=bool,
|
||||
)
|
||||
|
||||
|
||||
_flmt = FreqLimiter(Config.get_config("invite_manager", "welcome_msg_cd"))
|
||||
_flmt = FreqLimiter(Config.get_config("invite_manager", "welcome_msg_cd") or 5)
|
||||
|
||||
|
||||
# 群员增加处理
|
||||
@@ -69,7 +68,7 @@ add_group = on_request(priority=1, block=False)
|
||||
@group_increase_handle.handle()
|
||||
async def _(bot: Bot, event: GroupIncreaseNoticeEvent):
|
||||
if event.user_id == int(bot.self_id):
|
||||
group = await GroupInfo.get_group_info(event.group_id)
|
||||
group = await GroupInfo.filter(group_id=event.group_id).first()
|
||||
# 群聊不存在或被强制拉群,退出该群
|
||||
if (not group or group.group_flag == 0) and Config.get_config(
|
||||
"invite_manager", "flag"
|
||||
@@ -92,10 +91,10 @@ async def _(bot: Bot, event: GroupIncreaseNoticeEvent):
|
||||
message=f"触发强制入群保护,退出群聊 {event.group_id} 失败..",
|
||||
)
|
||||
# 默认群功能开关
|
||||
elif event.group_id not in group_manager["group_manager"].keys():
|
||||
elif event.group_id not in group_manager.get_data().group_manager.keys():
|
||||
data = plugins2settings_manager.get_data()
|
||||
for plugin in data.keys():
|
||||
if not data[plugin]["default_status"]:
|
||||
if not data[plugin].default_status:
|
||||
group_manager.block_plugin(plugin, event.group_id)
|
||||
# 即刻刷新权限
|
||||
for user_info in await bot.get_group_member_list(group_id=event.group_id):
|
||||
@@ -119,15 +118,21 @@ async def _(bot: Bot, event: GroupIncreaseNoticeEvent):
|
||||
user_info = await bot.get_group_member_info(
|
||||
group_id=event.group_id, user_id=event.user_id
|
||||
)
|
||||
if await GroupInfoUser.add_member_info(
|
||||
user_info["user_id"],
|
||||
user_info["group_id"],
|
||||
user_info["nickname"],
|
||||
join_time,
|
||||
):
|
||||
logger.info(f"用户{user_info['user_id']} 所属{user_info['group_id']} 更新成功")
|
||||
else:
|
||||
logger.info(f"用户{user_info['user_id']} 所属{user_info['group_id']} 更新失败")
|
||||
user, _ = await GroupInfoUser.get_or_create(
|
||||
user_qq=user_info["user_id"],
|
||||
group_id=user_info["group_id"],
|
||||
defaults={
|
||||
"user_name": user_info["nickname"],
|
||||
},
|
||||
)
|
||||
user.user_join_time = join_time
|
||||
await user.save()
|
||||
# await GroupInfoUser.update_or_create(
|
||||
# user_qq=user_info["user_id"],
|
||||
# group_id=user_info["group_id"],
|
||||
# defaults={"user_name": user_info["nickname"], "user_join_time": join_time},
|
||||
# )
|
||||
logger.info(f"用户{user_info['user_id']} 所属{user_info['group_id']} 更新成功")
|
||||
|
||||
# 群欢迎消息
|
||||
if _flmt.check(event.group_id):
|
||||
@@ -156,7 +161,11 @@ async def _(bot: Bot, event: GroupIncreaseNoticeEvent):
|
||||
else:
|
||||
await group_increase_handle.send(
|
||||
"[[_task|group_welcome]]新人快跑啊!!本群现状↓(快使用自定义!)"
|
||||
+ image(random.choice(os.listdir(IMAGE_PATH / "qxz")), "qxz")
|
||||
+ image(
|
||||
IMAGE_PATH
|
||||
/ "qxz"
|
||||
/ random.choice(os.listdir(IMAGE_PATH / "qxz"))
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -166,13 +175,13 @@ async def _(bot: Bot, event: GroupDecreaseNoticeEvent):
|
||||
if event.sub_type == "kick_me":
|
||||
group_id = event.group_id
|
||||
operator_id = event.operator_id
|
||||
try:
|
||||
operator_name = (
|
||||
await GroupInfoUser.get_member_info(event.operator_id, event.group_id)
|
||||
).user_name
|
||||
except AttributeError:
|
||||
if user := await GroupInfoUser.get_or_none(
|
||||
user_qq=event.operator_id, group_id=event.group_id
|
||||
):
|
||||
operator_name = user.user_name
|
||||
else:
|
||||
operator_name = "None"
|
||||
group = await GroupInfo.get_group_info(group_id)
|
||||
group = await GroupInfo.filter(group_id=group_id).first()
|
||||
group_name = group.group_name if group else ""
|
||||
coffee = int(list(bot.config.superusers)[0])
|
||||
await bot.send_private_msg(
|
||||
@@ -186,16 +195,19 @@ async def _(bot: Bot, event: GroupDecreaseNoticeEvent):
|
||||
if event.user_id == int(bot.self_id):
|
||||
group_manager.delete_group(event.group_id)
|
||||
return
|
||||
try:
|
||||
user_name = (
|
||||
await GroupInfoUser.get_member_info(event.user_id, event.group_id)
|
||||
).user_name
|
||||
except AttributeError:
|
||||
user_name = str(event.user_id)
|
||||
if await GroupInfoUser.delete_member_info(event.user_id, event.group_id):
|
||||
logger.info(f"用户{user_name}, qq={event.user_id} 所属{event.group_id} 删除成功")
|
||||
if user := await GroupInfoUser.get_or_none(
|
||||
user_qq=event.user_id, group_id=event.group_id
|
||||
):
|
||||
user_name = user.user_name
|
||||
else:
|
||||
logger.info(f"用户{user_name}, qq={event.user_id} 所属{event.group_id} 删除失败")
|
||||
user_name = f"{event.user_id}"
|
||||
await GroupInfoUser.filter(user_qq=event.user_id, group_id=event.group_id).delete()
|
||||
logger.info(
|
||||
f"名称: {user_name} 退出群聊",
|
||||
"group_decrease_handle",
|
||||
event.user_id,
|
||||
event.group_id,
|
||||
)
|
||||
rst = ""
|
||||
if event.sub_type == "leave":
|
||||
rst = f"{user_name}离开了我们..."
|
||||
|
||||
@@ -6,45 +6,30 @@ from nonebot.adapters.onebot.v11 import (
|
||||
Message
|
||||
)
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.rule import to_me
|
||||
from configs.path_config import IMAGE_PATH, DATA_PATH
|
||||
from utils.message_builder import image
|
||||
from .data_source import create_help_img, get_plugin_help
|
||||
from ._data_source import create_help_img, get_plugin_help
|
||||
from ._utils import GROUP_HELP_PATH
|
||||
import os
|
||||
|
||||
|
||||
__zx_plugin_name__ = "帮助"
|
||||
|
||||
group_help_path = DATA_PATH / "group_help"
|
||||
help_image = IMAGE_PATH / "help.png"
|
||||
__plugin_configs__ = {
|
||||
"TYPE": {"value": "normal", "help": "帮助图片样式 ['normal', 'HTML']", "default_value": "normal"}
|
||||
}
|
||||
|
||||
simple_help_image = IMAGE_PATH / "simple_help.png"
|
||||
if help_image.exists():
|
||||
help_image.unlink()
|
||||
if simple_help_image.exists():
|
||||
simple_help_image.unlink()
|
||||
group_help_path.mkdir(exist_ok=True, parents=True)
|
||||
for x in os.listdir(group_help_path):
|
||||
group_help_image = group_help_path / x
|
||||
group_help_image.unlink()
|
||||
|
||||
_help = on_command("详细功能", rule=to_me(), aliases={"详细帮助"}, priority=1, block=True)
|
||||
|
||||
simple_help = on_command("功能", rule=to_me(), aliases={"help", "帮助"}, priority=1, block=True)
|
||||
|
||||
|
||||
@_help.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
if not help_image.exists():
|
||||
if help_image.exists():
|
||||
help_image.unlink()
|
||||
if simple_help_image.exists():
|
||||
simple_help_image.unlink()
|
||||
await create_help_img(None, help_image, simple_help_image)
|
||||
await _help.finish(image("help.png"))
|
||||
|
||||
|
||||
@simple_help.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State, arg: Message = CommandArg()):
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
is_super = False
|
||||
if msg:
|
||||
@@ -54,20 +39,18 @@ async def _(bot: Bot, event: MessageEvent, state: T_State, arg: Message = Comman
|
||||
msg = msg.replace('-super', '').strip()
|
||||
msg = get_plugin_help(msg, is_super)
|
||||
if msg:
|
||||
await _help.send(image(b64=msg))
|
||||
await simple_help.send(image(b64=msg))
|
||||
else:
|
||||
await _help.send("没有此功能的帮助信息...")
|
||||
await simple_help.send("没有此功能的帮助信息...")
|
||||
else:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
_image_path = group_help_path / f"{event.group_id}.png"
|
||||
_image_path = GROUP_HELP_PATH / f"{event.group_id}.png"
|
||||
if not _image_path.exists():
|
||||
await create_help_img(event.group_id, help_image, _image_path)
|
||||
await create_help_img(event.group_id)
|
||||
await simple_help.send(image(_image_path))
|
||||
else:
|
||||
if not simple_help_image.exists():
|
||||
if help_image.exists():
|
||||
help_image.unlink()
|
||||
if simple_help_image.exists():
|
||||
simple_help_image.unlink()
|
||||
await create_help_img(None, help_image, simple_help_image)
|
||||
await _help.finish(image("simple_help.png"))
|
||||
await create_help_img(None)
|
||||
await simple_help.finish(image("simple_help.png"))
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
from typing import Optional, List, Any, Union, Dict
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
plugin_name: str
|
||||
sta: int
|
||||
|
||||
|
||||
class PluginList(BaseModel):
|
||||
plugin_type: str
|
||||
icon: str
|
||||
logo: str
|
||||
items: List[Item]
|
||||
@@ -0,0 +1,71 @@
|
||||
|
||||
from ._utils import HelpImageBuild
|
||||
from utils.image_utils import BuildImage
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from utils.manager import (
|
||||
plugins2settings_manager,
|
||||
admin_manager,
|
||||
)
|
||||
from typing import Optional
|
||||
import nonebot
|
||||
|
||||
|
||||
random_bk_path = IMAGE_PATH / "background" / "help" / "simple_help"
|
||||
|
||||
background = IMAGE_PATH / "background" / "0.png"
|
||||
|
||||
|
||||
async def create_help_img(group_id: Optional[int]):
|
||||
"""
|
||||
说明:
|
||||
生成帮助图片
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
await HelpImageBuild().build_image(group_id)
|
||||
|
||||
|
||||
def get_plugin_help(msg: str, is_super: bool = False) -> Optional[str]:
|
||||
"""
|
||||
说明:
|
||||
获取功能的帮助信息
|
||||
参数:
|
||||
:param msg: 功能cmd
|
||||
:param is_super: 是否为超级用户
|
||||
"""
|
||||
module = plugins2settings_manager.get_plugin_module(
|
||||
msg
|
||||
) or admin_manager.get_plugin_module(msg)
|
||||
if module:
|
||||
try:
|
||||
plugin = nonebot.plugin.get_plugin(module)
|
||||
metadata = plugin.metadata
|
||||
if plugin:
|
||||
if is_super:
|
||||
result = plugin.module.__getattribute__(
|
||||
"__plugin_superuser_usage__"
|
||||
)
|
||||
else:
|
||||
result = (
|
||||
metadata.usage
|
||||
if metadata
|
||||
else plugin.module.__getattribute__("__plugin_usage__")
|
||||
)
|
||||
if result:
|
||||
width = 0
|
||||
for x in result.split("\n"):
|
||||
_width = len(x) * 24
|
||||
width = width if width > _width else _width
|
||||
height = len(result.split("\n")) * 45
|
||||
A = BuildImage(width, height, font_size=24)
|
||||
bk = BuildImage(
|
||||
width,
|
||||
height,
|
||||
background=IMAGE_PATH / "background" / "1.png",
|
||||
)
|
||||
A.paste(bk, alpha=True)
|
||||
A.text((int(width * 0.048), int(height * 0.21)), result)
|
||||
return A.pic2bs4()
|
||||
except AttributeError:
|
||||
pass
|
||||
return None
|
||||
@@ -0,0 +1,255 @@
|
||||
import os
|
||||
import random
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from configs.config import Config
|
||||
from configs.path_config import DATA_PATH, IMAGE_PATH, TEMPLATE_PATH
|
||||
from utils.decorator import Singleton
|
||||
from utils.image_utils import BuildImage, build_sort_image, group_image
|
||||
from utils.manager import group_manager, plugin_data_manager
|
||||
from utils.manager.models import PluginData, PluginType
|
||||
|
||||
from ._config import Item
|
||||
|
||||
GROUP_HELP_PATH = DATA_PATH / "group_help"
|
||||
GROUP_HELP_PATH.mkdir(exist_ok=True, parents=True)
|
||||
for x in os.listdir(GROUP_HELP_PATH):
|
||||
group_help_image = GROUP_HELP_PATH / x
|
||||
group_help_image.unlink()
|
||||
|
||||
BACKGROUND_PATH = IMAGE_PATH / "background" / "help" / "simple_help"
|
||||
|
||||
LOGO_PATH = TEMPLATE_PATH / "menu" / "res" / "logo"
|
||||
|
||||
|
||||
@Singleton
|
||||
class HelpImageBuild:
|
||||
def __init__(self):
|
||||
self._data: Dict[str, PluginData] = plugin_data_manager.get_data()
|
||||
self._sort_data: Dict[str, List[PluginData]] = {}
|
||||
self._image_list = []
|
||||
self.icon2str = {
|
||||
"normal": "fa fa-cog",
|
||||
"原神相关": "fa fa-circle-o",
|
||||
"常规插件": "fa fa-cubes",
|
||||
"联系管理员": "fa fa-envelope-o",
|
||||
"抽卡相关": "fa fa-credit-card-alt",
|
||||
"来点好康的": "fa fa-picture-o",
|
||||
"数据统计": "fa fa-bar-chart",
|
||||
"一些工具": "fa fa-shopping-cart",
|
||||
"商店": "fa fa-shopping-cart",
|
||||
"其它": "fa fa-tags",
|
||||
"群内小游戏": "fa fa-gamepad",
|
||||
}
|
||||
|
||||
def sort_type(self):
|
||||
"""
|
||||
说明:
|
||||
对插件按照菜单类型分类
|
||||
"""
|
||||
if not self._sort_data.keys():
|
||||
for key in self._data.keys():
|
||||
plugin_data = self._data[key]
|
||||
if plugin_data.plugin_type == PluginType.NORMAL:
|
||||
if not self._sort_data.get(plugin_data.menu_type[0]):
|
||||
self._sort_data[plugin_data.menu_type[0]] = []
|
||||
self._sort_data[plugin_data.menu_type[0]].append(self._data[key])
|
||||
|
||||
async def build_image(self, group_id: Optional[int]):
|
||||
if group_id:
|
||||
help_image = GROUP_HELP_PATH / f"{group_id}.png"
|
||||
else:
|
||||
help_image = IMAGE_PATH / f"simple_help.png"
|
||||
build_type = Config.get_config("help", "TYPE")
|
||||
if build_type == "HTML":
|
||||
byt = await self.build_html_image(group_id)
|
||||
with open(help_image, "wb") as f:
|
||||
f.write(byt)
|
||||
else:
|
||||
img = await self.build_pil_image(group_id)
|
||||
img.save(help_image)
|
||||
|
||||
async def build_html_image(self, group_id: Optional[int]) -> bytes:
|
||||
from nonebot_plugin_htmlrender import template_to_pic
|
||||
self.sort_type()
|
||||
classify = {}
|
||||
for menu in self._sort_data:
|
||||
for plugin in self._sort_data[menu]:
|
||||
sta = 0
|
||||
if not plugin.plugin_status.status:
|
||||
if group_id and plugin.plugin_status.block_type in ["all", "group"]:
|
||||
sta = 2
|
||||
if not group_id and plugin.plugin_status.block_type in [
|
||||
"all",
|
||||
"private",
|
||||
]:
|
||||
sta = 2
|
||||
if group_id and not group_manager.get_plugin_super_status(
|
||||
plugin.model, group_id
|
||||
):
|
||||
sta = 2
|
||||
if group_id and not group_manager.get_plugin_status(
|
||||
plugin.model, group_id
|
||||
):
|
||||
sta = 1
|
||||
if classify.get(menu):
|
||||
classify[menu].append(Item(plugin_name=plugin.name, sta=sta))
|
||||
else:
|
||||
classify[menu] = [Item(plugin_name=plugin.name, sta=sta)]
|
||||
max_len = 0
|
||||
flag_index = -1
|
||||
max_data = None
|
||||
plugin_list = []
|
||||
for index, plu in enumerate(classify.keys()):
|
||||
if plu in self.icon2str.keys():
|
||||
icon = self.icon2str[plu]
|
||||
else:
|
||||
icon = "fa fa-pencil-square-o"
|
||||
logo = LOGO_PATH / random.choice(os.listdir(LOGO_PATH))
|
||||
data = {
|
||||
"name": plu if plu != "normal" else "功能",
|
||||
"items": classify[plu],
|
||||
"icon": icon,
|
||||
"logo": str(logo.absolute()),
|
||||
}
|
||||
if len(classify[plu]) > max_len:
|
||||
max_len = len(classify[plu])
|
||||
flag_index = index
|
||||
max_data = data
|
||||
plugin_list.append(data)
|
||||
del plugin_list[flag_index]
|
||||
plugin_list.insert(0, max_data)
|
||||
pic = await template_to_pic(
|
||||
template_path=str((TEMPLATE_PATH / "menu").absolute()),
|
||||
template_name="zhenxun_menu.html",
|
||||
templates={"plugin_list": plugin_list},
|
||||
pages={
|
||||
"viewport": {"width": 1903, "height": 975},
|
||||
"base_url": f"file://{TEMPLATE_PATH}",
|
||||
},
|
||||
wait=2,
|
||||
)
|
||||
return pic
|
||||
|
||||
async def build_pil_image(self, group_id: Optional[int]) -> BuildImage:
|
||||
"""
|
||||
说明:
|
||||
构造帮助图片
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
self._image_list = []
|
||||
self.sort_type()
|
||||
font_size = 24
|
||||
build_type = Config.get_config("help", "TYPE")
|
||||
_image = BuildImage(0, 0, plain_text="1", font_size=font_size)
|
||||
for idx, menu_type in enumerate(self._sort_data.keys()):
|
||||
plugin_list = self._sort_data[menu_type]
|
||||
wh_list = [_image.getsize(x.name) for x in plugin_list]
|
||||
wh_list.append(_image.getsize(menu_type))
|
||||
# sum_height = sum([x[1] for x in wh_list])
|
||||
if build_type == "VV":
|
||||
sum_height = 50 * len(plugin_list) + 10
|
||||
else:
|
||||
sum_height = (font_size + 6) * len(plugin_list) + 10
|
||||
max_width = max([x[0] for x in wh_list]) + 20
|
||||
bk = BuildImage(
|
||||
max_width + 40,
|
||||
sum_height + 50,
|
||||
font_size=30,
|
||||
color="#a7d1fc",
|
||||
font="CJGaoDeGuo.otf",
|
||||
)
|
||||
title_size = bk.getsize(menu_type)
|
||||
max_width = max_width if max_width > title_size[0] else title_size[0]
|
||||
B = BuildImage(
|
||||
max_width + 40,
|
||||
sum_height,
|
||||
font_size=font_size,
|
||||
color="white" if not idx % 2 else "black",
|
||||
)
|
||||
curr_h = 10
|
||||
for i, plugin_data in enumerate(plugin_list):
|
||||
text_color = (255, 255, 255) if idx % 2 else (0, 0, 0)
|
||||
if group_id and not group_manager.get_plugin_status(
|
||||
plugin_data.model, group_id
|
||||
):
|
||||
text_color = (252, 75, 13)
|
||||
pos = None
|
||||
# 禁用状态划线
|
||||
if (
|
||||
not plugin_data.plugin_status.status
|
||||
and plugin_data.plugin_status.block_type in ["group", "all"]
|
||||
) or (group_id and not group_manager.get_plugin_super_status(
|
||||
plugin_data.model, group_id
|
||||
)):
|
||||
w = curr_h + int(B.getsize(plugin_data.name)[1] / 2) + 2
|
||||
pos = (
|
||||
7,
|
||||
w,
|
||||
B.getsize(plugin_data.name)[0] + 35,
|
||||
w,
|
||||
)
|
||||
if build_type == "VV":
|
||||
name_image = await self.build_name_image(
|
||||
max_width,
|
||||
plugin_data.name,
|
||||
"black" if not idx % 2 else "white",
|
||||
text_color,
|
||||
pos,
|
||||
)
|
||||
await B.apaste(
|
||||
name_image, (0, curr_h), True, center_type="by_width"
|
||||
)
|
||||
curr_h += name_image.h + 5
|
||||
else:
|
||||
await B.atext(
|
||||
(10, curr_h), f"{i + 1}.{plugin_data.name}", text_color
|
||||
)
|
||||
if pos:
|
||||
await B.aline(pos, (236, 66, 7), 3)
|
||||
curr_h += font_size + 5
|
||||
if menu_type == "normal":
|
||||
menu_type = "功能"
|
||||
await bk.atext((0, 14), menu_type, center_type="by_width")
|
||||
await bk.apaste(B, (0, 50))
|
||||
await bk.atransparent(2)
|
||||
# await bk.acircle_corner(point_list=['lt', 'rt'])
|
||||
self._image_list.append(bk)
|
||||
image_group, h = group_image(self._image_list)
|
||||
B = await build_sort_image(
|
||||
image_group,
|
||||
h,
|
||||
background_path=BACKGROUND_PATH,
|
||||
background_handle=lambda image: image.filter("GaussianBlur", 5),
|
||||
)
|
||||
w = 10
|
||||
h = 10
|
||||
for msg in [
|
||||
"目前支持的功能列表:",
|
||||
"可以通过 ‘帮助[功能名称]’ 来获取对应功能的使用方法",
|
||||
]:
|
||||
text = BuildImage(
|
||||
0,
|
||||
0,
|
||||
plain_text=msg,
|
||||
font_size=24,
|
||||
font="HYWenHei-85W.ttf",
|
||||
)
|
||||
B.paste(text, (w, h), True)
|
||||
h += 50
|
||||
if msg == "目前支持的功能列表:":
|
||||
w += 50
|
||||
await B.apaste(
|
||||
BuildImage(
|
||||
0,
|
||||
0,
|
||||
plain_text="注: 红字代表功能被群管理员禁用,红线代表功能正在维护",
|
||||
font_size=24,
|
||||
font="HYWenHei-85W.ttf",
|
||||
font_color=(231, 74, 57),
|
||||
),
|
||||
(300, 10),
|
||||
True,
|
||||
)
|
||||
return B
|
||||
@@ -1,370 +0,0 @@
|
||||
from utils.image_utils import BuildImage
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from utils.manager import (
|
||||
plugins2settings_manager,
|
||||
admin_manager,
|
||||
plugins_manager,
|
||||
group_manager,
|
||||
)
|
||||
from typing import Optional
|
||||
from services.log import logger
|
||||
from pathlib import Path
|
||||
from utils.utils import get_matchers
|
||||
import random
|
||||
import asyncio
|
||||
import nonebot
|
||||
import os
|
||||
|
||||
|
||||
random_bk_path = IMAGE_PATH / "background" / "help" / "simple_help"
|
||||
|
||||
background = IMAGE_PATH / "background" / "0.png"
|
||||
|
||||
|
||||
async def create_help_img(
|
||||
group_id: Optional[int], help_image: Path, simple_help_image: Path
|
||||
):
|
||||
"""
|
||||
生成帮助图片
|
||||
:param group_id: 群号
|
||||
:param help_image: 图片路径
|
||||
:param simple_help_image: 简易帮助图片路径
|
||||
"""
|
||||
return await asyncio.get_event_loop().run_in_executor(
|
||||
None, _create_help_img, group_id, help_image, simple_help_image
|
||||
)
|
||||
|
||||
|
||||
def _create_help_img(
|
||||
group_id: Optional[int], help_image: Path, simple_help_image: Path
|
||||
):
|
||||
"""
|
||||
生成帮助图片
|
||||
:param group_id: 群号
|
||||
:param help_image: 图片路径
|
||||
:param simple_help_image: 简易帮助图片路径
|
||||
"""
|
||||
_matchers = get_matchers()
|
||||
width = 0
|
||||
matchers_data = {}
|
||||
_des_tmp = {}
|
||||
_plugin_name_tmp = []
|
||||
_tmp = []
|
||||
tmp_img = BuildImage(0, 0, plain_text="1", font_size=24)
|
||||
font_height = tmp_img.h
|
||||
# 插件分类
|
||||
for matcher in _matchers:
|
||||
plugin_name = None
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
if not _plugin:
|
||||
logger.warning(f"获取 功能:{matcher.plugin_name} 失败...")
|
||||
continue
|
||||
_module = _plugin.module
|
||||
try:
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
try:
|
||||
plugin_des = _module.__getattribute__("__plugin_des__")
|
||||
except AttributeError:
|
||||
plugin_des = "_"
|
||||
if (
|
||||
"[hidden]" in plugin_name.lower()
|
||||
or "[admin]" in plugin_name.lower()
|
||||
or "[superuser]" in plugin_name.lower()
|
||||
or plugin_name in _plugin_name_tmp
|
||||
or plugin_name == "帮助"
|
||||
):
|
||||
continue
|
||||
plugin_type = ("normal",)
|
||||
text_type = 0
|
||||
if plugins2settings_manager.get(
|
||||
matcher.plugin_name
|
||||
) and plugins2settings_manager[matcher.plugin_name].get("plugin_type"):
|
||||
plugin_type = tuple(
|
||||
plugins2settings_manager.get_plugin_data(matcher.plugin_name)[
|
||||
"plugin_type"
|
||||
]
|
||||
)
|
||||
else:
|
||||
try:
|
||||
plugin_type = _module.__getattribute__("__plugin_type__")
|
||||
except AttributeError:
|
||||
pass
|
||||
if len(plugin_type) > 1:
|
||||
try:
|
||||
text_type = int(plugin_type[1])
|
||||
except ValueError as e:
|
||||
logger.warning(f"生成列向帮助排列失败 {plugin_name}: {type(e)}: {e}")
|
||||
plugin_type = plugin_type[0]
|
||||
else:
|
||||
plugin_type = plugin_type[0]
|
||||
try:
|
||||
plugin_cmd = _module.__getattribute__("__plugin_cmd__")
|
||||
plugin_cmd = [x for x in plugin_cmd if "[_superuser]" not in x]
|
||||
except AttributeError:
|
||||
plugin_cmd = []
|
||||
if plugin_type not in matchers_data.keys():
|
||||
matchers_data[plugin_type] = {}
|
||||
if plugin_des in _des_tmp.keys():
|
||||
try:
|
||||
matchers_data[plugin_type][_des_tmp[plugin_des]]["cmd"] = (
|
||||
matchers_data[plugin_type][_des_tmp[plugin_des]]["cmd"]
|
||||
+ plugin_cmd
|
||||
)
|
||||
except KeyError as e:
|
||||
logger.warning(f"{type(e)}: {e}")
|
||||
else:
|
||||
matchers_data[plugin_type][plugin_name] = {
|
||||
"modules": matcher.plugin_name,
|
||||
"des": plugin_des,
|
||||
"cmd": plugin_cmd,
|
||||
"text_type": text_type,
|
||||
}
|
||||
try:
|
||||
if text_type == 0:
|
||||
x = tmp_img.getsize(
|
||||
f'{plugin_name}: {matchers_data[plugin_type][plugin_name]["des"]} ->'
|
||||
+ " / ".join(matchers_data[plugin_type][plugin_name]["cmd"])
|
||||
)[0]
|
||||
width = width if width > x else x
|
||||
except KeyError:
|
||||
pass
|
||||
if plugin_des not in _des_tmp:
|
||||
_des_tmp[plugin_des] = plugin_name
|
||||
except AttributeError as e:
|
||||
if plugin_name not in _plugin_name_tmp:
|
||||
logger.warning(f"获取功能 {matcher.plugin_name}: {plugin_name} 设置失败...e:{e}")
|
||||
if plugin_name not in _plugin_name_tmp:
|
||||
_plugin_name_tmp.append(plugin_name)
|
||||
help_img_list = []
|
||||
simple_help_img_list = []
|
||||
types = list(matchers_data.keys())
|
||||
types.sort()
|
||||
ix = 0
|
||||
# 详细帮助
|
||||
for type_ in types:
|
||||
keys = list(matchers_data[type_].keys())
|
||||
keys.sort()
|
||||
help_str = f"{type_ if type_ != 'normal' else '功能'}:\n\n"
|
||||
simple_help_str = f"{type_ if type_ != 'normal' else '功能'}:\n\n"
|
||||
for i, k in enumerate(keys):
|
||||
# 禁用flag
|
||||
flag = True
|
||||
if plugins_manager.get_plugin_status(
|
||||
matchers_data[type_][k]["modules"], "all"
|
||||
):
|
||||
flag = False
|
||||
if group_id:
|
||||
flag = flag and plugins_manager.get_plugin_status(
|
||||
matchers_data[type_][k]["modules"], "group"
|
||||
)
|
||||
simple_help_str += (
|
||||
f"{i+1}.{k}<|_|~|>"
|
||||
f"{group_manager.get_plugin_status(matchers_data[type_][k]['modules'], group_id) if group_id else '_'}|"
|
||||
f"{flag}\n"
|
||||
)
|
||||
if matchers_data[type_][k]["text_type"] == 1:
|
||||
_x = tmp_img.getsize(
|
||||
f"{i+1}".rjust(5)
|
||||
+ f'.{k}: {matchers_data[type_][k]["des"]} {"->" if matchers_data[type_][k]["cmd"] else ""} '
|
||||
)[0]
|
||||
_str = (
|
||||
f"{i+1}".rjust(5)
|
||||
+ f'.{k}: {matchers_data[type_][k]["des"]} {"->" if matchers_data[type_][k]["cmd"] else ""} '
|
||||
)
|
||||
_str += matchers_data[type_][k]["cmd"][0] + "\n"
|
||||
for c in matchers_data[type_][k]["cmd"][1:]:
|
||||
_str += "".rjust(int(_x * 0.125) + 1) + f"{c}\n"
|
||||
help_str += _str
|
||||
else:
|
||||
help_str += (
|
||||
f"{i+1}".rjust(5)
|
||||
+ f'.{k}: {matchers_data[type_][k]["des"]} {"->" if matchers_data[type_][k]["cmd"] else ""} '
|
||||
+ " / ".join(matchers_data[type_][k]["cmd"])
|
||||
+ "\n"
|
||||
)
|
||||
height = len(help_str.split("\n")) * (font_height + 5)
|
||||
simple_height = len(simple_help_str.split("\n")) * (font_height + 5)
|
||||
A = BuildImage(
|
||||
width + 150, height, font_size=24, color="white" if not ix % 2 else "black"
|
||||
)
|
||||
A.text((10, 10), help_str, (255, 255, 255) if ix % 2 else (0, 0, 0))
|
||||
# 生成各个分类的插件简易帮助图片
|
||||
simple_width = 0
|
||||
for x in [
|
||||
tmp_img.getsize(x.split("<|_|~|>")[0])[0]
|
||||
for x in simple_help_str.split("\n")
|
||||
]:
|
||||
simple_width = simple_width if simple_width > x else x
|
||||
bk = BuildImage(simple_width + 20, simple_height, font_size=24, color="#6495ED")
|
||||
B = BuildImage(
|
||||
simple_width + 20,
|
||||
simple_height,
|
||||
font_size=24,
|
||||
color="white" if not ix % 2 else "black",
|
||||
)
|
||||
# 切分,判断插件开关状态
|
||||
_s_height = 10
|
||||
for _s in simple_help_str.split("\n"):
|
||||
text_color = (255, 255, 255) if ix % 2 else (0, 0, 0)
|
||||
_line_flag = False
|
||||
if "<|_|~|>" in _s:
|
||||
_x = _s.split("<|_|~|>")
|
||||
_flag_sp = _x[-1].split("|")
|
||||
if group_id:
|
||||
if _flag_sp[0].lower() != "true":
|
||||
text_color = (252, 75, 13)
|
||||
if _flag_sp[1].lower() == "true":
|
||||
_line_flag = True
|
||||
_s = _x[0]
|
||||
B.text((10, _s_height), _s, text_color)
|
||||
if _line_flag:
|
||||
B.line(
|
||||
(
|
||||
7,
|
||||
_s_height + int(B.getsize(_s)[1] / 2) + 2,
|
||||
B.getsize(_s)[0] + 11,
|
||||
_s_height + int(B.getsize(_s)[1] / 2) + 2,
|
||||
),
|
||||
(236, 66, 7),
|
||||
3,
|
||||
)
|
||||
_s_height += B.getsize("1")[1] + 5
|
||||
# B.text((10, 10), simple_help_str, (255, 255, 255) if ix % 2 else (0, 0, 0))
|
||||
bk.paste(B, center_type="center")
|
||||
bk.transparent(2)
|
||||
ix += 1
|
||||
help_img_list.append(A)
|
||||
simple_help_img_list.append(bk)
|
||||
height = 0
|
||||
for img in help_img_list:
|
||||
height += img.h
|
||||
if not group_id:
|
||||
A = BuildImage(width + 150, height + 50, font_size=24)
|
||||
A.text(
|
||||
(10, 10), '* 注: ‘*’ 代表可有多个相同参数 ‘?’ 代表可省略该参数 *\n\n" "功能名: 功能简介 -> 指令\n\n'
|
||||
)
|
||||
current_height = 50
|
||||
for img in help_img_list:
|
||||
A.paste(img, (0, current_height))
|
||||
current_height += img.h
|
||||
A.save(help_image)
|
||||
# 详细帮助生成完毕
|
||||
# 简易帮助图片合成
|
||||
height = 0
|
||||
width = 0
|
||||
for img in simple_help_img_list:
|
||||
if img.h > height:
|
||||
height = img.h
|
||||
width += img.w + 10
|
||||
B = BuildImage(width + 100, height + 250, font_size=24)
|
||||
width, _ = get_max_width_or_paste(simple_help_img_list, B)
|
||||
bk = None
|
||||
random_bk = os.listdir(random_bk_path)
|
||||
if random_bk:
|
||||
bk = random.choice(random_bk)
|
||||
x = max(width + 50, height + 250)
|
||||
B = BuildImage(
|
||||
x,
|
||||
x,
|
||||
font_size=24,
|
||||
color="#FFEFD5",
|
||||
background=random_bk_path / bk,
|
||||
)
|
||||
B.filter("GaussianBlur", 10)
|
||||
_, B = get_max_width_or_paste(simple_help_img_list, B, True)
|
||||
w = 10
|
||||
h = 10
|
||||
for msg in ["目前支持的功能列表:", "可以通过 ‘帮助[功能名称]’ 来获取对应功能的使用方法", "或者使用 ‘详细帮助’ 来获取所有功能方法"]:
|
||||
text = BuildImage(
|
||||
0,
|
||||
0,
|
||||
plain_text=msg,
|
||||
font_size=24,
|
||||
font="HYWenHei-85W.ttf",
|
||||
)
|
||||
B.paste(text, (w, h), True)
|
||||
h += 50
|
||||
if msg == "目前支持的功能列表:":
|
||||
w += 50
|
||||
B.paste(
|
||||
BuildImage(
|
||||
0,
|
||||
0,
|
||||
plain_text="注: 红字代表功能被群管理员禁用,红线代表功能正在维护",
|
||||
font_size=24,
|
||||
font="HYWenHei-85W.ttf",
|
||||
font_color=(231, 74, 57)
|
||||
),
|
||||
(300, 10),
|
||||
True,
|
||||
)
|
||||
B.save(simple_help_image)
|
||||
|
||||
|
||||
def get_max_width_or_paste(
|
||||
simple_help_img_list: list, B: BuildImage = None, is_paste: bool = False
|
||||
) -> "int, BuildImage":
|
||||
"""
|
||||
获取最大宽度,或直接贴图
|
||||
:param simple_help_img_list: 简单帮助图片列表
|
||||
:param B: 背景图
|
||||
:param is_paste: 是否直接贴图
|
||||
"""
|
||||
current_width = 50
|
||||
current_height = 180
|
||||
max_width = simple_help_img_list[0].w
|
||||
for i in range(len(simple_help_img_list)):
|
||||
try:
|
||||
if is_paste and B:
|
||||
B.paste(simple_help_img_list[i], (current_width, current_height), True)
|
||||
current_height += simple_help_img_list[i].h + 40
|
||||
if current_height + simple_help_img_list[i + 1].h > B.h - 10:
|
||||
current_height = 180
|
||||
current_width += max_width + 30
|
||||
max_width = 0
|
||||
elif simple_help_img_list[i].w > max_width:
|
||||
max_width = simple_help_img_list[i].w
|
||||
except IndexError:
|
||||
pass
|
||||
if current_width > simple_help_img_list[0].w + 50:
|
||||
current_width += simple_help_img_list[-1].w
|
||||
return current_width, B
|
||||
|
||||
|
||||
def get_plugin_help(msg: str, is_super: bool = False) -> Optional[str]:
|
||||
"""
|
||||
获取功能的帮助信息
|
||||
:param msg: 功能cmd
|
||||
:param is_super: 是否为超级用户
|
||||
"""
|
||||
module = plugins2settings_manager.get_plugin_module(msg)
|
||||
if not module:
|
||||
module = admin_manager.get_plugin_module(msg)
|
||||
if module:
|
||||
try:
|
||||
plugin = nonebot.plugin.get_plugin(module)
|
||||
if plugin:
|
||||
if is_super:
|
||||
result = plugin.module.__getattribute__(
|
||||
"__plugin_superuser_usage__"
|
||||
)
|
||||
else:
|
||||
result = plugin.module.__getattribute__("__plugin_usage__")
|
||||
if result:
|
||||
width = 0
|
||||
for x in result.split("\n"):
|
||||
_width = len(x) * 24
|
||||
width = width if width > _width else _width
|
||||
height = len(result.split("\n")) * 45
|
||||
A = BuildImage(width, height, font_size=24)
|
||||
bk = BuildImage(
|
||||
width,
|
||||
height,
|
||||
background=IMAGE_PATH / "background" / "1.png",
|
||||
)
|
||||
A.paste(bk, alpha=True)
|
||||
A.text((int(width * 0.048), int(height * 0.21)), result)
|
||||
return A.pic2bs4()
|
||||
except AttributeError:
|
||||
pass
|
||||
return None
|
||||
@@ -1,13 +1,13 @@
|
||||
from configs.config import Config
|
||||
|
||||
|
||||
Config.add_plugin_config(
|
||||
"hook",
|
||||
"CHECK_NOTICE_INFO_CD",
|
||||
300,
|
||||
name="基础hook配置",
|
||||
help_="群检测,个人权限检测等各种检测提示信息cd",
|
||||
default_value=300
|
||||
default_value=300,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -15,7 +15,8 @@ Config.add_plugin_config(
|
||||
"MALICIOUS_BAN_TIME",
|
||||
30,
|
||||
help_="恶意命令触发检测触发后ban的时长(分钟)",
|
||||
default_value=30
|
||||
default_value=30,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -23,14 +24,10 @@ Config.add_plugin_config(
|
||||
"MALICIOUS_CHECK_TIME",
|
||||
5,
|
||||
help_="恶意命令触发检测规定时间内(秒)",
|
||||
default_value=5
|
||||
default_value=5,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
"hook",
|
||||
"MALICIOUS_BAN_COUNT",
|
||||
6,
|
||||
help_="恶意命令触发检测最大触发次数",
|
||||
default_value=6
|
||||
"hook", "MALICIOUS_BAN_COUNT", 6, help_="恶意命令触发检测最大触发次数", default_value=6, type=int
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,40 @@
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, PrivateMessageEvent
|
||||
from utils.manager import plugins2block_manager, StaticData
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import nonebot
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
Event,
|
||||
GroupMessageEvent,
|
||||
Message,
|
||||
MessageEvent,
|
||||
PokeNotifyEvent,
|
||||
PrivateMessageEvent,
|
||||
)
|
||||
from nonebot.exception import ActionFailed, IgnoredException
|
||||
from nonebot.internal.matcher import Matcher
|
||||
|
||||
from configs.config import Config
|
||||
from models.bag_user import BagUser
|
||||
from models.ban_user import BanUser
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from models.level_user import LevelUser
|
||||
from models.user_shop_gold_log import UserShopGoldLog
|
||||
from services.log import logger
|
||||
from utils.decorator import Singleton
|
||||
from utils.manager import (
|
||||
StaticData,
|
||||
admin_manager,
|
||||
group_manager,
|
||||
plugins2block_manager,
|
||||
plugins2cd_manager,
|
||||
plugins2count_manager,
|
||||
plugins2settings_manager,
|
||||
plugins_manager,
|
||||
)
|
||||
from utils.message_builder import at
|
||||
from utils.utils import FreqLimiter
|
||||
|
||||
ignore_rst_module = ["ai", "poke", "dialogue"]
|
||||
|
||||
@@ -8,7 +42,6 @@ other_limit_plugins = ["poke"]
|
||||
|
||||
|
||||
class StatusMessageManager(StaticData):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(None)
|
||||
|
||||
@@ -39,8 +72,8 @@ def set_block_limit_false(event, module):
|
||||
"""
|
||||
if plugins2block_manager.check_plugin_block_status(module):
|
||||
plugin_block_data = plugins2block_manager.get_plugin_block_data(module)
|
||||
check_type = plugin_block_data["check_type"]
|
||||
limit_type = plugin_block_data["limit_type"]
|
||||
check_type = plugin_block_data.check_type
|
||||
limit_type = plugin_block_data.limit_type
|
||||
if not (
|
||||
(isinstance(event, GroupMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, PrivateMessageEvent) and check_type == "group")
|
||||
@@ -50,3 +83,443 @@ def set_block_limit_false(event, module):
|
||||
block_type_ = event.group_id
|
||||
plugins2block_manager.set_false(block_type_, module)
|
||||
|
||||
|
||||
async def send_msg(msg: str, bot: Bot, event: MessageEvent):
|
||||
"""
|
||||
说明:
|
||||
发送信息
|
||||
参数:
|
||||
:param msg: pass
|
||||
:param bot: pass
|
||||
:param event: pass
|
||||
"""
|
||||
if "[uname]" in msg:
|
||||
uname = event.sender.card or event.sender.nickname
|
||||
msg = msg.replace("[uname]", uname)
|
||||
if "[nickname]" in msg:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
nickname = await GroupInfoUser.get_user_nickname(
|
||||
event.user_id, event.group_id
|
||||
)
|
||||
else:
|
||||
nickname = await FriendUser.get_user_nickname(event.user_id)
|
||||
msg = msg.replace("[nickname]", nickname)
|
||||
if "[at]" in msg and isinstance(event, GroupMessageEvent):
|
||||
msg = msg.replace("[at]", str(at(event.user_id)))
|
||||
try:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
status_message_manager.add(event.group_id)
|
||||
await bot.send_group_msg(group_id=event.group_id, message=Message(msg))
|
||||
else:
|
||||
status_message_manager.add(event.user_id)
|
||||
await bot.send_private_msg(user_id=event.user_id, message=Message(msg))
|
||||
except ActionFailed:
|
||||
pass
|
||||
|
||||
|
||||
class IsSuperuserException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@Singleton
|
||||
class AuthChecker:
|
||||
"""
|
||||
权限检查
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._flmt = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
self._flmt_g = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
self._flmt_s = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
self._flmt_c = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
|
||||
async def auth(self, matcher: Matcher, bot: Bot, event: Event):
|
||||
"""
|
||||
说明:
|
||||
权限检查
|
||||
参数:
|
||||
:param matcher: matcher
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
try:
|
||||
if plugin_name := matcher.plugin_name:
|
||||
cost_gold = await self.auth_cost(plugin_name, bot, event)
|
||||
user_id = getattr(event, "user_id", None)
|
||||
if user_id and str(user_id) not in bot.config.superusers:
|
||||
await self.auth_basic(plugin_name, bot, event)
|
||||
self.auth_group(plugin_name, bot, event)
|
||||
await self.auth_admin(plugin_name, matcher, bot, event)
|
||||
await self.auth_plugin(plugin_name, matcher, bot, event)
|
||||
await self.auth_limit(plugin_name, bot, event)
|
||||
if cost_gold:
|
||||
await BagUser.spend_gold(
|
||||
event.user_id, event.group_id, cost_gold
|
||||
)
|
||||
except IsSuperuserException:
|
||||
return
|
||||
|
||||
async def auth_limit(self, plugin_name: str, bot: Bot, event: Event):
|
||||
"""
|
||||
说明:
|
||||
插件限制
|
||||
参数:
|
||||
:param plugin_name: 模块名
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
if plugins2cd_manager.check_plugin_cd_status(plugin_name):
|
||||
plugin_cd_data = plugins2cd_manager.get_plugin_cd_data(plugin_name)
|
||||
check_type = plugin_cd_data.check_type
|
||||
limit_type = plugin_cd_data.limit_type
|
||||
msg = plugin_cd_data.rst
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or plugins2cd_manager.get_plugin_data(plugin_name).check_type == "all"
|
||||
):
|
||||
cd_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
cd_type_ = event.group_id
|
||||
if not plugins2cd_manager.check(plugin_name, cd_type_):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event)
|
||||
raise IgnoredException(f"{plugin_name} 正在cd中...")
|
||||
else:
|
||||
plugins2cd_manager.start_cd(plugin_name, cd_type_)
|
||||
# Block
|
||||
if plugins2block_manager.check_plugin_block_status(plugin_name):
|
||||
plugin_block_data = plugins2block_manager.get_plugin_block_data(plugin_name)
|
||||
check_type = plugin_block_data.check_type
|
||||
limit_type = plugin_block_data.limit_type
|
||||
msg = plugin_block_data.rst
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or check_type == "all"
|
||||
):
|
||||
block_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
block_type_ = event.group_id
|
||||
if plugins2block_manager.check(block_type_, plugin_name):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event)
|
||||
raise IgnoredException(f"{event.user_id}正在调用{plugin_name}....")
|
||||
else:
|
||||
plugins2block_manager.set_true(block_type_, plugin_name)
|
||||
# Count
|
||||
if (
|
||||
plugins2count_manager.check_plugin_count_status(plugin_name)
|
||||
and event.user_id not in bot.config.superusers
|
||||
):
|
||||
plugin_count_data = plugins2count_manager.get_plugin_count_data(plugin_name)
|
||||
limit_type = plugin_count_data.limit_type
|
||||
msg = plugin_count_data.rst
|
||||
count_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
count_type_ = event.group_id
|
||||
if not plugins2count_manager.check(plugin_name, count_type_):
|
||||
if msg:
|
||||
await send_msg(msg, bot, event)
|
||||
raise IgnoredException(f"{plugin_name} count次数限制...")
|
||||
else:
|
||||
plugins2count_manager.increase(plugin_name, count_type_)
|
||||
|
||||
async def auth_plugin(
|
||||
self, plugin_name: str, matcher: Matcher, bot: Bot, event: Event
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
插件状态
|
||||
参数:
|
||||
:param plugin_name: 模块名
|
||||
:param matcher: matcher
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
if plugin_name in plugins2settings_manager.keys() and matcher.priority not in [
|
||||
1,
|
||||
999,
|
||||
]:
|
||||
# 戳一戳单独判断
|
||||
if (
|
||||
isinstance(event, GroupMessageEvent)
|
||||
or isinstance(event, PokeNotifyEvent)
|
||||
or matcher.plugin_name in other_limit_plugins
|
||||
):
|
||||
if status_message_manager.get(event.group_id) is None:
|
||||
status_message_manager.delete(event.group_id)
|
||||
if plugins2settings_manager[
|
||||
plugin_name
|
||||
].level > group_manager.get_group_level(event.group_id):
|
||||
try:
|
||||
if (
|
||||
self._flmt_g.check(event.user_id)
|
||||
and plugin_name not in ignore_rst_module
|
||||
):
|
||||
self._flmt_g.start_cd(event.user_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="群权限不足..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
raise IgnoredException("群权限不足")
|
||||
# 插件状态
|
||||
if not group_manager.get_plugin_status(plugin_name, event.group_id):
|
||||
try:
|
||||
if plugin_name not in ignore_rst_module and self._flmt_s.check(
|
||||
event.group_id
|
||||
):
|
||||
self._flmt_s.start_cd(event.group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="该群未开启此功能.."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
raise IgnoredException("未开启此功能...")
|
||||
# 管理员禁用
|
||||
if not group_manager.get_plugin_status(
|
||||
f"{plugin_name}:super", event.group_id
|
||||
):
|
||||
try:
|
||||
if (
|
||||
self._flmt_s.check(event.group_id)
|
||||
and plugin_name not in ignore_rst_module
|
||||
):
|
||||
self._flmt_s.start_cd(event.group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="管理员禁用了此群该功能..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
raise IgnoredException("管理员禁用了此群该功能...")
|
||||
# 群聊禁用
|
||||
if not plugins_manager.get_plugin_status(
|
||||
plugin_name, block_type="group"
|
||||
):
|
||||
try:
|
||||
if (
|
||||
self._flmt_c.check(event.group_id)
|
||||
and plugin_name not in ignore_rst_module
|
||||
):
|
||||
self._flmt_c.start_cd(event.group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="该功能在群聊中已被禁用..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
raise IgnoredException("该插件在群聊中已被禁用...")
|
||||
else:
|
||||
# 私聊禁用
|
||||
if not plugins_manager.get_plugin_status(
|
||||
plugin_name, block_type="private"
|
||||
):
|
||||
try:
|
||||
if self._flmt_c.check(event.user_id):
|
||||
self._flmt_c.start_cd(event.user_id)
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id, message="该功能在私聊中已被禁用..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.user_id)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
raise IgnoredException("该插件在私聊中已被禁用...")
|
||||
# 维护
|
||||
if not plugins_manager.get_plugin_status(plugin_name, block_type="all"):
|
||||
if isinstance(
|
||||
event, GroupMessageEvent
|
||||
) and group_manager.check_group_is_white(event.group_id):
|
||||
raise IsSuperuserException()
|
||||
try:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
if (
|
||||
self._flmt_c.check(event.group_id)
|
||||
and plugin_name not in ignore_rst_module
|
||||
):
|
||||
self._flmt_c.start_cd(event.group_id)
|
||||
logger.info(f"{event.user_id} ||XXXXXX: {matcher.module}")
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="此功能正在维护..."
|
||||
)
|
||||
else:
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id, message="此功能正在维护..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
id_ = (
|
||||
event.group_id
|
||||
if isinstance(event, GroupMessageEvent)
|
||||
else event.user_id
|
||||
)
|
||||
status_message_manager.add(id_)
|
||||
set_block_limit_false(event, plugin_name)
|
||||
raise IgnoredException("此功能正在维护...")
|
||||
|
||||
async def auth_admin(
|
||||
self, plugin_name: str, matcher: Matcher, bot: Bot, event: Event
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
管理员命令 个人权限
|
||||
参数:
|
||||
:param plugin_name: 模块名
|
||||
:param matcher: matcher
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
if plugin_name in admin_manager.keys() and matcher.priority not in [1, 999]:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
# 个人权限
|
||||
if (
|
||||
not await LevelUser.check_level(
|
||||
event.user_id,
|
||||
event.group_id,
|
||||
admin_manager.get_plugin_level(plugin_name),
|
||||
)
|
||||
and admin_manager.get_plugin_level(plugin_name) > 0
|
||||
):
|
||||
try:
|
||||
if self._flmt.check(event.user_id):
|
||||
self._flmt.start_cd(event.user_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id,
|
||||
message=f"{at(event.user_id)}你的权限不足喔,该功能需要的权限等级:"
|
||||
f"{admin_manager.get_plugin_level(plugin_name)}",
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
set_block_limit_false(event, plugin_name)
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
raise IgnoredException("权限不足")
|
||||
else:
|
||||
if not await LevelUser.check_level(
|
||||
event.user_id, 0, admin_manager.get_plugin_level(plugin_name)
|
||||
):
|
||||
try:
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id,
|
||||
message=f"你的权限不足喔,该功能需要的权限等级:{admin_manager.get_plugin_level(plugin_name)}",
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
set_block_limit_false(event, plugin_name)
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.user_id)
|
||||
raise IgnoredException("权限不足")
|
||||
|
||||
def auth_group(self, plugin_name: str, bot: Bot, event: Event):
|
||||
"""
|
||||
说明:
|
||||
群黑名单检测 群总开关检测
|
||||
参数:
|
||||
:param plugin_name: 模块名
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
if isinstance(event, GroupMessageEvent) or plugin_name in other_limit_plugins:
|
||||
try:
|
||||
if (
|
||||
group_manager.get_group_level(event.group_id) < 0
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
):
|
||||
raise IgnoredException("群黑名单")
|
||||
if not group_manager.check_group_bot_status(event.group_id):
|
||||
try:
|
||||
if str(event.get_message()) != "醒来":
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
except ValueError:
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
async def auth_basic(self, plugin_name: str, bot: Bot, event: Event):
|
||||
"""
|
||||
说明:
|
||||
检测是否满足超级用户权限,是否被ban等
|
||||
参数:
|
||||
:param plugin_name: 模块名
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
try:
|
||||
if (
|
||||
(
|
||||
not isinstance(event, MessageEvent)
|
||||
and plugin_name not in other_limit_plugins
|
||||
)
|
||||
or await BanUser.is_ban(event.user_id)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
) or (
|
||||
str(event.user_id) in bot.config.superusers
|
||||
and plugins2settings_manager.get_plugin_data(plugin_name)
|
||||
and not plugins2settings_manager.get_plugin_data(
|
||||
plugin_name
|
||||
).limit_superuser
|
||||
):
|
||||
raise IsSuperuserException()
|
||||
_plugin = nonebot.plugin.get_plugin(plugin_name)
|
||||
_module = _plugin.module
|
||||
_plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
if (
|
||||
"[superuser]" in _plugin_name.lower()
|
||||
and str(event.user_id) in bot.config.superusers
|
||||
):
|
||||
raise IsSuperuserException()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
async def auth_cost(self, plugin_name: str, bot: Bot, event: Event) -> int:
|
||||
"""
|
||||
说明:
|
||||
检测是否满足金币条件
|
||||
参数:
|
||||
:param plugin_name: 模块名
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
"""
|
||||
cost_gold = 0
|
||||
if isinstance(event, GroupMessageEvent) and (
|
||||
psm := plugins2settings_manager.get_plugin_data(plugin_name)
|
||||
):
|
||||
if psm.cost_gold > 0:
|
||||
if (
|
||||
await BagUser.get_gold(event.user_id, event.group_id)
|
||||
< psm.cost_gold
|
||||
):
|
||||
await send_msg(f"金币不足..该功能需要{psm.cost_gold}金币..", bot, event)
|
||||
raise IgnoredException(f"{plugin_name} 金币限制...")
|
||||
# 当插件不阻塞超级用户时,超级用户提前扣除金币
|
||||
if (
|
||||
str(event.user_id) in bot.config.superusers
|
||||
and not psm.limit_superuser
|
||||
):
|
||||
await BagUser.spend_gold(
|
||||
event.user_id, event.group_id, psm.cost_gold
|
||||
)
|
||||
await UserShopGoldLog.create(
|
||||
user_qq=event.user_id,
|
||||
group_id=event.group_id,
|
||||
type=2,
|
||||
name=plugin_name,
|
||||
num=1,
|
||||
spend_gold=psm.cost_gold,
|
||||
)
|
||||
cost_gold = psm.cost_gold
|
||||
return cost_gold
|
||||
|
||||
@@ -1,393 +1,36 @@
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor, run_postprocessor, IgnoredException
|
||||
from nonebot.adapters.onebot.v11.exception import ActionFailed
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from models.bag_user import BagUser
|
||||
from utils.manager import (
|
||||
plugins2settings_manager,
|
||||
admin_manager,
|
||||
group_manager,
|
||||
plugins_manager,
|
||||
plugins2cd_manager,
|
||||
plugins2block_manager,
|
||||
plugins2count_manager,
|
||||
)
|
||||
from ._utils import (
|
||||
set_block_limit_false,
|
||||
status_message_manager,
|
||||
ignore_rst_module,
|
||||
other_limit_plugins,
|
||||
)
|
||||
from nonebot.typing import T_State
|
||||
from typing import Optional
|
||||
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
MessageEvent,
|
||||
GroupMessageEvent,
|
||||
PokeNotifyEvent,
|
||||
PrivateMessageEvent,
|
||||
Message,
|
||||
Event,
|
||||
)
|
||||
from configs.config import Config
|
||||
from models.ban_user import BanUser
|
||||
from utils.utils import FreqLimiter
|
||||
from utils.message_builder import at
|
||||
from models.level_user import LevelUser
|
||||
import nonebot
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor, run_postprocessor
|
||||
from nonebot.typing import T_State
|
||||
|
||||
_flmt = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
_flmt_g = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
_flmt_s = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
_flmt_c = FreqLimiter(Config.get_config("hook", "CHECK_NOTICE_INFO_CD"))
|
||||
from ._utils import (
|
||||
set_block_limit_false,
|
||||
AuthChecker,
|
||||
)
|
||||
|
||||
|
||||
# 权限检测
|
||||
# # 权限检测
|
||||
@run_preprocessor
|
||||
async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State):
|
||||
module = matcher.plugin_name
|
||||
plugins2info_dict = plugins2settings_manager.get_data()
|
||||
# 功能的金币检测 #######################################
|
||||
# 功能的金币检测 #######################################
|
||||
# 功能的金币检测 #######################################
|
||||
cost_gold = 0
|
||||
if isinstance(
|
||||
event, GroupMessageEvent
|
||||
) and plugins2settings_manager.get_plugin_data(module).get("cost_gold"):
|
||||
cost_gold = plugins2settings_manager.get_plugin_data(module).get("cost_gold")
|
||||
if await BagUser.get_gold(event.user_id, event.group_id) < cost_gold:
|
||||
await send_msg(f"金币不足..该功能需要{cost_gold}金币..", bot, event)
|
||||
raise IgnoredException(f"{module} 金币限制...")
|
||||
# 当插件不阻塞超级用户时,超级用户提前扣除金币
|
||||
if (
|
||||
str(event.user_id) in bot.config.superusers
|
||||
and not plugins2info_dict[module]["limit_superuser"]
|
||||
):
|
||||
await BagUser.spend_gold(event.user_id, event.group_id, cost_gold)
|
||||
try:
|
||||
if (
|
||||
(not isinstance(event, MessageEvent) and module not in other_limit_plugins)
|
||||
or await BanUser.is_ban(event.user_id)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
) or (
|
||||
str(event.user_id) in bot.config.superusers
|
||||
and plugins2info_dict.get(module)
|
||||
and not plugins2info_dict[module]["limit_superuser"]
|
||||
):
|
||||
return
|
||||
except AttributeError:
|
||||
pass
|
||||
# 超级用户命令
|
||||
try:
|
||||
_plugin = nonebot.plugin.get_plugin(module)
|
||||
_module = _plugin.module
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
if (
|
||||
"[superuser]" in plugin_name.lower()
|
||||
and str(event.user_id) in bot.config.superusers
|
||||
):
|
||||
return
|
||||
except AttributeError:
|
||||
pass
|
||||
# 群黑名单检测 群总开关检测
|
||||
if (
|
||||
isinstance(event, GroupMessageEvent)
|
||||
or matcher.plugin_name in other_limit_plugins
|
||||
):
|
||||
try:
|
||||
if (
|
||||
group_manager.get_group_level(event.group_id) < 0
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
):
|
||||
raise IgnoredException("群黑名单")
|
||||
if not group_manager.check_group_bot_status(event.group_id):
|
||||
try:
|
||||
if str(event.get_message()) != "醒来":
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
except ValueError:
|
||||
raise IgnoredException("功能总开关关闭状态")
|
||||
except AttributeError:
|
||||
pass
|
||||
if module in admin_manager.keys() and matcher.priority not in [1, 999]:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
# 个人权限
|
||||
if (
|
||||
not await LevelUser.check_level(
|
||||
event.user_id,
|
||||
event.group_id,
|
||||
admin_manager.get_plugin_level(module),
|
||||
)
|
||||
and admin_manager.get_plugin_level(module) > 0
|
||||
):
|
||||
try:
|
||||
if _flmt.check(event.user_id):
|
||||
_flmt.start_cd(event.user_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id,
|
||||
message=f"{at(event.user_id)}你的权限不足喔,该功能需要的权限等级:"
|
||||
f"{admin_manager.get_plugin_level(module)}",
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
set_block_limit_false(event, module)
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
raise IgnoredException("权限不足")
|
||||
else:
|
||||
if not await LevelUser.check_level(
|
||||
event.user_id, 0, admin_manager.get_plugin_level(module)
|
||||
):
|
||||
try:
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id,
|
||||
message=f"你的权限不足喔,该功能需要的权限等级:{admin_manager.get_plugin_level(module)}",
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
set_block_limit_false(event, module)
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.user_id)
|
||||
raise IgnoredException("权限不足")
|
||||
if module in plugins2info_dict.keys() and matcher.priority not in [1, 999]:
|
||||
# 戳一戳单独判断
|
||||
if (
|
||||
isinstance(event, GroupMessageEvent)
|
||||
or isinstance(event, PokeNotifyEvent)
|
||||
or matcher.plugin_name in other_limit_plugins
|
||||
):
|
||||
if status_message_manager.get(event.group_id) is None:
|
||||
status_message_manager.delete(event.group_id)
|
||||
if plugins2info_dict[module]["level"] > group_manager.get_group_level(
|
||||
event.group_id
|
||||
):
|
||||
try:
|
||||
if _flmt_g.check(event.user_id) and module not in ignore_rst_module:
|
||||
_flmt_g.start_cd(event.user_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="群权限不足..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, module)
|
||||
raise IgnoredException("群权限不足")
|
||||
# 插件状态
|
||||
if not group_manager.get_plugin_status(module, event.group_id):
|
||||
try:
|
||||
if module not in ignore_rst_module and _flmt_s.check(
|
||||
event.group_id
|
||||
):
|
||||
_flmt_s.start_cd(event.group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="该群未开启此功能.."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, module)
|
||||
raise IgnoredException("未开启此功能...")
|
||||
# 管理员禁用
|
||||
if not group_manager.get_plugin_status(f"{module}:super", event.group_id):
|
||||
try:
|
||||
if (
|
||||
_flmt_s.check(event.group_id)
|
||||
and module not in ignore_rst_module
|
||||
):
|
||||
_flmt_s.start_cd(event.group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="管理员禁用了此群该功能..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, module)
|
||||
raise IgnoredException("管理员禁用了此群该功能...")
|
||||
# 群聊禁用
|
||||
if not plugins_manager.get_plugin_status(module, block_type="group"):
|
||||
try:
|
||||
if (
|
||||
_flmt_c.check(event.group_id)
|
||||
and module not in ignore_rst_module
|
||||
):
|
||||
_flmt_c.start_cd(event.group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="该功能在群聊中已被禁用..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.group_id)
|
||||
set_block_limit_false(event, module)
|
||||
raise IgnoredException("该插件在群聊中已被禁用...")
|
||||
else:
|
||||
# 私聊禁用
|
||||
if not plugins_manager.get_plugin_status(module, block_type="private"):
|
||||
try:
|
||||
if _flmt_c.check(event.user_id):
|
||||
_flmt_c.start_cd(event.user_id)
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id, message="该功能在私聊中已被禁用..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
status_message_manager.add(event.user_id)
|
||||
set_block_limit_false(event, module)
|
||||
raise IgnoredException("该插件在私聊中已被禁用...")
|
||||
# 维护
|
||||
if not plugins_manager.get_plugin_status(module, block_type="all"):
|
||||
if isinstance(
|
||||
event, GroupMessageEvent
|
||||
) and group_manager.check_group_is_white(event.group_id):
|
||||
return
|
||||
try:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
if (
|
||||
_flmt_c.check(event.group_id)
|
||||
and module not in ignore_rst_module
|
||||
):
|
||||
_flmt_c.start_cd(event.group_id)
|
||||
await bot.send_group_msg(
|
||||
group_id=event.group_id, message="此功能正在维护..."
|
||||
)
|
||||
else:
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id, message="此功能正在维护..."
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
if event.is_tome():
|
||||
id_ = (
|
||||
event.group_id
|
||||
if isinstance(event, GroupMessageEvent)
|
||||
else event.user_id
|
||||
)
|
||||
status_message_manager.add(id_)
|
||||
set_block_limit_false(event, module)
|
||||
raise IgnoredException("此功能正在维护...")
|
||||
|
||||
# 以下为限制检测 #######################################################
|
||||
# 以下为限制检测 #######################################################
|
||||
# 以下为限制检测 #######################################################
|
||||
# 以下为限制检测 #######################################################
|
||||
# 以下为限制检测 #######################################################
|
||||
# 以下为限制检测 #######################################################
|
||||
# Cd
|
||||
if plugins2cd_manager.check_plugin_cd_status(module):
|
||||
plugin_cd_data = plugins2cd_manager.get_plugin_cd_data(module)
|
||||
check_type = plugin_cd_data["check_type"]
|
||||
limit_type = plugin_cd_data["limit_type"]
|
||||
rst = plugin_cd_data["rst"]
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or plugins2cd_manager.get_plugin_data(module).get("check_type") == "all"
|
||||
):
|
||||
cd_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
cd_type_ = event.group_id
|
||||
if not plugins2cd_manager.check(module, cd_type_):
|
||||
if rst:
|
||||
rst = await init_rst(rst, event)
|
||||
await send_msg(rst, bot, event)
|
||||
raise IgnoredException(f"{module} 正在cd中...")
|
||||
else:
|
||||
plugins2cd_manager.start_cd(module, cd_type_)
|
||||
# Block
|
||||
if plugins2block_manager.check_plugin_block_status(module):
|
||||
plugin_block_data = plugins2block_manager.get_plugin_block_data(module)
|
||||
check_type = plugin_block_data["check_type"]
|
||||
limit_type = plugin_block_data["limit_type"]
|
||||
rst = plugin_block_data["rst"]
|
||||
if (
|
||||
(isinstance(event, PrivateMessageEvent) and check_type == "private")
|
||||
or (isinstance(event, GroupMessageEvent) and check_type == "group")
|
||||
or check_type == "all"
|
||||
):
|
||||
block_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
block_type_ = event.group_id
|
||||
if plugins2block_manager.check(block_type_, module):
|
||||
if rst:
|
||||
rst = await init_rst(rst, event)
|
||||
await send_msg(rst, bot, event)
|
||||
raise IgnoredException(f"{event.user_id}正在调用{module}....")
|
||||
else:
|
||||
plugins2block_manager.set_true(block_type_, module)
|
||||
# Count
|
||||
if (
|
||||
plugins2count_manager.check_plugin_count_status(module)
|
||||
and event.user_id not in bot.config.superusers
|
||||
):
|
||||
plugin_count_data = plugins2count_manager.get_plugin_count_data(module)
|
||||
limit_type = plugin_count_data["limit_type"]
|
||||
rst = plugin_count_data["rst"]
|
||||
count_type_ = event.user_id
|
||||
if limit_type == "group" and isinstance(event, GroupMessageEvent):
|
||||
count_type_ = event.group_id
|
||||
if not plugins2count_manager.check(module, count_type_):
|
||||
if rst:
|
||||
rst = await init_rst(rst, event)
|
||||
await send_msg(rst, bot, event)
|
||||
raise IgnoredException(f"{module} count次数限制...")
|
||||
else:
|
||||
plugins2count_manager.increase(module, count_type_)
|
||||
# 功能花费的金币 #######################################
|
||||
# 功能花费的金币 #######################################
|
||||
if cost_gold:
|
||||
await BagUser.spend_gold(event.user_id, event.group_id, cost_gold)
|
||||
|
||||
|
||||
async def send_msg(rst: str, bot: Bot, event: MessageEvent):
|
||||
"""
|
||||
发送信息
|
||||
:param rst: pass
|
||||
:param bot: pass
|
||||
:param event: pass
|
||||
"""
|
||||
rst = await init_rst(rst, event)
|
||||
try:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
status_message_manager.add(event.group_id)
|
||||
await bot.send_group_msg(group_id=event.group_id, message=Message(rst))
|
||||
else:
|
||||
status_message_manager.add(event.user_id)
|
||||
await bot.send_private_msg(user_id=event.user_id, message=Message(rst))
|
||||
except ActionFailed:
|
||||
pass
|
||||
async def _(matcher: Matcher, bot: Bot, event: Event):
|
||||
await AuthChecker().auth(matcher, bot, event)
|
||||
|
||||
|
||||
# 解除命令block阻塞
|
||||
@run_postprocessor
|
||||
async def _(
|
||||
matcher: Matcher,
|
||||
exception: Optional[Exception],
|
||||
bot: Bot,
|
||||
event: Event,
|
||||
state: T_State,
|
||||
matcher: Matcher,
|
||||
exception: Optional[Exception],
|
||||
bot: Bot,
|
||||
event: Event,
|
||||
state: T_State,
|
||||
):
|
||||
if not isinstance(event, MessageEvent) and matcher.plugin_name != "poke":
|
||||
return
|
||||
module = matcher.plugin_name
|
||||
set_block_limit_false(event, module)
|
||||
|
||||
|
||||
async def init_rst(rst: str, event: MessageEvent):
|
||||
if "[uname]" in rst:
|
||||
uname = event.sender.card or event.sender.nickname
|
||||
rst = rst.replace("[uname]", uname)
|
||||
if "[nickname]" in rst:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
nickname = await GroupInfoUser.get_group_member_nickname(
|
||||
event.user_id, event.group_id
|
||||
)
|
||||
else:
|
||||
nickname = await FriendUser.get_friend_nickname(event.user_id)
|
||||
rst = rst.replace("[nickname]", nickname)
|
||||
if "[at]" in rst and isinstance(event, GroupMessageEvent):
|
||||
rst = rst.replace("[at]", str(at(event.user_id)))
|
||||
return rst
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor, IgnoredException
|
||||
from nonebot.adapters.onebot.v11.exception import ActionFailed
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
ActionFailed,
|
||||
Bot,
|
||||
Event,
|
||||
GroupMessageEvent,
|
||||
MessageEvent,
|
||||
PokeNotifyEvent,
|
||||
GroupMessageEvent,
|
||||
)
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import IgnoredException, run_preprocessor
|
||||
from nonebot.typing import T_State
|
||||
|
||||
from configs.config import Config
|
||||
from models.ban_user import BanUser
|
||||
from utils.utils import is_number, static_flmt, FreqLimiter
|
||||
from utils.message_builder import at
|
||||
from ._utils import ignore_rst_module, other_limit_plugins
|
||||
from utils.utils import FreqLimiter, is_number, static_flmt
|
||||
|
||||
from ._utils import ignore_rst_module, other_limit_plugins
|
||||
|
||||
Config.add_plugin_config(
|
||||
"hook",
|
||||
@@ -29,10 +30,13 @@ _flmt = FreqLimiter(300)
|
||||
# 检查是否被ban
|
||||
@run_preprocessor
|
||||
async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State):
|
||||
if (
|
||||
(isinstance(event, MessageEvent) or isinstance(event, PokeNotifyEvent))
|
||||
and matcher.priority not in [1, 999]
|
||||
) or matcher.plugin_name in other_limit_plugins:
|
||||
if hasattr(event, "user_id") and (
|
||||
(
|
||||
(isinstance(event, MessageEvent) or isinstance(event, PokeNotifyEvent))
|
||||
and matcher.priority not in [1, 999]
|
||||
)
|
||||
or matcher.plugin_name in other_limit_plugins
|
||||
):
|
||||
try:
|
||||
if (
|
||||
await BanUser.is_super_ban(event.user_id)
|
||||
@@ -46,7 +50,7 @@ async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State):
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
):
|
||||
time = await BanUser.check_ban_time(event.user_id)
|
||||
if is_number(time):
|
||||
if isinstance(time, int):
|
||||
time = abs(int(time))
|
||||
if time < 60:
|
||||
time = str(time) + " 秒"
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor, IgnoredException
|
||||
from nonebot.adapters.onebot.v11.exception import ActionFailed
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
ActionFailed,
|
||||
MessageEvent,
|
||||
GroupMessageEvent,
|
||||
)
|
||||
@@ -28,12 +28,12 @@ async def _(matcher: Matcher, bot: Bot, event: GroupMessageEvent, state: T_State
|
||||
if matcher.type == "message" and matcher.priority not in [1, 999]:
|
||||
if state["_prefix"]["raw_command"]:
|
||||
if _blmt.check(f'{event.user_id}{state["_prefix"]["raw_command"]}'):
|
||||
if await BanUser.ban(
|
||||
await BanUser.ban(
|
||||
event.user_id,
|
||||
9,
|
||||
Config.get_config("hook", "MALICIOUS_BAN_TIME") * 60,
|
||||
):
|
||||
logger.info(f"USER {event.user_id} 触发了恶意触发检测")
|
||||
)
|
||||
logger.info(f"USER {event.user_id} 触发了恶意触发检测")
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
try:
|
||||
await bot.send_group_msg(
|
||||
|
||||
@@ -2,15 +2,12 @@ from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor, IgnoredException
|
||||
from nonebot.typing import T_State
|
||||
from ._utils import status_message_manager
|
||||
from utils.image_utils import text2image
|
||||
from typing import Dict, Any
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
MessageEvent,
|
||||
PrivateMessageEvent,
|
||||
GroupMessageEvent,
|
||||
)
|
||||
import re
|
||||
|
||||
|
||||
# 为什么AI会自己和自己聊天
|
||||
@@ -41,11 +38,6 @@ async def _(matcher: Matcher, bot: Bot, event: MessageEvent, state: T_State):
|
||||
status_message_manager.delete(event.user_id)
|
||||
raise IgnoredException("有命令就别说话了")
|
||||
|
||||
# @Bot.on_calling_api
|
||||
# async def handle_api_call(bot: Bot, api: str, data: Dict[str, Any]):
|
||||
# if api in ["send_msg", "send_group_msg", "send_private_msg"]:
|
||||
# msg = str(data["message"])
|
||||
# if (r := re.search("\[\[To_Img\|?(.*?)]]", msg)) or (r := re.search("[[To_Img\|?(.*?)[[")):
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,47 +1,56 @@
|
||||
from nonebot.exception import MockApiException
|
||||
from nonebot.adapters.onebot.v11 import Bot, Message
|
||||
from utils.manager import group_manager
|
||||
from services.log import logger
|
||||
from typing import Dict, Any
|
||||
import re
|
||||
|
||||
|
||||
@Bot.on_calling_api
|
||||
async def handle_api_call(bot: Bot, api: str, data: Dict[str, Any]):
|
||||
async def _(bot: Bot, api: str, data: Dict[str, Any]):
|
||||
r = None
|
||||
if (
|
||||
(
|
||||
(api == "send_msg" and data.get("message_type") == "group")
|
||||
or api == "send_group_msg"
|
||||
)
|
||||
and (
|
||||
task = None
|
||||
group_id = None
|
||||
try:
|
||||
if (
|
||||
(
|
||||
r := re.search(
|
||||
"^\[\[_task\|(.*)]]",
|
||||
data["message"].strip()
|
||||
if isinstance(data["message"], str)
|
||||
else str(data["message"]["text"]).strip(),
|
||||
(api == "send_msg" and data.get("message_type") == "group")
|
||||
or api == "send_group_msg"
|
||||
)
|
||||
and (
|
||||
(
|
||||
r := re.search(
|
||||
"^\[\[_task\|(.*)]]",
|
||||
data["message"].strip()
|
||||
if isinstance(data["message"], str)
|
||||
else str(data["message"]["text"]).strip(),
|
||||
)
|
||||
)
|
||||
or (
|
||||
r := re.search(
|
||||
"^[[_task\|(.*)]]",
|
||||
data["message"].strip()
|
||||
if isinstance(data["message"], str)
|
||||
else str(data["message"]["text"]).strip(),
|
||||
)
|
||||
)
|
||||
)
|
||||
or (
|
||||
r := re.search(
|
||||
"^[[_task\|(.*)]]",
|
||||
data["message"].strip()
|
||||
if isinstance(data["message"], str)
|
||||
else str(data["message"]["text"]).strip(),
|
||||
and r.group(1) in group_manager.get_task_data().keys()
|
||||
):
|
||||
task = r.group(1)
|
||||
group_id = data["group_id"]
|
||||
except Exception as e:
|
||||
logger.error(f"TaskHook ERROR {type(e)}:{e}")
|
||||
else:
|
||||
if task and group_id:
|
||||
if (
|
||||
group_manager.get_group_level(group_id) < 0
|
||||
or not group_manager.check_task_status(task, group_id)
|
||||
):
|
||||
raise MockApiException(f"被动技能 {task} 处于关闭状态...")
|
||||
else:
|
||||
msg = str(data["message"]).strip()
|
||||
msg = msg.replace(f"[[_task|{task}]]", "").replace(
|
||||
f"[[_task|{task}]]", ""
|
||||
)
|
||||
)
|
||||
)
|
||||
and r.group(1) in group_manager.get_task_data().keys()
|
||||
):
|
||||
task = r.group(1)
|
||||
group_id = data["group_id"]
|
||||
if group_manager.get_group_level(
|
||||
group_id
|
||||
) < 0 or not await group_manager.check_group_task_status(group_id, task):
|
||||
raise MockApiException(f"被动技能 {task} 处于关闭状态...")
|
||||
else:
|
||||
msg = str(data["message"]).strip()
|
||||
msg = msg.replace(f"[[_task|{task}]]", "").replace(
|
||||
f"[[_task|{task}]]", ""
|
||||
)
|
||||
data["message"] = Message(msg)
|
||||
data["message"] = Message(msg)
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
from .init_group_manager import init_group_manager, group_manager
|
||||
from .init_plugins_config import init_plugins_config
|
||||
from .init_plugins_data import init_plugins_data, plugins_manager
|
||||
from .init_none_plugin_count_manager import init_none_plugin_count_manager
|
||||
from .init_plugins_resources import init_plugins_resources
|
||||
from .init_plugins_settings import init_plugins_settings
|
||||
from .init_plugins_limit import (
|
||||
init_plugins_block_limit,
|
||||
init_plugins_count_limit,
|
||||
init_plugins_cd_limit,
|
||||
)
|
||||
from .init import init
|
||||
from .check_plugin_status import check_plugin_status
|
||||
import nonebot
|
||||
from nonebot import Driver
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
|
||||
from configs.path_config import DATA_PATH
|
||||
from services.log import logger
|
||||
from pathlib import Path
|
||||
from nonebot import Driver
|
||||
import nonebot
|
||||
|
||||
from .check_plugin_status import check_plugin_status
|
||||
from .init import init
|
||||
from .init_none_plugin_count_manager import init_none_plugin_count_manager
|
||||
from .init_plugin_info import init_plugin_info
|
||||
from .init_plugins_config import init_plugins_config
|
||||
from .init_plugins_data import init_plugins_data, plugins_manager
|
||||
from .init_plugins_limit import (
|
||||
init_plugins_block_limit,
|
||||
init_plugins_cd_limit,
|
||||
init_plugins_count_limit,
|
||||
)
|
||||
from .init_plugins_resources import init_plugins_resources
|
||||
from .init_plugins_settings import init_plugins_settings
|
||||
|
||||
__zx_plugin_name__ = "初始化插件数据 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
@@ -28,27 +28,22 @@ driver: Driver = nonebot.get_driver()
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
def _():
|
||||
async def _():
|
||||
"""
|
||||
初始化数据
|
||||
"""
|
||||
_flag = False
|
||||
config_file = DATA_PATH / "configs" / "plugins2config.yaml"
|
||||
if not config_file.exists():
|
||||
_flag = True
|
||||
_flag = not config_file.exists()
|
||||
init()
|
||||
init_plugins_settings(DATA_PATH)
|
||||
init_plugins_cd_limit(DATA_PATH)
|
||||
init_plugins_block_limit(DATA_PATH)
|
||||
init_plugins_count_limit(DATA_PATH)
|
||||
init_plugins_data(DATA_PATH)
|
||||
init_plugins_config(DATA_PATH)
|
||||
init_plugin_info()
|
||||
init_plugins_settings()
|
||||
init_plugins_cd_limit()
|
||||
init_plugins_block_limit()
|
||||
init_plugins_count_limit()
|
||||
init_plugins_data()
|
||||
init_plugins_config()
|
||||
init_plugins_resources()
|
||||
init_none_plugin_count_manager()
|
||||
x = group_manager.get_super_old_data()
|
||||
if x:
|
||||
for key in x.keys():
|
||||
plugins_manager.block_plugin(key, block_type=x[key])
|
||||
if _flag:
|
||||
raise Exception("首次运行,已在configs目录下生成配置文件config.yaml,修改后重启即可...")
|
||||
logger.info("初始化数据完成...")
|
||||
@@ -56,5 +51,5 @@ def _():
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def _(bot: Bot):
|
||||
await init_group_manager()
|
||||
# await init_group_manager()
|
||||
await check_plugin_status(bot)
|
||||
|
||||
@@ -6,13 +6,13 @@ async def check_plugin_status(bot: Bot):
|
||||
"""
|
||||
遍历查看插件加载情况
|
||||
"""
|
||||
rst = ""
|
||||
msg = ""
|
||||
for plugin in plugins_manager.keys():
|
||||
data = plugins_manager.get(plugin)
|
||||
if data.get("error") or data.get("error") is None:
|
||||
rst += f'{plugin}:{data["plugin_name"]}\n'
|
||||
if rst:
|
||||
rst = "以下插件加载失败..\n" + rst
|
||||
if data.error:
|
||||
msg += f'{plugin}:{data.plugin_name}\n'
|
||||
if msg and bot.config.superusers:
|
||||
msg = "以下插件加载失败..\n" + msg
|
||||
await bot.send_private_msg(
|
||||
user_id=int(list(bot.config.superusers)[0]), message=rst[:-1]
|
||||
user_id=int(list(bot.config.superusers)[0]), message=msg.strip()
|
||||
)
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
from pathlib import Path
|
||||
from utils.manager import group_manager
|
||||
from services.db_context import db
|
||||
from asyncpg.exceptions import DuplicateColumnError
|
||||
from services.log import logger
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
try:
|
||||
from models.group_remind import GroupRemind
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
async def init_group_manager():
|
||||
"""
|
||||
旧数据格式替换为新格式
|
||||
初始化数据
|
||||
"""
|
||||
old_group_level_file = Path() / "data" / "manager" / "group_level.json"
|
||||
old_plugin_list_file = Path() / "data" / "manager" / "plugin_list.json"
|
||||
if old_group_level_file.exists():
|
||||
data = json.load(open(old_group_level_file, "r", encoding="utf8"))
|
||||
for key in data.keys():
|
||||
group = key
|
||||
level = data[key]
|
||||
group_manager.set_group_level(group, level)
|
||||
old_group_level_file.unlink()
|
||||
group_manager.save()
|
||||
|
||||
if old_plugin_list_file.exists():
|
||||
data = json.load(open(old_plugin_list_file, "r", encoding="utf8"))
|
||||
for plugin in data.keys():
|
||||
for group in data[plugin].keys():
|
||||
if group == "default" and not data[plugin]["default"]:
|
||||
group_manager.block_plugin(plugin)
|
||||
elif not data[plugin][group]:
|
||||
group_manager.block_plugin(plugin, group)
|
||||
old_plugin_list_file.unlink()
|
||||
old_data_table = Path() / "models" / "group_remind.py"
|
||||
try:
|
||||
if old_data_table.exists():
|
||||
b = {
|
||||
"hy": "group_welcome",
|
||||
"kxcz": "open_case_reset_remind",
|
||||
"zwa": "zwa",
|
||||
"blpar": "bilibili_parse",
|
||||
"epic": "epic_free_game",
|
||||
"pa": "pa",
|
||||
"almanac": "genshin_alc",
|
||||
}
|
||||
for group in group_manager.get_data()["group_manager"]:
|
||||
for remind in b:
|
||||
try:
|
||||
status = await GroupRemind.get_status(int(group), remind)
|
||||
if status is not None:
|
||||
if status:
|
||||
await group_manager.open_group_task(group, b[remind])
|
||||
logger.info(f"读取旧数据-->{group} 开启 {b[remind]}")
|
||||
else:
|
||||
await group_manager.close_group_task(group, b[remind])
|
||||
logger.info(f"读取旧数据-->{group} 关闭 {b[remind]}")
|
||||
except Exception as e:
|
||||
pass
|
||||
query = db.text("DROP TABLE group_reminds;")
|
||||
await db.first(query)
|
||||
old_data_table.unlink()
|
||||
logger.info("旧数据读取完毕,删除了舍弃表 group_reminds...")
|
||||
except (ModuleNotFoundError, DuplicateColumnError):
|
||||
pass
|
||||
group_manager.save()
|
||||
@@ -5,7 +5,6 @@ from utils.manager import (
|
||||
plugins2settings_manager,
|
||||
plugins2block_manager,
|
||||
plugins_manager,
|
||||
resources_manager
|
||||
)
|
||||
from services.log import logger
|
||||
from utils.utils import get_matchers
|
||||
@@ -20,36 +19,44 @@ def init_none_plugin_count_manager():
|
||||
"""
|
||||
清除已删除插件数据
|
||||
"""
|
||||
modules = [x.plugin_name for x in get_matchers()]
|
||||
modules = [x.plugin_name for x in get_matchers(True)]
|
||||
plugins_manager_list = list(plugins_manager.keys())
|
||||
for module in plugins_manager_list:
|
||||
if module not in modules or none_plugin_count_manager.check(module):
|
||||
try:
|
||||
plugin_name = plugins_manager.get(module)["plugin_name"]
|
||||
except (AttributeError, KeyError):
|
||||
plugin_name = ""
|
||||
if none_plugin_count_manager.check(module):
|
||||
try:
|
||||
if module not in modules or none_plugin_count_manager.check(module):
|
||||
try:
|
||||
plugins2settings_manager.delete(module)
|
||||
plugins2settings_manager.save()
|
||||
plugins2count_manager.delete(module)
|
||||
plugins2count_manager.save()
|
||||
plugins2cd_manager.delete(module)
|
||||
plugins2cd_manager.save()
|
||||
plugins2block_manager.delete(module)
|
||||
plugins2block_manager.save()
|
||||
plugins_manager.delete(module)
|
||||
plugins_manager.save()
|
||||
# resources_manager.remove_resource(module)
|
||||
none_plugin_count_manager.delete(module)
|
||||
logger.info(f"{module}:{plugin_name} 插件疑似已删除,清除对应插件数据...")
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"{module}:{plugin_name} 插件疑似已删除,清除对应插件数据失败...{type(e)}:{e}")
|
||||
plugin_name = plugins_manager.get(module).plugin_name
|
||||
except (AttributeError, KeyError):
|
||||
plugin_name = ""
|
||||
if none_plugin_count_manager.check(module):
|
||||
try:
|
||||
plugins2settings_manager.delete(module)
|
||||
plugins2count_manager.delete(module)
|
||||
plugins2cd_manager.delete(module)
|
||||
plugins2block_manager.delete(module)
|
||||
plugins_manager.delete(module)
|
||||
plugins_manager.save()
|
||||
# resources_manager.remove_resource(module)
|
||||
none_plugin_count_manager.delete(module)
|
||||
logger.info(f"{module}:{plugin_name} 插件疑似已删除,清除对应插件数据...")
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"{module}:{plugin_name} 插件疑似已删除,清除对应插件数据失败...{type(e)}:{e}"
|
||||
)
|
||||
else:
|
||||
none_plugin_count_manager.add_count(module)
|
||||
logger.info(
|
||||
f"{module}:{plugin_name} 插件疑似已删除,"
|
||||
f"加载{none_plugin_count_manager._max_count}次失败后将清除对应插件数据,"
|
||||
f"当前次数:{none_plugin_count_manager.get(module)}"
|
||||
)
|
||||
else:
|
||||
none_plugin_count_manager.add_count(module)
|
||||
logger.info(
|
||||
f"{module}:{plugin_name} 插件疑似已删除,加载{none_plugin_count_manager._max_count}次失败后将清除对应插件数据,当前次数:{none_plugin_count_manager._data[module]}")
|
||||
else:
|
||||
none_plugin_count_manager.reset(module)
|
||||
none_plugin_count_manager.reset(module)
|
||||
except Exception as e:
|
||||
logger.error(f"清除插件数据错误 {type(e)}:{e}")
|
||||
plugins2settings_manager.save()
|
||||
plugins2count_manager.save()
|
||||
plugins2cd_manager.save()
|
||||
plugins2block_manager.save()
|
||||
plugins_manager.save()
|
||||
none_plugin_count_manager.save()
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
import random
|
||||
from types import ModuleType
|
||||
from typing import Any, Dict
|
||||
|
||||
from configs.config import Config
|
||||
from services import logger
|
||||
from utils.manager import (
|
||||
plugin_data_manager,
|
||||
plugins2block_manager,
|
||||
plugins2cd_manager,
|
||||
plugins2count_manager,
|
||||
plugins2settings_manager,
|
||||
plugins_manager,
|
||||
)
|
||||
from utils.manager.models import (
|
||||
Plugin,
|
||||
PluginBlock,
|
||||
PluginCd,
|
||||
PluginCount,
|
||||
PluginData,
|
||||
PluginSetting,
|
||||
PluginType,
|
||||
)
|
||||
from utils.utils import get_matchers
|
||||
|
||||
|
||||
def get_attr(module: ModuleType, name: str, default: Any = None) -> Any:
|
||||
"""
|
||||
说明:
|
||||
获取属性
|
||||
参数:
|
||||
:param module: module
|
||||
:param name: name
|
||||
:param default: default
|
||||
"""
|
||||
return getattr(module, name, None) or default
|
||||
|
||||
|
||||
def init_plugin_info():
|
||||
|
||||
for matcher in [x for x in get_matchers(True)]:
|
||||
try:
|
||||
if (plugin := matcher.plugin) and matcher.plugin_name:
|
||||
metadata = plugin.metadata
|
||||
extra = metadata.extra if metadata else {}
|
||||
if hasattr(plugin, "module"):
|
||||
module = plugin.module
|
||||
plugin_model = matcher.plugin_name
|
||||
plugin_name = (
|
||||
metadata.name
|
||||
if metadata and metadata.name
|
||||
else get_attr(module, "__zx_plugin_name__", matcher.plugin_name)
|
||||
)
|
||||
if not plugin_name:
|
||||
logger.warning(f"配置文件 模块:{plugin_model} 获取 plugin_name 失败...")
|
||||
continue
|
||||
if "[Admin]" in plugin_name:
|
||||
plugin_type = PluginType.ADMIN
|
||||
plugin_name = plugin_name.replace("[Admin]", "").strip()
|
||||
elif "[Hidden]" in plugin_name:
|
||||
plugin_type = PluginType.HIDDEN
|
||||
plugin_name = plugin_name.replace("[Hidden]", "").strip()
|
||||
elif "[Superuser]" in plugin_name:
|
||||
plugin_type = PluginType.SUPERUSER
|
||||
plugin_name = plugin_name.replace("[Superuser]", "").strip()
|
||||
else:
|
||||
plugin_type = PluginType.NORMAL
|
||||
plugin_usage = (
|
||||
metadata.usage
|
||||
if metadata and metadata.usage
|
||||
else get_attr(module, "__plugin_usage__")
|
||||
)
|
||||
plugin_des = (
|
||||
metadata.description
|
||||
if metadata and metadata.description
|
||||
else get_attr(module, "__plugin_des__")
|
||||
)
|
||||
menu_type = get_attr(module, "__plugin_type__") or ("normal",)
|
||||
plugin_setting = get_attr(module, "__plugin_settings__")
|
||||
if plugin_setting:
|
||||
plugin_setting = PluginSetting(**plugin_setting)
|
||||
plugin_setting.plugin_type = menu_type
|
||||
plugin_superuser_usage = get_attr(module, "__plugin_super_usage__")
|
||||
plugin_task = get_attr(module, "__plugin_task__")
|
||||
plugin_version = extra.get("__plugin_version__") or get_attr(
|
||||
module, "__plugin_version__"
|
||||
)
|
||||
plugin_author = extra.get("__plugin_author__") or get_attr(
|
||||
module, "__plugin_author__"
|
||||
)
|
||||
plugin_cd = get_attr(module, "__plugin_cd_limit__")
|
||||
if plugin_cd:
|
||||
plugin_cd = PluginCd(**plugin_cd)
|
||||
plugin_block = get_attr(module, "__plugin_block_limit__")
|
||||
if plugin_block:
|
||||
plugin_block = PluginBlock(**plugin_block)
|
||||
plugin_count = get_attr(module, "__plugin_count_limit__")
|
||||
if plugin_count:
|
||||
plugin_count = PluginCount(**plugin_count)
|
||||
plugin_resources = get_attr(module, "__plugin_resources__")
|
||||
plugin_configs = get_attr(module, "__plugin_configs__")
|
||||
if settings := plugins2settings_manager.get(plugin_model):
|
||||
plugin_setting = settings
|
||||
if plugin_cd_limit := plugins2cd_manager.get(plugin_model):
|
||||
plugin_cd = plugin_cd_limit
|
||||
if plugin_block_limit := plugins2block_manager.get(plugin_model):
|
||||
plugin_block = plugin_block_limit
|
||||
if plugin_count_limit := plugins2count_manager.get(plugin_model):
|
||||
plugin_count = plugin_count_limit
|
||||
if plugin_cfg := Config.get(plugin_model):
|
||||
if plugin_configs:
|
||||
for config_name in plugin_configs:
|
||||
config: Dict[str, Any] = plugin_configs[config_name] # type: ignore
|
||||
Config.add_plugin_config(
|
||||
plugin_model,
|
||||
config_name,
|
||||
config.get("value"),
|
||||
help_=config.get("help"),
|
||||
default_value=config.get("default_value"),
|
||||
type=config.get("type"),
|
||||
)
|
||||
plugin_configs = plugin_cfg.configs
|
||||
plugin_status = plugins_manager.get(plugin_model)
|
||||
if not plugin_status:
|
||||
plugin_status = Plugin(plugin_name=plugin_model)
|
||||
plugin_status.author = plugin_author
|
||||
plugin_status.version = plugin_version
|
||||
plugin_data = PluginData(
|
||||
model=plugin_model,
|
||||
name=plugin_name.strip(),
|
||||
plugin_type=plugin_type,
|
||||
usage=plugin_usage,
|
||||
superuser_usage=plugin_superuser_usage,
|
||||
des=plugin_des,
|
||||
task=plugin_task,
|
||||
menu_type=menu_type,
|
||||
plugin_setting=plugin_setting,
|
||||
plugin_cd=plugin_cd,
|
||||
plugin_block=plugin_block,
|
||||
plugin_count=plugin_count,
|
||||
plugin_resources=plugin_resources,
|
||||
plugin_configs=plugin_configs, # type: ignore
|
||||
plugin_status=plugin_status,
|
||||
)
|
||||
plugin_data_manager.add_plugin_info(plugin_data)
|
||||
except Exception as e:
|
||||
logger.error(f"构造插件数据失败 {matcher.plugin_name}", e=e)
|
||||
@@ -1,168 +1,173 @@
|
||||
from pathlib import Path
|
||||
from ruamel.yaml import round_trip_load, round_trip_dump, YAML
|
||||
from utils.manager import admin_manager, plugins_manager
|
||||
|
||||
from ruamel import yaml
|
||||
from ruamel.yaml import YAML, round_trip_dump, round_trip_load
|
||||
|
||||
from configs.config import Config
|
||||
from configs.path_config import DATA_PATH
|
||||
from services.log import logger
|
||||
from utils.manager import admin_manager, plugin_data_manager, plugins_manager
|
||||
from utils.text_utils import prompt2cn
|
||||
from utils.utils import get_matchers
|
||||
from ruamel import yaml
|
||||
import nonebot
|
||||
|
||||
|
||||
_yaml = YAML(typ="safe")
|
||||
|
||||
|
||||
def init_plugins_config(data_path):
|
||||
def init_plugins_config():
|
||||
"""
|
||||
初始化插件数据配置
|
||||
"""
|
||||
plugins2config_file = data_path / "configs" / "plugins2config.yaml"
|
||||
plugins2config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
_data = {}
|
||||
if plugins2config_file.exists():
|
||||
_data = _yaml.load(open(plugins2config_file, "r", encoding="utf8"))
|
||||
_matchers = get_matchers()
|
||||
for matcher in _matchers:
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
try:
|
||||
_module = _plugin.module
|
||||
except AttributeError:
|
||||
continue
|
||||
try:
|
||||
plugin_version = _module.__getattribute__("__plugin_version__")
|
||||
except AttributeError:
|
||||
plugin_version = None
|
||||
try:
|
||||
plugin_configs = _module.__getattribute__("__plugin_configs__")
|
||||
except AttributeError:
|
||||
continue
|
||||
# 插件配置版本更新或为Version为None或不在存储配置内
|
||||
if (
|
||||
plugin_version is None
|
||||
or (
|
||||
_data.get(matcher.plugin_name)
|
||||
and _data[matcher.plugin_name].keys() != plugin_configs.keys()
|
||||
)
|
||||
or plugin_version > plugins_manager.get(matcher.plugin_name)["version"]
|
||||
or matcher.plugin_name not in _data.keys()
|
||||
):
|
||||
for key in plugin_configs:
|
||||
if isinstance(plugin_configs[key], dict):
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
plugin_configs[key].get("value"),
|
||||
help_=plugin_configs[key].get("help"),
|
||||
default_value=plugin_configs[key].get("default_value"),
|
||||
_override=True,
|
||||
plugins2config_file = DATA_PATH / "configs" / "plugins2config.yaml"
|
||||
_data = Config.get_data()
|
||||
# 优先使用 metadata 数据
|
||||
for matcher in get_matchers(True):
|
||||
if matcher.plugin_name:
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
# 插件配置版本更新或为Version为None或不在存储配置内,当使用metadata时,必定更新
|
||||
version = plugin_data.plugin_status.version
|
||||
config = _data.get(matcher.plugin_name)
|
||||
plugin = plugins_manager.get(matcher.plugin_name)
|
||||
if plugin_data.plugin_configs and (
|
||||
isinstance(version, str)
|
||||
or (
|
||||
version is None
|
||||
or (
|
||||
config
|
||||
and config.configs.keys()
|
||||
!= plugin_data.plugin_configs.keys()
|
||||
)
|
||||
or version > int(plugin.version or 0)
|
||||
or matcher.plugin_name not in _data.keys()
|
||||
)
|
||||
else:
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name, key, plugin_configs[key]
|
||||
)
|
||||
else:
|
||||
plugin_configs = _data[matcher.plugin_name]
|
||||
for key in plugin_configs:
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
plugin_configs[key]["value"],
|
||||
help_=plugin_configs[key]["help"],
|
||||
default_value=plugin_configs[key]["default_value"],
|
||||
_override=True,
|
||||
)
|
||||
):
|
||||
plugin_configs = plugin_data.plugin_configs
|
||||
for key in plugin_configs:
|
||||
if isinstance(plugin_data.plugin_configs[key], dict):
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
plugin_configs[key].get("value"),
|
||||
help_=plugin_configs[key].get("help"),
|
||||
default_value=plugin_configs[key].get("default_value"),
|
||||
_override=True,
|
||||
type=plugin_configs[key].get("type"),
|
||||
)
|
||||
else:
|
||||
config = plugin_configs[key]
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
config.value,
|
||||
name=config.name,
|
||||
help_=config.help,
|
||||
default_value=config.default_value,
|
||||
_override=True,
|
||||
type=config.type,
|
||||
)
|
||||
elif plugin_configs := _data.get(matcher.plugin_name):
|
||||
for key in plugin_configs.configs:
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
plugin_configs.configs[key].value,
|
||||
help_=plugin_configs.configs[key].help,
|
||||
default_value=plugin_configs.configs[key].default_value,
|
||||
_override=True,
|
||||
type=plugin_configs.configs[key].type,
|
||||
)
|
||||
if not Config.is_empty():
|
||||
Config.save()
|
||||
_data = round_trip_load(open(plugins2config_file, encoding="utf8"))
|
||||
for plugin in _data.keys():
|
||||
try:
|
||||
plugin_name = plugins_manager.get(plugin)["plugin_name"]
|
||||
plugin_name = plugins_manager.get(plugin).plugin_name
|
||||
except (AttributeError, TypeError):
|
||||
plugin_name = plugin
|
||||
_data[plugin].yaml_set_start_comment(plugin_name, indent=2)
|
||||
# 初始化未设置的管理员权限等级
|
||||
for k, v in Config.get_admin_level_data():
|
||||
try:
|
||||
admin_manager.set_admin_level(k, v)
|
||||
except KeyError as e:
|
||||
raise KeyError(f"{e} ****** 请检查是否有插件加载失败 ******")
|
||||
admin_manager.set_admin_level(k, v)
|
||||
# 存完插件基本设置
|
||||
with open(plugins2config_file, "w", encoding="utf8") as wf:
|
||||
round_trip_dump(
|
||||
_data, wf, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
)
|
||||
# 再开始读取用户配置
|
||||
user_config_file = Path() / "configs" / "config.yaml"
|
||||
_data = {}
|
||||
_tmp_data = {}
|
||||
if user_config_file.exists():
|
||||
with open(user_config_file, "r", encoding="utf8") as f:
|
||||
_data = _yaml.load(f)
|
||||
# 数据替换
|
||||
for plugin in Config.keys():
|
||||
_tmp_data[plugin] = {}
|
||||
for k in Config[plugin].keys():
|
||||
try:
|
||||
if _data.get(plugin) and k in _data[plugin].keys():
|
||||
Config.set_config(plugin, k, _data[plugin][k])
|
||||
if level2module := Config.get_level2module(plugin, k):
|
||||
try:
|
||||
admin_manager.set_admin_level(
|
||||
level2module, _data[plugin][k]
|
||||
)
|
||||
except KeyError:
|
||||
logger.warning(
|
||||
f"{level2module} 设置权限等级失败:{_data[plugin][k]}"
|
||||
)
|
||||
_tmp_data[plugin][k] = Config.get_config(plugin, k)
|
||||
except AttributeError as e:
|
||||
raise AttributeError(
|
||||
f"{e}\n" + prompt2cn("可能为config.yaml配置文件填写不规范", 46)
|
||||
)
|
||||
Config.save()
|
||||
temp_file = Path() / "configs" / "temp_config.yaml"
|
||||
try:
|
||||
with open(temp_file, "w", encoding="utf8") as wf:
|
||||
yaml.dump(
|
||||
_tmp_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
)
|
||||
with open(temp_file, "r", encoding="utf8") as rf:
|
||||
_data = round_trip_load(rf)
|
||||
# 添加注释
|
||||
for plugin in _data.keys():
|
||||
rst = ""
|
||||
plugin_name = None
|
||||
try:
|
||||
plugin_data = Config.get(plugin)
|
||||
for x in list(Config.get(plugin).keys()):
|
||||
_replace_config()
|
||||
|
||||
|
||||
def _replace_config():
|
||||
"""
|
||||
说明:
|
||||
定时任务加载的配置读取替换
|
||||
"""
|
||||
# 再开始读取用户配置
|
||||
user_config_file = Path() / "configs" / "config.yaml"
|
||||
_data = {}
|
||||
_tmp_data = {}
|
||||
if user_config_file.exists():
|
||||
with open(user_config_file, "r", encoding="utf8") as f:
|
||||
_data = _yaml.load(f)
|
||||
# 数据替换
|
||||
for plugin in Config.keys():
|
||||
_tmp_data[plugin] = {}
|
||||
for k in Config[plugin].configs.keys():
|
||||
try:
|
||||
if _data.get(plugin) and k in _data[plugin].keys():
|
||||
Config.set_config(plugin, k, _data[plugin][k])
|
||||
if level2module := Config.get_level2module(plugin, k):
|
||||
try:
|
||||
_x = plugin_data[x].get("name")
|
||||
if _x:
|
||||
plugin_name = _x
|
||||
admin_manager.set_admin_level(
|
||||
level2module, _data[plugin][k]
|
||||
)
|
||||
except KeyError:
|
||||
logger.warning(
|
||||
f"{level2module} 设置权限等级失败:{_data[plugin][k]}"
|
||||
)
|
||||
_tmp_data[plugin][k] = Config.get_config(plugin, k)
|
||||
except AttributeError as e:
|
||||
raise AttributeError(
|
||||
f"{e}\n" + prompt2cn("可能为config.yaml配置文件填写不规范", 46)
|
||||
)
|
||||
Config.save()
|
||||
temp_file = Path() / "configs" / "temp_config.yaml"
|
||||
try:
|
||||
with open(temp_file, "w", encoding="utf8") as wf:
|
||||
yaml.dump(_tmp_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
with open(temp_file, "r", encoding="utf8") as rf:
|
||||
_data = round_trip_load(rf)
|
||||
# 添加注释
|
||||
for plugin in _data.keys():
|
||||
rst = ""
|
||||
plugin_name = None
|
||||
try:
|
||||
if config_group := Config.get(plugin):
|
||||
for key in list(config_group.configs.keys()):
|
||||
try:
|
||||
if config := config_group.configs[key]:
|
||||
if config.name:
|
||||
plugin_name = config.name
|
||||
except AttributeError:
|
||||
pass
|
||||
except (KeyError, AttributeError):
|
||||
plugin_name = None
|
||||
if not plugin_name:
|
||||
try:
|
||||
plugin_name = plugins_manager.get(plugin)["plugin_name"]
|
||||
except (AttributeError, TypeError):
|
||||
plugin_name = plugin
|
||||
plugin_name = (
|
||||
plugin_name.replace("[Hidden]", "")
|
||||
.replace("[Superuser]", "")
|
||||
.replace("[Admin]", "")
|
||||
.strip()
|
||||
)
|
||||
rst += plugin_name + "\n"
|
||||
for k in _data[plugin].keys():
|
||||
rst += f'{k}: {Config[plugin][k]["help"]}' + "\n"
|
||||
_data[plugin].yaml_set_start_comment(rst[:-1], indent=2)
|
||||
with open(Path() / "configs" / "config.yaml", "w", encoding="utf8") as wf:
|
||||
round_trip_dump(
|
||||
_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"生成简易配置注释错误 {type(e)}:{e}")
|
||||
if temp_file.exists():
|
||||
temp_file.unlink()
|
||||
except (KeyError, AttributeError):
|
||||
plugin_name = None
|
||||
if not plugin_name:
|
||||
try:
|
||||
plugin_name = plugins_manager.get(plugin).plugin_name
|
||||
except (AttributeError, TypeError):
|
||||
plugin_name = plugin
|
||||
plugin_name = (
|
||||
plugin_name.replace("[Hidden]", "")
|
||||
.replace("[Superuser]", "")
|
||||
.replace("[Admin]", "")
|
||||
.strip()
|
||||
)
|
||||
rst += plugin_name + "\n"
|
||||
for k in _data[plugin].keys():
|
||||
rst += f"{k}: {Config[plugin].configs[k].help}" + "\n"
|
||||
_data[plugin].yaml_set_start_comment(rst[:-1], indent=2)
|
||||
with open(Path() / "configs" / "config.yaml", "w", encoding="utf8") as wf:
|
||||
round_trip_dump(_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
except Exception as e:
|
||||
logger.error(f"生成简易配置注释错误 {type(e)}:{e}")
|
||||
if temp_file.exists():
|
||||
temp_file.unlink()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from pathlib import Path
|
||||
|
||||
from ruamel.yaml import YAML
|
||||
from utils.manager import plugins_manager
|
||||
from utils.manager import plugins_manager, plugin_data_manager
|
||||
from utils.utils import get_matchers
|
||||
import nonebot
|
||||
from services.log import logger
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
@@ -13,70 +13,45 @@ except ModuleNotFoundError:
|
||||
_yaml = YAML(typ="safe")
|
||||
|
||||
|
||||
def init_plugins_data(data_path):
|
||||
def init_plugins_data():
|
||||
"""
|
||||
初始化插件数据信息
|
||||
"""
|
||||
plugin2data_file = data_path / "manager" / "plugin_manager.json"
|
||||
plugin2data_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
_data = {}
|
||||
if plugin2data_file.exists():
|
||||
_data = json.load(open(plugin2data_file, "r", encoding="utf8"))
|
||||
_matchers = get_matchers()
|
||||
for matcher in _matchers:
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
for matcher in get_matchers(True):
|
||||
_plugin = matcher.plugin
|
||||
if not _plugin:
|
||||
continue
|
||||
try:
|
||||
_module = _plugin.module
|
||||
except AttributeError:
|
||||
if matcher.plugin_name not in _data.keys():
|
||||
if matcher.plugin_name not in plugins_manager.keys():
|
||||
plugins_manager.add_plugin_data(
|
||||
matcher.plugin_name, matcher.plugin_name, error=True
|
||||
)
|
||||
else:
|
||||
plugins_manager.set_module_data(matcher.plugin_name, "error", True)
|
||||
plugin_data = plugins_manager.get(matcher.plugin_name)
|
||||
if plugin_data:
|
||||
plugins_manager.set_module_data(
|
||||
matcher.plugin_name, "version", plugin_data.get("version")
|
||||
)
|
||||
plugins_manager[matcher.plugin_name].error = True
|
||||
else:
|
||||
try:
|
||||
plugin_version = _module.__getattribute__("__plugin_version__")
|
||||
except AttributeError:
|
||||
plugin_version = None
|
||||
try:
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
except AttributeError:
|
||||
plugin_name = matcher.plugin_name
|
||||
try:
|
||||
plugin_author = _module.__getattribute__("__plugin_author__")
|
||||
except AttributeError:
|
||||
plugin_author = None
|
||||
if matcher.plugin_name in plugins_manager.keys():
|
||||
plugins_manager.set_module_data(matcher.plugin_name, "error", False)
|
||||
if matcher.plugin_name not in plugins_manager.keys():
|
||||
plugins_manager.add_plugin_data(
|
||||
matcher.plugin_name,
|
||||
plugin_name=plugin_name,
|
||||
author=plugin_author,
|
||||
version=plugin_version,
|
||||
)
|
||||
elif plugins_manager[matcher.plugin_name]["version"] is None or (
|
||||
plugin_version is not None
|
||||
and plugin_version > plugins_manager[matcher.plugin_name]["version"]
|
||||
):
|
||||
plugins_manager.set_module_data(
|
||||
matcher.plugin_name, "plugin_name", plugin_name
|
||||
)
|
||||
plugins_manager.set_module_data(matcher.plugin_name, "author", plugin_author)
|
||||
plugins_manager.set_module_data(
|
||||
matcher.plugin_name, "version", plugin_version
|
||||
)
|
||||
if matcher.plugin_name in _data.keys():
|
||||
plugins_manager.set_module_data(
|
||||
matcher.plugin_name, "error", _data[matcher.plugin_name]["error"]
|
||||
)
|
||||
plugins_manager.set_module_data(
|
||||
matcher.plugin_name, "plugin_name", _data[matcher.plugin_name]["plugin_name"]
|
||||
)
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
try:
|
||||
plugin_version = plugin_data.plugin_status.version
|
||||
plugin_name = plugin_data.name
|
||||
plugin_author = plugin_data.plugin_status.author
|
||||
if matcher.plugin_name in plugins_manager.keys():
|
||||
plugins_manager[matcher.plugin_name].error = False
|
||||
if matcher.plugin_name not in plugins_manager.keys():
|
||||
plugins_manager.add_plugin_data(
|
||||
matcher.plugin_name,
|
||||
plugin_name=plugin_name,
|
||||
author=plugin_author,
|
||||
version=plugin_version,
|
||||
)
|
||||
elif isinstance(plugin_version, str) or plugins_manager[matcher.plugin_name].version is None or (
|
||||
plugin_version is not None
|
||||
and plugin_version > float(plugins_manager[matcher.plugin_name].version)
|
||||
):
|
||||
plugins_manager[matcher.plugin_name].plugin_name = plugin_name
|
||||
plugins_manager[matcher.plugin_name].author = plugin_author
|
||||
plugins_manager[matcher.plugin_name].version = plugin_version
|
||||
except Exception as e:
|
||||
logger.error(f"插件数据 {matcher.plugin_name} 加载发生错误 {type(e)}:{e}")
|
||||
plugins_manager.save()
|
||||
|
||||
@@ -1,157 +1,64 @@
|
||||
from pathlib import Path
|
||||
from ruamel.yaml import round_trip_load, round_trip_dump, YAML
|
||||
from utils.manager import (
|
||||
plugins2cd_manager,
|
||||
plugins2block_manager,
|
||||
plugins2count_manager,
|
||||
plugin_data_manager,
|
||||
)
|
||||
from utils.utils import get_matchers
|
||||
from ruamel import yaml
|
||||
import nonebot
|
||||
from configs.path_config import DATA_PATH
|
||||
|
||||
|
||||
_yaml = YAML(typ="safe")
|
||||
|
||||
|
||||
def init_plugins_cd_limit(data_path):
|
||||
def init_plugins_cd_limit():
|
||||
"""
|
||||
加载 cd 限制
|
||||
"""
|
||||
plugins2cd_file = data_path / "configs" / "plugins2cd.yaml"
|
||||
plugins2cd_file = DATA_PATH / "configs" / "plugins2cd.yaml"
|
||||
plugins2cd_file.parent.mkdir(exist_ok=True, parents=True)
|
||||
_data = {}
|
||||
_matchers = get_matchers()
|
||||
for matcher in _matchers:
|
||||
if not plugins2cd_manager.get_plugin_cd_data(matcher.plugin_name):
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
try:
|
||||
_module = _plugin.module
|
||||
plugin_cd_limit = _module.__getattribute__("__plugin_cd_limit__")
|
||||
for matcher in get_matchers(True):
|
||||
if not plugins2cd_manager.get_plugin_cd_data(matcher.plugin_name) and (
|
||||
plugin_data := plugin_data_manager.get(matcher.plugin_name)
|
||||
):
|
||||
if plugin_data.plugin_cd:
|
||||
plugins2cd_manager.add_cd_limit(
|
||||
matcher.plugin_name, **plugin_cd_limit
|
||||
matcher.plugin_name, plugin_data.plugin_cd
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
if not plugins2cd_manager.keys():
|
||||
plugins2cd_manager.add_cd_limit(
|
||||
"这是一个示例"
|
||||
)
|
||||
_tmp_data = {"PluginCdLimit": plugins2cd_manager.get_data()}
|
||||
with open(plugins2cd_file, "w", encoding="utf8") as wf:
|
||||
yaml.dump(_tmp_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
_data = round_trip_load(open(plugins2cd_file, encoding="utf8"))
|
||||
_data["PluginCdLimit"].yaml_set_start_comment(
|
||||
"""# 需要cd的功能
|
||||
# 自定义的功能需要cd也可以在此配置
|
||||
# key:模块名称
|
||||
# cd:cd 时长(秒)
|
||||
# status:此限制的开关状态
|
||||
# check_type:'private'/'group'/'all',限制私聊/群聊/全部
|
||||
# limit_type:监听对象,以user_id或group_id作为键来限制,'user':用户id,'group':群id
|
||||
# 示例:'user':用户N秒内触发1次,'group':群N秒内触发1次
|
||||
# rst:回复的话,可以添加[at],[uname],[nickname]来对应艾特,用户群名称,昵称系统昵称
|
||||
# rst 为 "" 或 None 时则不回复
|
||||
# rst示例:"[uname]你冲的太快了,[nickname]先生,请稍后再冲[at]"
|
||||
# rst回复:"老色批你冲的太快了,欧尼酱先生,请稍后再冲@老色批"
|
||||
# 用户昵称↑ 昵称系统的昵称↑ 艾特用户↑""",
|
||||
indent=2,
|
||||
)
|
||||
with open(plugins2cd_file, "w", encoding="utf8") as wf:
|
||||
round_trip_dump(_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
plugins2cd_manager.add_cd_limit("这是一个示例")
|
||||
plugins2cd_manager.save()
|
||||
plugins2cd_manager.reload_cd_limit()
|
||||
|
||||
|
||||
def init_plugins_block_limit(data_path):
|
||||
def init_plugins_block_limit():
|
||||
"""
|
||||
加载阻塞限制
|
||||
"""
|
||||
plugins2block_file = data_path / "configs" / "plugins2block.yaml"
|
||||
plugins2block_file.parent.mkdir(exist_ok=True, parents=True)
|
||||
_data = {}
|
||||
_matchers = get_matchers()
|
||||
for matcher in _matchers:
|
||||
if not plugins2block_manager.get_plugin_block_data(matcher.plugin_name):
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
try:
|
||||
_module = _plugin.module
|
||||
plugin_block_limit = _module.__getattribute__("__plugin_block_limit__")
|
||||
for matcher in get_matchers(True):
|
||||
if not plugins2block_manager.get_plugin_block_data(matcher.plugin_name) and (
|
||||
plugin_data := plugin_data_manager.get(matcher.plugin_name)
|
||||
):
|
||||
if plugin_data.plugin_block:
|
||||
plugins2block_manager.add_block_limit(
|
||||
matcher.plugin_name, **plugin_block_limit
|
||||
matcher.plugin_name, plugin_data.plugin_block
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
if not plugins2block_manager.keys():
|
||||
plugins2block_manager.add_block_limit(
|
||||
"这是一个示例"
|
||||
)
|
||||
_tmp_data = {"PluginBlockLimit": plugins2block_manager.get_data()}
|
||||
with open(plugins2block_file, "w", encoding="utf8") as wf:
|
||||
yaml.dump(_tmp_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
_data = round_trip_load(open(plugins2block_file, encoding="utf8"))
|
||||
_data["PluginBlockLimit"].yaml_set_start_comment(
|
||||
"""# 用户调用阻塞
|
||||
# 即 当用户调用此功能还未结束时
|
||||
# 用发送消息阻止用户重复调用此命令直到该命令结束
|
||||
# key:模块名称
|
||||
# status:此限制的开关状态
|
||||
# check_type:'private'/'group'/'all',限制私聊/群聊/全部
|
||||
# limit_type:监听对象,以user_id或group_id作为键来限制,'user':用户id,'group':群id
|
||||
# 示例:'user':阻塞用户,'group':阻塞群聊
|
||||
# rst:回复的话,可以添加[at],[uname],[nickname]来对应艾特,用户群名称,昵称系统昵称
|
||||
# rst 为 "" 或 None 时则不回复
|
||||
# rst示例:"[uname]你冲的太快了,[nickname]先生,请稍后再冲[at]"
|
||||
# rst回复:"老色批你冲的太快了,欧尼酱先生,请稍后再冲@老色批"
|
||||
# 用户昵称↑ 昵称系统的昵称↑ 艾特用户↑""",
|
||||
indent=2,
|
||||
)
|
||||
with open(plugins2block_file, "w", encoding="utf8") as wf:
|
||||
round_trip_dump(_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
plugins2block_manager.add_block_limit("这是一个示例")
|
||||
plugins2block_manager.save()
|
||||
plugins2block_manager.reload_block_limit()
|
||||
|
||||
|
||||
def init_plugins_count_limit(data_path):
|
||||
def init_plugins_count_limit():
|
||||
"""
|
||||
加载次数限制
|
||||
"""
|
||||
plugins2count_file = data_path / "configs" / "plugins2count.yaml"
|
||||
plugins2count_file.parent.mkdir(exist_ok=True, parents=True)
|
||||
_data = {}
|
||||
_matchers = get_matchers()
|
||||
for matcher in _matchers:
|
||||
if not plugins2count_manager.get_plugin_count_data(matcher.plugin_name):
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
try:
|
||||
_module = _plugin.module
|
||||
plugin_count_limit = _module.__getattribute__("__plugin_count_limit__")
|
||||
for matcher in get_matchers(True):
|
||||
if not plugins2count_manager.get_plugin_count_data(matcher.plugin_name) and (
|
||||
plugin_data := plugin_data_manager.get(matcher.plugin_name)
|
||||
):
|
||||
if plugin_data.plugin_count:
|
||||
plugins2count_manager.add_count_limit(
|
||||
matcher.plugin_name, **plugin_count_limit
|
||||
matcher.plugin_name, plugin_data.plugin_count
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
if not plugins2count_manager.keys():
|
||||
plugins2count_manager.add_count_limit(
|
||||
"这是一个示例"
|
||||
)
|
||||
_tmp_data = {"PluginCountLimit": plugins2count_manager.get_data()}
|
||||
with open(plugins2count_file, "w", encoding="utf8") as wf:
|
||||
yaml.dump(_tmp_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
_data = round_trip_load(open(plugins2count_file, encoding="utf8"))
|
||||
_data["PluginCountLimit"].yaml_set_start_comment(
|
||||
"""# 命令每日次数限制
|
||||
# 即 用户/群聊 每日可调用命令的次数 [数据内存存储,重启将会重置]
|
||||
# 每日调用直到 00:00 刷新
|
||||
# key:模块名称
|
||||
# max_count: 每日调用上限
|
||||
# status:此限制的开关状态
|
||||
# limit_type:监听对象,以user_id或group_id作为键来限制,'user':用户id,'group':群id
|
||||
# 示例:'user':用户上限,'group':群聊上限
|
||||
# rst:回复的话,可以添加[at],[uname],[nickname]来对应艾特,用户群名称,昵称系统昵称
|
||||
# rst 为 "" 或 None 时则不回复
|
||||
# rst示例:"[uname]你冲的太快了,[nickname]先生,请稍后再冲[at]"
|
||||
# rst回复:"老色批你冲的太快了,欧尼酱先生,请稍后再冲@老色批"
|
||||
# 用户昵称↑ 昵称系统的昵称↑ 艾特用户↑""",
|
||||
indent=2,
|
||||
)
|
||||
with open(plugins2count_file, "w", encoding="utf8") as wf:
|
||||
round_trip_dump(_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
plugins2count_manager.add_count_limit("这是一个示例")
|
||||
plugins2count_manager.save()
|
||||
plugins2count_manager.reload_count_limit()
|
||||
|
||||
@@ -1,43 +1,25 @@
|
||||
from utils.manager import resources_manager
|
||||
from utils.manager import resources_manager, plugin_data_manager
|
||||
from utils.utils import get_matchers
|
||||
from services.log import logger
|
||||
from pathlib import Path
|
||||
import nonebot
|
||||
|
||||
|
||||
def init_plugins_resources():
|
||||
"""
|
||||
资源文件路径的移动
|
||||
"""
|
||||
_tmp = []
|
||||
for matcher in get_matchers():
|
||||
if matcher.plugin_name not in _tmp:
|
||||
_tmp.append(matcher.plugin_name)
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
for matcher in get_matchers(True):
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
try:
|
||||
_module = _plugin.module
|
||||
_module = matcher.plugin.module
|
||||
except AttributeError:
|
||||
logger.warning(f"插件 {matcher.plugin_name} 加载失败...,资源控制未加载...")
|
||||
else:
|
||||
try:
|
||||
resources = _module.__getattribute__("__plugin_resources__")
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
if resources := plugin_data.plugin_resources:
|
||||
path = Path(_module.__getattribute__("__file__")).parent
|
||||
for resource in resources.keys():
|
||||
resources_manager.add_resource(matcher.plugin_name, path / resource, resources[resource])
|
||||
resources_manager.add_resource(
|
||||
matcher.plugin_name, path / resource, resources[resource]
|
||||
)
|
||||
resources_manager.save()
|
||||
resources_manager.start_move()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,128 +1,44 @@
|
||||
from pathlib import Path
|
||||
from ruamel.yaml import round_trip_load, round_trip_dump, YAML
|
||||
from utils.manager import plugins2settings_manager, admin_manager
|
||||
from utils.manager import plugins2settings_manager, admin_manager, plugin_data_manager
|
||||
from services.log import logger
|
||||
from utils.manager.models import PluginType
|
||||
from utils.utils import get_matchers
|
||||
from ruamel import yaml
|
||||
import nonebot
|
||||
|
||||
|
||||
_yaml = YAML(typ="safe")
|
||||
|
||||
|
||||
def init_plugins_settings(data_path: str):
|
||||
def init_plugins_settings():
|
||||
"""
|
||||
初始化插件设置,从插件中获取 __zx_plugin_name__,__plugin_cmd__,__plugin_settings__
|
||||
"""
|
||||
plugins2settings_file = data_path / "configs" / "plugins2settings.yaml"
|
||||
plugins2settings_file.parent.mkdir(exist_ok=True, parents=True)
|
||||
_matchers = get_matchers()
|
||||
_tmp_module = {}
|
||||
_tmp = []
|
||||
for x in plugins2settings_manager.keys():
|
||||
# for x in plugins2settings_manager.keys():
|
||||
# try:
|
||||
# _plugin = nonebot.plugin.get_plugin(x)
|
||||
# _module = _plugin.module
|
||||
# _module.__getattribute__("__zx_plugin_name__")
|
||||
# except (KeyError, AttributeError) as e:
|
||||
# logger.warning(f"配置文件 模块:{x} 获取 plugin_name 失败...{e}")
|
||||
for matcher in get_matchers(True):
|
||||
try:
|
||||
_plugin = nonebot.plugin.get_plugin(x)
|
||||
_module = _plugin.module
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
_tmp_module[x] = plugin_name
|
||||
except (KeyError, AttributeError) as e:
|
||||
logger.warning(f"配置文件 模块:{x} 获取 plugin_name 失败...{e}")
|
||||
_tmp_module[x] = ""
|
||||
for matcher in _matchers:
|
||||
if matcher.plugin_name not in plugins2settings_manager.keys():
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
try:
|
||||
_module = _plugin.module
|
||||
except AttributeError:
|
||||
logger.warning(f"插件 {matcher.plugin_name} 加载失败...,插件控制未加载.")
|
||||
else:
|
||||
if matcher.plugin_name not in plugins2settings_manager.keys():
|
||||
_plugin = matcher.plugin
|
||||
try:
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
if "[admin]" in plugin_name.lower():
|
||||
try:
|
||||
admin_settings = _module.__getattribute__(
|
||||
"__plugin_settings__"
|
||||
)
|
||||
level = admin_settings["admin_level"]
|
||||
cmd = admin_settings.get("cmd")
|
||||
except (AttributeError, KeyError):
|
||||
level = 5
|
||||
cmd = None
|
||||
if level is None:
|
||||
level = 5
|
||||
admin_manager.add_admin_plugin_settings(
|
||||
matcher.plugin_name, cmd, level
|
||||
)
|
||||
if (
|
||||
"[hidden]" in plugin_name.lower()
|
||||
or "[admin]" in plugin_name.lower()
|
||||
or "[superuser]" in plugin_name.lower()
|
||||
or matcher.plugin_name in plugins2settings_manager.keys()
|
||||
):
|
||||
continue
|
||||
_module = _plugin.module
|
||||
except AttributeError:
|
||||
if matcher.plugin_name not in _tmp:
|
||||
logger.warning(
|
||||
f"获取插件 {matcher.plugin_name} __zx_plugin_name__ 失败...,插件控制未加载."
|
||||
)
|
||||
logger.warning(f"插件 {matcher.plugin_name} 加载失败...,插件控制未加载.")
|
||||
else:
|
||||
try:
|
||||
_tmp_module[matcher.plugin_name] = plugin_name
|
||||
plugin_settings = _module.__getattribute__(
|
||||
"__plugin_settings__"
|
||||
)
|
||||
if plugin_settings.get('cost_gold') is None:
|
||||
plugin_settings['cost_gold'] = 0
|
||||
if (
|
||||
plugin_settings.get("cmd") is not None
|
||||
and plugin_name not in plugin_settings["cmd"]
|
||||
):
|
||||
plugin_settings["cmd"].append(plugin_name)
|
||||
if plugins2settings_manager.get(
|
||||
matcher.plugin_name
|
||||
) and plugins2settings_manager[matcher.plugin_name].get(
|
||||
"plugin_type"
|
||||
):
|
||||
plugin_type = tuple(
|
||||
plugins2settings_manager.get_plugin_data(
|
||||
matcher.plugin_name
|
||||
)["plugin_type"]
|
||||
)
|
||||
else:
|
||||
try:
|
||||
plugin_type = _module.__getattribute__(
|
||||
"__plugin_type__"
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
if plugin_settings := plugin_data.plugin_setting:
|
||||
if (name := _module.__getattribute__("__zx_plugin_name__")) not in plugin_settings.cmd:
|
||||
plugin_settings.cmd.append(name)
|
||||
# 管理员命令
|
||||
if plugin_data.plugin_type == PluginType.ADMIN:
|
||||
admin_manager.add_admin_plugin_settings(
|
||||
matcher.plugin_name, plugin_settings.cmd, plugin_settings.level
|
||||
)
|
||||
except AttributeError:
|
||||
plugin_type = ("normal",)
|
||||
if plugin_settings and matcher.plugin_name:
|
||||
plugins2settings_manager.add_plugin_settings(
|
||||
matcher.plugin_name,
|
||||
plugin_type=plugin_type,
|
||||
**plugin_settings,
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
_tmp.append(matcher.plugin_name)
|
||||
_tmp_data = {"PluginSettings": plugins2settings_manager.get_data()}
|
||||
with open(plugins2settings_file, "w", encoding="utf8") as wf:
|
||||
yaml.dump(_tmp_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
_data = round_trip_load(open(plugins2settings_file, encoding="utf8"))
|
||||
_data["PluginSettings"].yaml_set_start_comment(
|
||||
"""# 模块与对应命令和对应群权限
|
||||
# 用于生成帮助图片 和 开关功能
|
||||
# key:模块名称
|
||||
# level:需要的群等级
|
||||
# default_status:加入群时功能的默认开关状态
|
||||
# limit_superuser: 功能状态是否限制超级用户
|
||||
# cmd: 关闭[cmd] 都会触发命令 关闭对应功能,cmd列表第一个词为统计的功能名称
|
||||
# plugin_type: 帮助类别 示例:('原神相关',) 或 ('原神相关', 1),1代表帮助命令列向排列,否则为横向排列""",
|
||||
indent=2,
|
||||
)
|
||||
for plugin in _data["PluginSettings"].keys():
|
||||
_data["PluginSettings"][plugin].yaml_set_start_comment(
|
||||
f"{plugin}:{_tmp_module[plugin]}", indent=2
|
||||
)
|
||||
with open(plugins2settings_file, "w", encoding="utf8") as wf:
|
||||
round_trip_dump(_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
else:
|
||||
plugins2settings_manager.add_plugin_settings(
|
||||
matcher.plugin_name, plugin_settings
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f'{matcher.plugin_name} 初始化 plugin_settings 发生错误 {type(e)}:{e}')
|
||||
plugins2settings_manager.save()
|
||||
logger.info(f"已成功加载 {len(plugins2settings_manager.get_data())} 个非限制插件.")
|
||||
|
||||
@@ -1,127 +1,145 @@
|
||||
from nonebot import on_request, on_message
|
||||
import asyncio
|
||||
import re
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from nonebot import on_message, on_request
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
ActionFailed,
|
||||
Bot,
|
||||
FriendRequestEvent,
|
||||
GroupRequestEvent,
|
||||
MessageEvent,
|
||||
)
|
||||
from models.friend_user import FriendUser
|
||||
from datetime import datetime
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from nonebot.adapters.onebot.v11.exception import ActionFailed
|
||||
from utils.manager import requests_manager
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
from utils.manager import requests_manager
|
||||
from utils.utils import scheduler
|
||||
import asyncio
|
||||
import time
|
||||
import re
|
||||
|
||||
from .utils import time_manager
|
||||
|
||||
__zx_plugin_name__ = "好友群聊处理请求 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_configs__ = {
|
||||
"AUTO_ADD_FRIEND": {"value": False, "help": "是否自动同意好友添加", "default_value": False}
|
||||
"AUTO_ADD_FRIEND": {
|
||||
"value": False,
|
||||
"help": "是否自动同意好友添加",
|
||||
"default_value": False,
|
||||
"type": bool,
|
||||
}
|
||||
}
|
||||
|
||||
friend_req = on_request(priority=5, block=True)
|
||||
group_req = on_request(priority=5, block=True)
|
||||
x = on_message(priority=999, block=False)
|
||||
|
||||
exists_data = {"private": {}, "group": {}}
|
||||
x = on_message(priority=999, block=False, rule=lambda: False)
|
||||
|
||||
|
||||
@friend_req.handle()
|
||||
async def _(bot: Bot, event: FriendRequestEvent):
|
||||
global exists_data
|
||||
if exists_data["private"].get(event.user_id):
|
||||
if time.time() - exists_data["private"][event.user_id] < 60 * 5:
|
||||
return
|
||||
exists_data["private"][event.user_id] = time.time()
|
||||
user = await bot.get_stranger_info(user_id=event.user_id)
|
||||
nickname = user["nickname"]
|
||||
sex = user["sex"]
|
||||
age = str(user["age"])
|
||||
comment = event.comment
|
||||
await bot.send_private_msg(
|
||||
user_id=int(list(bot.config.superusers)[0]),
|
||||
message=f"*****一份好友申请*****\n"
|
||||
f"昵称:{nickname}({event.user_id})\n"
|
||||
f"自动同意:{'√' if Config.get_config('invite_manager', 'AUTO_ADD_FRIEND') else '×'}\n"
|
||||
f"日期:{str(datetime.now()).split('.')[0]}\n"
|
||||
f"备注:{event.comment}",
|
||||
)
|
||||
if Config.get_config("invite_manager", "AUTO_ADD_FRIEND"):
|
||||
await bot.set_friend_add_request(flag=event.flag, approve=True)
|
||||
await FriendUser.add_friend_info(user["user_id"], user["nickname"])
|
||||
else:
|
||||
requests_manager.add_request(
|
||||
event.user_id,
|
||||
"private",
|
||||
event.flag,
|
||||
nickname=nickname,
|
||||
sex=sex,
|
||||
age=age,
|
||||
comment=comment,
|
||||
if time_manager.add_user_request(event.user_id):
|
||||
logger.debug(f"收录 用户[{event.user_id}] 好友请求", "好友请求")
|
||||
user = await bot.get_stranger_info(user_id=event.user_id)
|
||||
nickname = user["nickname"]
|
||||
sex = user["sex"]
|
||||
age = str(user["age"])
|
||||
comment = event.comment
|
||||
await bot.send_private_msg(
|
||||
user_id=int(list(bot.config.superusers)[0]),
|
||||
message=f"*****一份好友申请*****\n"
|
||||
f"昵称:{nickname}({event.user_id})\n"
|
||||
f"自动同意:{'√' if Config.get_config('invite_manager', 'AUTO_ADD_FRIEND') else '×'}\n"
|
||||
f"日期:{str(datetime.now()).split('.')[0]}\n"
|
||||
f"备注:{event.comment}",
|
||||
)
|
||||
if Config.get_config("invite_manager", "AUTO_ADD_FRIEND"):
|
||||
logger.debug(f"已开启好友请求自动同意,成功通过该请求", "好友请求", target=event.user_id)
|
||||
await bot.set_friend_add_request(flag=event.flag, approve=True)
|
||||
await FriendUser.create(user_id=user["user_id"], user_name=user["nickname"])
|
||||
else:
|
||||
requests_manager.add_request(
|
||||
event.user_id,
|
||||
"private",
|
||||
event.flag,
|
||||
nickname=nickname,
|
||||
sex=sex,
|
||||
age=age,
|
||||
comment=comment,
|
||||
)
|
||||
else:
|
||||
logger.debug(f"好友请求五分钟内重复, 已忽略", "好友请求", target=event.user_id)
|
||||
|
||||
|
||||
@group_req.handle()
|
||||
async def _(bot: Bot, event: GroupRequestEvent):
|
||||
global exists_data
|
||||
# 邀请
|
||||
if event.sub_type == "invite":
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
try:
|
||||
if await GroupInfo.get_group_info(event.group_id):
|
||||
await GroupInfo.set_group_flag(event.group_id, 1)
|
||||
else:
|
||||
group_info = await bot.get_group_info(group_id=event.group_id)
|
||||
await GroupInfo.add_group_info(
|
||||
group_info["group_id"],
|
||||
group_info["group_name"],
|
||||
group_info["max_member_count"],
|
||||
group_info["member_count"],
|
||||
1,
|
||||
)
|
||||
logger.debug(
|
||||
f"超级用户自动同意加入群聊", "群聊请求", event.user_id, target=event.group_id
|
||||
)
|
||||
await bot.set_group_add_request(
|
||||
flag=event.flag, sub_type="invite", approve=True
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
group_info = await bot.get_group_info(group_id=event.group_id)
|
||||
await GroupInfo.update_or_create(
|
||||
group_id=group_info["group_id"],
|
||||
defaults={
|
||||
"group_name": group_info["group_name"],
|
||||
"max_member_count": group_info["max_member_count"],
|
||||
"member_count": group_info["member_count"],
|
||||
"group_flag": 1,
|
||||
},
|
||||
)
|
||||
except ActionFailed as e:
|
||||
logger.error(
|
||||
"超级用户自动同意加入群聊发生错误",
|
||||
"群聊请求",
|
||||
event.user_id,
|
||||
target=event.group_id,
|
||||
e=e,
|
||||
)
|
||||
else:
|
||||
user = await bot.get_stranger_info(user_id=event.user_id)
|
||||
sex = user["sex"]
|
||||
age = str(user["age"])
|
||||
if exists_data["group"].get(f"{event.user_id}:{event.group_id}"):
|
||||
if (
|
||||
time.time()
|
||||
- exists_data["group"][f"{event.user_id}:{event.group_id}"]
|
||||
< 60 * 5
|
||||
):
|
||||
return
|
||||
exists_data["group"][f"{event.user_id}:{event.group_id}"] = time.time()
|
||||
nickname = await FriendUser.get_user_name(event.user_id)
|
||||
await bot.send_private_msg(
|
||||
user_id=int(list(bot.config.superusers)[0]),
|
||||
message=f"*****一份入群申请*****\n"
|
||||
f"申请人:{nickname}({event.user_id})\n"
|
||||
f"群聊:{event.group_id}\n"
|
||||
f"邀请日期:{str(datetime.now()).split('.')[0]}",
|
||||
)
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id,
|
||||
message=f"想要邀请我偷偷入群嘛~已经提醒{NICKNAME}的管理员大人了\n"
|
||||
"请确保已经群主或群管理沟通过!\n"
|
||||
"等待管理员处理吧!",
|
||||
)
|
||||
requests_manager.add_request(
|
||||
event.user_id,
|
||||
"group",
|
||||
event.flag,
|
||||
invite_group=event.group_id,
|
||||
nickname=nickname,
|
||||
sex=sex,
|
||||
age=age,
|
||||
)
|
||||
if time_manager.add_group_request(event.user_id, event.group_id):
|
||||
logger.debug(
|
||||
f"收录 用户[{event.user_id}] 群聊[{event.group_id}] 群聊请求", "群聊请求"
|
||||
)
|
||||
user = await bot.get_stranger_info(user_id=event.user_id)
|
||||
sex = user["sex"]
|
||||
age = str(user["age"])
|
||||
nickname = await FriendUser.get_user_name(event.user_id)
|
||||
await bot.send_private_msg(
|
||||
user_id=int(list(bot.config.superusers)[0]),
|
||||
message=f"*****一份入群申请*****\n"
|
||||
f"申请人:{nickname}({event.user_id})\n"
|
||||
f"群聊:{event.group_id}\n"
|
||||
f"邀请日期:{datetime.now().replace(microsecond=0)}",
|
||||
)
|
||||
await bot.send_private_msg(
|
||||
user_id=event.user_id,
|
||||
message=f"想要邀请我偷偷入群嘛~已经提醒{NICKNAME}的管理员大人了\n"
|
||||
"请确保已经群主或群管理沟通过!\n"
|
||||
"等待管理员处理吧!",
|
||||
)
|
||||
requests_manager.add_request(
|
||||
event.user_id,
|
||||
"group",
|
||||
event.flag,
|
||||
invite_group=event.group_id,
|
||||
nickname=nickname,
|
||||
sex=sex,
|
||||
age=age,
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f"群聊请求五分钟内重复, 已忽略",
|
||||
"群聊请求",
|
||||
target=f"{event.user_id}:{event.group_id}",
|
||||
)
|
||||
|
||||
|
||||
@x.handle()
|
||||
@@ -145,5 +163,4 @@ async def _(event: MessageEvent):
|
||||
minutes=5,
|
||||
)
|
||||
async def _():
|
||||
global exists_data
|
||||
exists_data = {"private": {}, "group": {}}
|
||||
time_manager.clear()
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict
|
||||
|
||||
|
||||
@dataclass
|
||||
class PrivateRequest:
|
||||
|
||||
"""
|
||||
好友请求
|
||||
"""
|
||||
|
||||
user_id: int
|
||||
time: float = time.time()
|
||||
|
||||
|
||||
@dataclass
|
||||
class GroupRequest:
|
||||
|
||||
"""
|
||||
群聊请求
|
||||
"""
|
||||
|
||||
user_id: int
|
||||
group_id: int
|
||||
time: float = time.time()
|
||||
|
||||
|
||||
class RequestTimeManage:
|
||||
|
||||
"""
|
||||
过滤五分钟以内的重复请求
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self._group: Dict[str, GroupRequest] = {}
|
||||
self._user: Dict[int, PrivateRequest] = {}
|
||||
|
||||
def add_user_request(self, user_id: int) -> bool:
|
||||
"""
|
||||
添加请求时间
|
||||
|
||||
Args:
|
||||
user_id (int): 用户id
|
||||
|
||||
Returns:
|
||||
bool: 是否满足时间
|
||||
"""
|
||||
if user := self._user.get(user_id):
|
||||
if time.time() - user.time < 60 * 5:
|
||||
return False
|
||||
self._user[user_id] = PrivateRequest(user_id)
|
||||
return True
|
||||
|
||||
def add_group_request(self, user_id: int, group_id: int) -> bool:
|
||||
"""
|
||||
添加请求时间
|
||||
|
||||
Args:
|
||||
user_id (int): 用户id
|
||||
group_id (int): 邀请群聊
|
||||
|
||||
Returns:
|
||||
bool: 是否满足时间
|
||||
"""
|
||||
key = f"{user_id}:{group_id}"
|
||||
if group := self._group.get(key):
|
||||
if time.time() - group.time < 60 * 5:
|
||||
return False
|
||||
self._group[key] = GroupRequest(user_id=user_id, group_id=group_id)
|
||||
return True
|
||||
|
||||
def clear(self):
|
||||
"""
|
||||
清理过期五分钟请求
|
||||
"""
|
||||
now = time.time()
|
||||
for user_id in self._user:
|
||||
if now - self._user[user_id].time < 60 * 5:
|
||||
del self._user[user_id]
|
||||
for key in self._group:
|
||||
if now - self._group[key].time < 60 * 5:
|
||||
del self._group[key]
|
||||
|
||||
|
||||
time_manager = RequestTimeManage()
|
||||
+136
-129
@@ -1,114 +1,163 @@
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, PrivateMessageEvent, Message, MessageEvent
|
||||
from nonebot import on_command
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.rule import to_me
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from models.friend_user import FriendUser
|
||||
from models.ban_user import BanUser
|
||||
from services.log import logger
|
||||
from configs.config import NICKNAME, Config
|
||||
from nonebot.params import CommandArg
|
||||
import random
|
||||
from typing import Any, List, Tuple
|
||||
|
||||
from nonebot import on_command, on_regex
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
|
||||
from nonebot.internal.matcher import Matcher
|
||||
from nonebot.internal.params import Depends
|
||||
from nonebot.params import CommandArg, RegexGroup
|
||||
from nonebot.rule import to_me
|
||||
|
||||
from configs.config import NICKNAME
|
||||
from models.ban_user import BanUser
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from services.log import logger
|
||||
from utils.depends import GetConfig
|
||||
|
||||
__zx_plugin_name__ = "昵称系统"
|
||||
__plugin_usage__ = f"""
|
||||
usage:
|
||||
个人昵称系统,群聊 与 私聊 昵称相互独立
|
||||
个人昵称,将替换真寻称呼你的名称,群聊 与 私聊 昵称相互独立,全局昵称设置将更改您目前所有群聊中及私聊的昵称
|
||||
指令:
|
||||
以后叫我 [昵称]
|
||||
以后叫我 [昵称]: 设置当前群聊/私聊的昵称
|
||||
全局昵称设置 [昵称]: 设置当前所有群聊和私聊的昵称
|
||||
{NICKNAME}我是谁
|
||||
""".strip()
|
||||
__plugin_des__ = "区区昵称,才不想叫呢!"
|
||||
__plugin_cmd__ = ["以后叫我 [昵称]", f"{NICKNAME}我是谁"]
|
||||
__plugin_cmd__ = ["以后叫我 [昵称]", f"{NICKNAME}我是谁", "全局昵称设置 [昵称]"]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
"level": 5,
|
||||
"default_status": True,
|
||||
"limit_superuser": False,
|
||||
"cmd": ["昵称"],
|
||||
}
|
||||
__plugin_configs__ = {
|
||||
"BLACK_WORD": {
|
||||
"value": ["爸", "爹", "爷", "父亲"],
|
||||
"help": "昵称所屏蔽的关键词,会被替换为 *",
|
||||
"default_value": None
|
||||
"value": ["爸", "爹", "爷", "父"],
|
||||
"help": "昵称所屏蔽的关键词,已设置的昵称会被替换为 *,未设置的昵称会在设置时提示",
|
||||
"default_value": None,
|
||||
"type": List[str],
|
||||
}
|
||||
}
|
||||
|
||||
nickname = on_command(
|
||||
"nickname",
|
||||
aliases={"以后叫我", "以后请叫我", "称呼我", "以后请称呼我", "以后称呼我", "叫我", "请叫我"},
|
||||
nickname = on_regex(
|
||||
"(?:以后)?(?:叫我|请叫我|称呼我)(.*)",
|
||||
rule=to_me(),
|
||||
priority=5,
|
||||
block=True,
|
||||
)
|
||||
|
||||
my_nickname = on_command(
|
||||
"my_name", aliases={"我叫什么", "我是谁", "我的名字"}, rule=to_me(), priority=5, block=True
|
||||
"我叫什么", aliases={"我是谁", "我的名字"}, rule=to_me(), priority=5, block=True
|
||||
)
|
||||
|
||||
global_nickname = on_regex("设置全局昵称(.*)", rule=to_me(), priority=5, block=True)
|
||||
|
||||
|
||||
cancel_nickname = on_command("取消昵称", rule=to_me(), priority=5, block=True)
|
||||
|
||||
|
||||
@nickname.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
if not msg:
|
||||
await nickname.finish("叫你空白?叫你虚空?叫你无名??", at_sender=True)
|
||||
if len(msg) > 10:
|
||||
await nickname.finish("昵称可不能超过10个字!", at_sender=True)
|
||||
if msg in bot.config.superusers:
|
||||
await nickname.finish("笨蛋!休想占用我的名字!#", at_sender=True)
|
||||
_tmp = ""
|
||||
black_word = Config.get_config("nickname", "BLACK_WORD")
|
||||
for x in msg:
|
||||
_tmp += "*" if x in black_word else x
|
||||
msg = _tmp
|
||||
def CheckNickname():
|
||||
"""
|
||||
说明:
|
||||
检查名称是否合法
|
||||
"""
|
||||
|
||||
async def dependency(
|
||||
bot: Bot,
|
||||
matcher: Matcher,
|
||||
event: MessageEvent,
|
||||
reg_group: Tuple[Any, ...] = RegexGroup(),
|
||||
black_word: Any = GetConfig(config="BLACK_WORD"),
|
||||
):
|
||||
(msg,) = reg_group
|
||||
logger.debug(f"昵称检查: {msg}", "昵称设置", event.user_id)
|
||||
if not msg:
|
||||
await matcher.finish("叫你空白?叫你虚空?叫你无名??", at_sender=True)
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
logger.debug(f"超级用户设置昵称, 跳过合法检测: {msg}", "昵称设置", event.user_id)
|
||||
return
|
||||
if len(msg) > 20:
|
||||
await nickname.finish("昵称可不能超过20个字!", at_sender=True)
|
||||
if msg in bot.config.nickname:
|
||||
await nickname.finish("笨蛋!休想占用我的名字!#", at_sender=True)
|
||||
if black_word:
|
||||
for x in msg:
|
||||
if x in black_word:
|
||||
logger.debug("昵称设置禁止字符: [{x}]", "昵称设置", event.user_id)
|
||||
await nickname.finish(f"字符 [{x}] 为禁止字符!", at_sender=True)
|
||||
for word in black_word:
|
||||
if word in msg:
|
||||
logger.debug("昵称设置禁止字符: [{word}]", "昵称设置", event.user_id)
|
||||
await nickname.finish(f"字符 [{word}] 为禁止字符!", at_sender=True)
|
||||
|
||||
return Depends(dependency)
|
||||
|
||||
|
||||
@global_nickname.handle(parameterless=[CheckNickname()])
|
||||
async def _(
|
||||
event: MessageEvent,
|
||||
reg_group: Tuple[Any, ...] = RegexGroup(),
|
||||
):
|
||||
(msg,) = reg_group
|
||||
await FriendUser.set_user_nickname(event.user_id, msg)
|
||||
await GroupInfoUser.filter(user_qq=event.user_id).update(nickname=msg)
|
||||
logger.info(f"设置全局昵称成功: {msg}", "设置全局昵称", event.user_id)
|
||||
await global_nickname.send(f"设置全局昵称成功!亲爱的{msg}")
|
||||
|
||||
|
||||
@nickname.handle(parameterless=[CheckNickname()])
|
||||
async def _(
|
||||
event: MessageEvent,
|
||||
reg_group: Tuple[Any, ...] = RegexGroup(),
|
||||
):
|
||||
(msg,) = reg_group
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
if await GroupInfoUser.set_group_member_nickname(
|
||||
event.user_id, event.group_id, msg
|
||||
):
|
||||
if len(msg) < 5:
|
||||
if random.random() < 0.3:
|
||||
msg = "~".join(msg)
|
||||
await nickname.send(
|
||||
random.choice(
|
||||
[
|
||||
f"好啦好啦,我知道啦,{msg},以后就这么叫你吧",
|
||||
f"嗯嗯,{NICKNAME}记住你的昵称了哦,{msg}",
|
||||
f"好突然,突然要叫你昵称什么的...{msg}..",
|
||||
f"{NICKNAME}会好好记住{msg}的,放心吧",
|
||||
f"好..好.,那窝以后就叫你{msg}了.",
|
||||
]
|
||||
)
|
||||
await GroupInfoUser.set_user_nickname(event.user_id, event.group_id, msg)
|
||||
if len(msg) < 5:
|
||||
if random.random() < 0.3:
|
||||
msg = "~".join(msg)
|
||||
await nickname.send(
|
||||
random.choice(
|
||||
[
|
||||
f"好啦好啦,我知道啦,{msg},以后就这么叫你吧",
|
||||
f"嗯嗯,{NICKNAME}记住你的昵称了哦,{msg}",
|
||||
f"好突然,突然要叫你昵称什么的...{msg}..",
|
||||
f"{NICKNAME}会好好记住{msg}的,放心吧",
|
||||
f"好..好.,那窝以后就叫你{msg}了.",
|
||||
]
|
||||
)
|
||||
logger.info(f"USER {event.user_id} GROUP {event.group_id} 设置群昵称 {msg}")
|
||||
else:
|
||||
await nickname.send("设置昵称失败,请更新群组成员信息!", at_sender=True)
|
||||
logger.warning(f"USER {event.user_id} GROUP {event.group_id} 设置群昵称 {msg} 失败")
|
||||
)
|
||||
logger.info(f"设置群昵称成功: {msg}", "昵称设置", event.user_id, event.group_id)
|
||||
else:
|
||||
if await FriendUser.set_friend_nickname(event.user_id, msg):
|
||||
await nickname.send(
|
||||
random.choice(
|
||||
[
|
||||
f"好啦好啦,我知道啦,{msg},以后就这么叫你吧",
|
||||
f"嗯嗯,{NICKNAME}记住你的昵称了哦,{msg}",
|
||||
f"好突然,突然要叫你昵称什么的...{msg}..",
|
||||
f"{NICKNAME}会好好记住{msg}的,放心吧",
|
||||
f"好..好.,那窝以后就叫你{msg}了.",
|
||||
]
|
||||
)
|
||||
await FriendUser.set_user_nickname(event.user_id, msg)
|
||||
await nickname.send(
|
||||
random.choice(
|
||||
[
|
||||
f"好啦好啦,我知道啦,{msg},以后就这么叫你吧",
|
||||
f"嗯嗯,{NICKNAME}记住你的昵称了哦,{msg}",
|
||||
f"好突然,突然要叫你昵称什么的...{msg}..",
|
||||
f"{NICKNAME}会好好记住{msg}的,放心吧",
|
||||
f"好..好.,那窝以后就叫你{msg}了.",
|
||||
]
|
||||
)
|
||||
logger.info(f"USER {event.user_id} 设置昵称 {msg}")
|
||||
else:
|
||||
await nickname.send("设置昵称失败了,明天再来试一试!或联系管理员更新好友!", at_sender=True)
|
||||
logger.warning(f"USER {event.user_id} 设置昵称 {msg} 失败")
|
||||
)
|
||||
logger.info(f"设置私聊昵称成功: {msg}", "昵称设置", event.user_id)
|
||||
|
||||
|
||||
@my_nickname.handle()
|
||||
async def _(event: GroupMessageEvent):
|
||||
try:
|
||||
nickname_ = await GroupInfoUser.get_group_member_nickname(
|
||||
event.user_id, event.group_id
|
||||
)
|
||||
except AttributeError:
|
||||
nickname_ = ""
|
||||
async def _(event: MessageEvent):
|
||||
nickname_ = None
|
||||
card = None
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
nickname_ = await GroupInfoUser.get_user_nickname(event.user_id, event.group_id)
|
||||
card = event.sender.card or event.sender.nickname
|
||||
else:
|
||||
nickname_ = await FriendUser.get_user_nickname(event.user_id)
|
||||
card = event.sender.nickname
|
||||
if nickname_:
|
||||
await my_nickname.send(
|
||||
random.choice(
|
||||
@@ -123,44 +172,20 @@ async def _(event: GroupMessageEvent):
|
||||
)
|
||||
)
|
||||
else:
|
||||
nickname_ = event.sender.card or event.sender.nickname
|
||||
await my_nickname.send(
|
||||
random.choice(
|
||||
["没..没有昵称嘛,{}", "啊,你是{}啊,我想叫你的昵称!", "是{}啊,有什么事吗?", "你是{}?"]
|
||||
).format(nickname_)
|
||||
)
|
||||
|
||||
|
||||
@my_nickname.handle()
|
||||
async def _(bot: Bot, event: PrivateMessageEvent, state: T_State):
|
||||
nickname_ = await FriendUser.get_friend_nickname(event.user_id)
|
||||
if nickname_:
|
||||
await my_nickname.send(
|
||||
random.choice(
|
||||
[
|
||||
f"我肯定记得你啊,你是{nickname_}啊",
|
||||
f"我不会忘记你的,你也不要忘记我!{nickname_}",
|
||||
f"哼哼,{NICKNAME}记忆力可是很好的,{nickname_}",
|
||||
f"嗯?你是失忆了嘛...{nickname_}..",
|
||||
f"不要小看{NICKNAME}的记忆力啊!笨蛋{nickname_}!QAQ",
|
||||
f"哎?{nickname_}..怎么了吗..突然这样问..",
|
||||
]
|
||||
)
|
||||
)
|
||||
else:
|
||||
nickname_ = (await bot.get_stranger_info(user_id=event.user_id))["nickname"]
|
||||
await my_nickname.send(
|
||||
random.choice(
|
||||
["没..没有昵称嘛,{}", "啊,你是{}啊,我想叫你的昵称!", "是{}啊,有什么事吗?", "你是{}?"]
|
||||
).format(nickname_)
|
||||
).format(card)
|
||||
)
|
||||
|
||||
|
||||
@cancel_nickname.handle()
|
||||
async def _(event: GroupMessageEvent):
|
||||
nickname_ = await GroupInfoUser.get_group_member_nickname(
|
||||
event.user_id, event.group_id
|
||||
)
|
||||
async def _(event: MessageEvent):
|
||||
nickname_ = None
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
nickname_ = await GroupInfoUser.get_user_nickname(event.user_id, event.group_id)
|
||||
else:
|
||||
nickname_ = await FriendUser.get_user_nickname(event.user_id)
|
||||
if nickname_:
|
||||
await cancel_nickname.send(
|
||||
random.choice(
|
||||
@@ -173,28 +198,10 @@ async def _(event: GroupMessageEvent):
|
||||
]
|
||||
)
|
||||
)
|
||||
await GroupInfoUser.set_group_member_nickname(event.user_id, event.group_id, "")
|
||||
await BanUser.ban(event.user_id, 9, 60)
|
||||
else:
|
||||
await cancel_nickname.send("你在做梦吗?你没有昵称啊", at_sender=True)
|
||||
|
||||
|
||||
@cancel_nickname.handle()
|
||||
async def _(event: PrivateMessageEvent):
|
||||
nickname_ = await FriendUser.get_friend_nickname(event.user_id)
|
||||
if nickname_:
|
||||
await cancel_nickname.send(
|
||||
random.choice(
|
||||
[
|
||||
f"呜..{NICKNAME}睡一觉就会忘记的..和梦一样..{nickname_}",
|
||||
f"窝知道了..{nickname_}..",
|
||||
f"是{NICKNAME}哪里做的不好嘛..好吧..晚安{nickname_}",
|
||||
f"呃,{nickname_},下次我绝对绝对绝对不会再忘记你!",
|
||||
f"可..可恶!{nickname_}!太可恶了!呜",
|
||||
]
|
||||
)
|
||||
)
|
||||
await FriendUser.get_user_name(event.user_id)
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
await GroupInfoUser.set_user_nickname(event.user_id, event.group_id, "")
|
||||
else:
|
||||
await FriendUser.set_user_nickname(event.user_id, "")
|
||||
await BanUser.ban(event.user_id, 9, 60)
|
||||
else:
|
||||
await cancel_nickname.send("你在做梦吗?你没有昵称啊", at_sender=True)
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
from nonebot import on_command, on_regex
|
||||
|
||||
from .data_source import show_plugin_repo, install_plugin, uninstall_plugin, download_json
|
||||
from services.log import logger
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
|
||||
from nonebot.params import CommandArg
|
||||
|
||||
from utils.message_builder import image
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from services.log import logger
|
||||
from utils.message_builder import image
|
||||
|
||||
from .data_source import (
|
||||
download_json,
|
||||
install_plugin,
|
||||
show_plugin_repo,
|
||||
uninstall_plugin,
|
||||
)
|
||||
|
||||
__zx_plugin_name__ = "插件商店 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
@@ -20,7 +24,7 @@ usage:
|
||||
卸载插件 [name/id]
|
||||
""".strip()
|
||||
__plugin_des__ = "从真寻适配仓库中下载插件"
|
||||
__plugin_cmd__ = [""]
|
||||
__plugin_cmd__ = ["查看插件仓库", "更新插件仓库", "安装插件 [name/id]", "卸载插件 [name/id]"]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
@@ -28,23 +32,29 @@ show_repo = on_regex("^查看插件仓库$", priority=1, block=True, permission=
|
||||
|
||||
update_repo = on_regex("^更新插件仓库$", priority=1, block=True, permission=SUPERUSER)
|
||||
|
||||
install_plugin_matcher = on_command("安装插件", priority=1, block=True, permission=SUPERUSER)
|
||||
install_plugin_matcher = on_command(
|
||||
"安装插件", priority=1, block=True, permission=SUPERUSER
|
||||
)
|
||||
|
||||
uninstall_plugin_matcher = on_command("卸载插件", priority=1, block=True, permission=SUPERUSER)
|
||||
uninstall_plugin_matcher = on_command(
|
||||
"卸载插件", priority=1, block=True, permission=SUPERUSER
|
||||
)
|
||||
|
||||
|
||||
@install_plugin_matcher.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
arg = arg.extract_plain_text().strip()
|
||||
msg = await install_plugin(arg)
|
||||
name = arg.extract_plain_text().strip()
|
||||
msg = await install_plugin(name)
|
||||
await install_plugin_matcher.send(msg)
|
||||
logger.info(f"安装插件: {name}", "安装插件", event.user_id)
|
||||
|
||||
|
||||
@uninstall_plugin_matcher.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
arg = arg.extract_plain_text().strip()
|
||||
msg = await uninstall_plugin(arg)
|
||||
name = arg.extract_plain_text().strip()
|
||||
msg = await uninstall_plugin(name)
|
||||
await install_plugin_matcher.send(msg)
|
||||
logger.info(f"卸载插件: {name}", "卸载插件", event.user_id)
|
||||
|
||||
|
||||
@update_repo.handle()
|
||||
@@ -53,16 +63,13 @@ async def _(bot: Bot, event: MessageEvent):
|
||||
if code == 200:
|
||||
await update_repo.finish("更新插件仓库信息成功!")
|
||||
await update_repo.send("更新插件仓库信息失败!")
|
||||
|
||||
logger.info("更新插件仓库信息", "更新插件仓库信息", event.user_id)
|
||||
|
||||
|
||||
@show_repo.handle()
|
||||
async def _(bot: Bot, event: MessageEvent):
|
||||
msg = await show_plugin_repo()
|
||||
if isinstance(msg, int):
|
||||
await show_repo.finish("文件下载失败或解压失败..")
|
||||
await show_repo.send(image(b64=msg))
|
||||
logger.info(
|
||||
f"(USER {event.user_id}, GROUP {event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
|
||||
f" 查看插件仓库"
|
||||
)
|
||||
|
||||
await show_repo.send(image(msg))
|
||||
logger.info("查看插件仓库", "查看插件仓库", event.user_id)
|
||||
|
||||
@@ -2,15 +2,15 @@ import os
|
||||
import shutil
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from typing import Union, Tuple
|
||||
from utils.manager import plugins_manager
|
||||
from typing import Tuple, Union
|
||||
|
||||
import ujson as json
|
||||
|
||||
from configs.path_config import DATA_PATH, TEMP_PATH
|
||||
from services import logger
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from configs.path_config import TEMP_PATH, DATA_PATH
|
||||
from utils.image_utils import text2image, BuildImage
|
||||
from utils.image_utils import BuildImage, text2image
|
||||
from utils.manager import plugins_manager
|
||||
from utils.utils import is_number
|
||||
|
||||
path = DATA_PATH / "plugin_shop"
|
||||
@@ -48,7 +48,7 @@ async def install_plugin(name: str) -> str:
|
||||
if zip_file.exists():
|
||||
zip_file.unlink()
|
||||
if await AsyncHttpx.download_file(url, zip_file):
|
||||
logger.debug("开始解压插件压缩包...")
|
||||
logger.debug("开始解压插件压缩包...", "安装插件", target=name)
|
||||
# 解压
|
||||
zf = zipfile.ZipFile(zip_file, "r")
|
||||
extract_path = TEMP_PATH / f"{name}"
|
||||
@@ -58,32 +58,34 @@ async def install_plugin(name: str) -> str:
|
||||
for file in zf.namelist():
|
||||
zf.extract(file, extract_path)
|
||||
zf.close()
|
||||
logger.debug("解压插件压缩包完成...")
|
||||
logger.debug("开始移动插件文件夹...")
|
||||
logger.debug("解压插件压缩包完成...", "安装插件", target=name)
|
||||
logger.debug("开始移动插件文件夹...", "安装插件", target=name)
|
||||
if (extensive_plugin_path / f"{name}").exists():
|
||||
logger.debug("extensive_plugin目录下文件夹已存在,删除该目录插件文件夹...")
|
||||
logger.debug(
|
||||
"extensive_plugin目录下文件夹已存在,删除该目录插件文件夹...", "安装插件", target=name
|
||||
)
|
||||
shutil.rmtree(
|
||||
(extensive_plugin_path / f"{name}").absolute(), ignore_errors=True
|
||||
)
|
||||
extract_path.rename(extensive_plugin_path / f"{name}")
|
||||
tmp = ""
|
||||
prompt = ""
|
||||
if "pyproject.toml" in os.listdir(extensive_plugin_path / f"{name}"):
|
||||
tmp = "检测到该插件含有额外依赖,当前安装无法保证依赖完全安装成功。"
|
||||
prompt = "检测到该插件含有额外依赖,当前安装无法保证依赖完全安装成功。"
|
||||
os.system(
|
||||
f"poetry run pip install -r {(extensive_plugin_path / f'{name}' / 'pyproject.toml').absolute()}"
|
||||
)
|
||||
elif "requirements.txt" in os.listdir(extensive_plugin_path / f"{name}"):
|
||||
tmp = "检测到该插件含有额外依赖,当前安装无法保证依赖完全安装成功。"
|
||||
prompt = "检测到该插件含有额外依赖,当前安装无法保证依赖完全安装成功。"
|
||||
os.system(
|
||||
f"poetry run pip install -r {(extensive_plugin_path / f'{name}' / 'requirements.txt').absolute()}"
|
||||
)
|
||||
with open(extensive_plugin_path / f"{name}" / "plugin_info.json", "w") as f:
|
||||
json.dump(data[name], f, ensure_ascii=False, indent=4)
|
||||
logger.debug("移动插件文件夹完成...")
|
||||
logger.info(f"成功安装插件 {name} 成功!\n{tmp}")
|
||||
return f"成功安装插件 {name},请重启bot!"
|
||||
logger.debug("移动插件文件夹完成...", "安装插件", target=name)
|
||||
logger.info(f"成功安装插件 {name} 成功!\n{prompt}", "安装插件", target=name)
|
||||
return f"成功安装插件 {name},请重启真寻!"
|
||||
except Exception as e:
|
||||
logger.error(f"安装插件 {name} 失败 {type(e)}:{e}")
|
||||
logger.error(f"安装插失败", "安装插件", target=name, e=e)
|
||||
return f"安装插件 {name} 失败 {type(e)}:{e}"
|
||||
|
||||
|
||||
@@ -116,7 +118,8 @@ async def show_plugin_repo() -> Union[int, str]:
|
||||
if code != 200:
|
||||
return code
|
||||
plugin_info = json.load(open(plugin_json, "r", encoding="utf8"))
|
||||
load_plugin_list = plugins_manager.get_data().keys()
|
||||
plugins_data = plugins_manager.get_data()
|
||||
load_plugin_list = plugins_data.keys()
|
||||
image_list = []
|
||||
w, h = 0, 0
|
||||
line_height = 10
|
||||
@@ -130,12 +133,17 @@ async def show_plugin_repo() -> Union[int, str]:
|
||||
"download_url": plugin_info[key]["download_url"],
|
||||
"github_url": plugin_info[key]["github_url"],
|
||||
}
|
||||
status = ""
|
||||
version = ""
|
||||
if key in load_plugin_list:
|
||||
status = "<f font_color=#1a7e30>[已安装]</f>"
|
||||
version = f"<f font_color=#1a7e30>[{plugins_data[key].version}]</f>"
|
||||
s = (
|
||||
f'id:{i+1}\n名称:{plugin_info[key]["plugin_name"]}'
|
||||
f' \t\t{"<f font_color=#1a7e30>[已安装]</f>" if key in load_plugin_list else ""}\n'
|
||||
f" \t\t{status}\n"
|
||||
f"模块:{key}\n"
|
||||
f'作者:{plugin_info[key]["author"]}\n'
|
||||
f'版本:{plugin_info[key]["version"]}\n'
|
||||
f'版本:{plugin_info[key]["version"]} \t\t{version}\n'
|
||||
f'简介:{plugin_info[key]["introduction"]}\n'
|
||||
f"-------------------"
|
||||
)
|
||||
|
||||
+139
-141
@@ -1,20 +1,16 @@
|
||||
from asyncpg.exceptions import (
|
||||
DuplicateColumnError,
|
||||
UndefinedColumnError,
|
||||
PostgresSyntaxError,
|
||||
)
|
||||
from nonebot import Driver
|
||||
from services.db_context import db
|
||||
from models.group_info import GroupInfo
|
||||
from models.bag_user import BagUser
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
from services.log import logger
|
||||
from configs.path_config import TEXT_PATH
|
||||
import random
|
||||
from asyncio.exceptions import TimeoutError
|
||||
from typing import List
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from utils.utils import scheduler
|
||||
|
||||
import nonebot
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
from nonebot.drivers import Driver
|
||||
|
||||
from configs.path_config import TEXT_PATH
|
||||
from models.bag_user import BagUser
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from utils.utils import GDict, scheduler
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
@@ -35,6 +31,7 @@ async def update_city():
|
||||
data = {}
|
||||
if not china_city.exists():
|
||||
try:
|
||||
logger.debug("开始更新城市列表...")
|
||||
res = await AsyncHttpx.get(
|
||||
"http://www.weather.com.cn/data/city3jdata/china.html", timeout=5
|
||||
)
|
||||
@@ -53,103 +50,97 @@ async def update_city():
|
||||
with open(china_city, "w", encoding="utf8") as f:
|
||||
json.dump(data, f, indent=4, ensure_ascii=False)
|
||||
logger.info("自动更新城市列表完成.....")
|
||||
except TimeoutError:
|
||||
logger.warning("自动更新城市列表超时.....")
|
||||
except ValueError:
|
||||
logger.warning("自动城市列表失败.....")
|
||||
except TimeoutError as e:
|
||||
logger.warning("自动更新城市列表超时...", e=e)
|
||||
except ValueError as e:
|
||||
logger.warning("自动城市列表失败.....", e=e)
|
||||
except Exception as e:
|
||||
logger.error(f"自动城市列表未知错误 {type(e)}:{e}")
|
||||
logger.error(f"自动城市列表未知错误", e=e)
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
"""
|
||||
数据库表结构变换
|
||||
"""
|
||||
_flag = []
|
||||
sql_str = [
|
||||
(
|
||||
"ALTER TABLE group_info ADD group_flag Integer NOT NULL DEFAULT 0;",
|
||||
"group_info",
|
||||
), # group_info表添加一个group_flag
|
||||
(
|
||||
"ALTER TABLE bag_users rename belonging_group To group_id;",
|
||||
"bag_users",
|
||||
), # 将 bag_users 的 belonging_group 改为 group_id
|
||||
(
|
||||
"ALTER TABLE group_info_users rename belonging_group To group_id;",
|
||||
"group_info_users",
|
||||
),
|
||||
(
|
||||
"ALTER TABLE sign_group_users rename belonging_group To group_id;",
|
||||
"sign_group_users",
|
||||
),
|
||||
(
|
||||
"ALTER TABLE open_cases_users rename belonging_group To group_id;",
|
||||
"open_cases_users",
|
||||
),
|
||||
(
|
||||
"ALTER TABLE bag_users ADD property json NOT NULL DEFAULT '{}';",
|
||||
"bag_users",
|
||||
), # bag_users 新增字段 property 替代 props
|
||||
(
|
||||
"ALTER TABLE genshin ADD auto_sign_time timestamp with time zone;",
|
||||
"genshin"
|
||||
), # 新增原神自动签到字段
|
||||
(
|
||||
"ALTER TABLE genshin ADD resin_remind boolean DEFAULT False;",
|
||||
"genshin"
|
||||
), # 新增原神自动签到字段
|
||||
(
|
||||
"ALTER TABLE genshin ADD resin_recovery_time timestamp with time zone;",
|
||||
"genshin"
|
||||
), # 新增原神自动签到字段
|
||||
(
|
||||
"ALTER TABLE genshin ADD bind_group Integer;",
|
||||
"genshin"
|
||||
), # 新增原神群号绑定字段
|
||||
(
|
||||
"ALTER TABLE chat_history ADD plain_text Text;",
|
||||
"chat_history"
|
||||
), # 新增纯文本
|
||||
]
|
||||
for sql in sql_str:
|
||||
try:
|
||||
flag = sql[1]
|
||||
sql = sql[0]
|
||||
query = db.text(sql)
|
||||
await db.first(query)
|
||||
logger.info(f"完成sql操作:{sql}")
|
||||
_flag.append(flag)
|
||||
except (DuplicateColumnError, UndefinedColumnError):
|
||||
pass
|
||||
except PostgresSyntaxError:
|
||||
logger.error(f"语法错误:执行sql失败:{sql}")
|
||||
# bag_user 将文本转为字典格式
|
||||
await __database_script(_flag)
|
||||
|
||||
# 完成后
|
||||
end_sql_str = [
|
||||
# "ALTER TABLE bag_users DROP COLUMN props;" # 删除 bag_users 的 props 字段(还不到时候)
|
||||
]
|
||||
for sql in end_sql_str:
|
||||
try:
|
||||
query = db.text(sql)
|
||||
await db.first(query)
|
||||
logger.info(f"完成执行sql操作:{sql}")
|
||||
except (DuplicateColumnError, UndefinedColumnError):
|
||||
pass
|
||||
except PostgresSyntaxError:
|
||||
logger.error(f"语法错误:执行sql失败:{sql}")
|
||||
|
||||
# str2json_sql = ["alter table bag_users alter COLUMN props type json USING props::json;"] # 字段类型替换
|
||||
# rename_sql = 'alter table {} rename {} to {};' # 字段更名
|
||||
# for sql in str2json_sql:
|
||||
# try:
|
||||
# query = db.text(sql)
|
||||
# await db.first(query)
|
||||
# except DuplicateColumnError:
|
||||
# pass
|
||||
# @driver.on_startup
|
||||
# async def _():
|
||||
# """
|
||||
# 数据库表结构变换
|
||||
# """
|
||||
# _flag = []
|
||||
# sql_str = [
|
||||
# (
|
||||
# "ALTER TABLE group_info ADD group_flag Integer NOT NULL DEFAULT 0;",
|
||||
# "group_info",
|
||||
# ), # group_info表添加一个group_flag
|
||||
# (
|
||||
# "ALTER TABLE bag_users rename belonging_group To group_id;",
|
||||
# "bag_users",
|
||||
# ), # 将 bag_users 的 belonging_group 改为 group_id
|
||||
# (
|
||||
# "ALTER TABLE group_info_users rename belonging_group To group_id;",
|
||||
# "group_info_users",
|
||||
# ),
|
||||
# (
|
||||
# "ALTER TABLE sign_group_users rename belonging_group To group_id;",
|
||||
# "sign_group_users",
|
||||
# ),
|
||||
# (
|
||||
# "ALTER TABLE open_cases_users rename belonging_group To group_id;",
|
||||
# "open_cases_users",
|
||||
# ),
|
||||
# (
|
||||
# "ALTER TABLE bag_users ADD property json NOT NULL DEFAULT '{}';",
|
||||
# "bag_users",
|
||||
# ), # bag_users 新增字段 property 替代 props
|
||||
# (
|
||||
# "ALTER TABLE genshin ADD auto_sign_time timestamp with time zone;",
|
||||
# "genshin",
|
||||
# ), # 新增原神自动签到字段
|
||||
# (
|
||||
# "ALTER TABLE genshin ADD resin_remind boolean DEFAULT False;",
|
||||
# "genshin",
|
||||
# ), # 新增原神自动签到字段
|
||||
# (
|
||||
# "ALTER TABLE genshin ADD resin_recovery_time timestamp with time zone;",
|
||||
# "genshin",
|
||||
# ), # 新增原神自动签到字段
|
||||
# ("ALTER TABLE genshin ADD bind_group Integer;", "genshin"), # 新增原神群号绑定字段
|
||||
# (
|
||||
# "ALTER TABLE genshin ADD login_ticket VARCHAR(255) DEFAULT '';",
|
||||
# "genshin",
|
||||
# ), # 新增米游社login_ticket绑定字段
|
||||
# (
|
||||
# "ALTER TABLE genshin ADD stuid VARCHAR(255) DEFAULT '';",
|
||||
# "genshin",
|
||||
# ), # 新增米游社stuid绑定字段
|
||||
# (
|
||||
# "ALTER TABLE genshin ADD stoken VARCHAR(255) DEFAULT '';",
|
||||
# "genshin",
|
||||
# ), # 新增米游社stoken绑定字段
|
||||
# ("ALTER TABLE chat_history ADD plain_text Text;", "chat_history"), # 新增纯文本
|
||||
# (
|
||||
# "ALTER TABLE goods_info ADD daily_limit Integer DEFAULT 0;",
|
||||
# "goods_info",
|
||||
# ), # 新增纯文本
|
||||
# (
|
||||
# "ALTER TABLE goods_info ADD daily_purchase_limit Json DEFAULT '{}';",
|
||||
# "goods_info",
|
||||
# ), # 新增纯文本
|
||||
# ]
|
||||
# for sql in sql_str + GDict.get("run_sql", []):
|
||||
# try:
|
||||
# if isinstance(sql, str):
|
||||
# flag = f"{random.randint(1, 10000)}"
|
||||
# else:
|
||||
# flag = sql[1]
|
||||
# sql = sql[0]
|
||||
# query = db.text(sql)
|
||||
# await db.first(query)
|
||||
# logger.info(f"完成sql操作:{sql}")
|
||||
# _flag.append(flag)
|
||||
# except (DuplicateColumnError, UndefinedColumnError):
|
||||
# pass
|
||||
# except PostgresSyntaxError:
|
||||
# logger.error(f"语法错误:执行sql失败:{sql}")
|
||||
# bag_user 将文本转为字典格式
|
||||
# await __database_script(_flag)
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
@@ -158,51 +149,58 @@ async def _(bot: Bot):
|
||||
版本某些需要的变换
|
||||
"""
|
||||
# 清空不存在的群聊信息,并将已所有已存在的群聊group_flag设置为1(认证所有已存在的群)
|
||||
if not await GroupInfo.get_group_info(114514):
|
||||
if not await GroupInfo.get_or_none(group_id=114514):
|
||||
# 标识符,该功能只需执行一次
|
||||
await GroupInfo.add_group_info(114514, "114514", 114514, 114514, 1)
|
||||
await GroupInfo.create(
|
||||
group_id=114514,
|
||||
group_name="114514",
|
||||
max_member_count=114514,
|
||||
member_count=114514,
|
||||
group_flag=1,
|
||||
)
|
||||
group_list = await bot.get_group_list()
|
||||
group_list = [g["group_id"] for g in group_list]
|
||||
_gl = [x.group_id for x in await GroupInfo.get_all_group()]
|
||||
_gl = [x.group_id for x in await GroupInfo.all()]
|
||||
if 114514 in _gl:
|
||||
_gl.remove(114514)
|
||||
for group_id in _gl:
|
||||
if group_id in group_list:
|
||||
if await GroupInfo.get_group_info(group_id):
|
||||
await GroupInfo.set_group_flag(group_id, 1)
|
||||
if group := await GroupInfo.get_or_none(group_id=group_id):
|
||||
group.group_flag = 1
|
||||
await group.save(update_fields=["group_flag"])
|
||||
else:
|
||||
group_info = await bot.get_group_info(group_id=group_id)
|
||||
await GroupInfo.add_group_info(
|
||||
group_info["group_id"],
|
||||
group_info["group_name"],
|
||||
group_info["max_member_count"],
|
||||
group_info["member_count"],
|
||||
1,
|
||||
await GroupInfo.create(
|
||||
group_id=group_info["group_id"],
|
||||
group_name=group_info["group_name"],
|
||||
max_member_count=group_info["max_member_count"],
|
||||
member_count=group_info["member_count"],
|
||||
group_flag=1,
|
||||
)
|
||||
logger.info(f"已将群聊 {group_id} 添加认证...")
|
||||
logger.info(f"已添加群认证...", group_id=group_id)
|
||||
else:
|
||||
await GroupInfo.delete_group_info(group_id)
|
||||
logger.info(f"移除不存在的群聊信息:{group_id}")
|
||||
await GroupInfo.filter(group_id=group_id).delete()
|
||||
logger.info(f"移除不存在的群聊信息", group_id=group_id)
|
||||
|
||||
|
||||
async def __database_script(_flag: List[str]):
|
||||
# bag_user 将文本转为字典格式
|
||||
if "bag_users" in _flag:
|
||||
for x in await BagUser.get_all_users():
|
||||
props = {}
|
||||
if x.props:
|
||||
for prop in [p for p in x.props.split(",") if p]:
|
||||
if props.get(prop):
|
||||
props[prop] += 1
|
||||
else:
|
||||
props[prop] = 1
|
||||
logger.info(
|
||||
f"__database_script USER {x.user_qq} GROUP {x.group_id} 更新数据 {props}"
|
||||
)
|
||||
await x.update(
|
||||
property=props,
|
||||
props="",
|
||||
).apply()
|
||||
# async def __database_script(_flag: List[str]):
|
||||
# # bag_user 将文本转为字典格式
|
||||
# if "bag_users" in _flag:
|
||||
# for x in await BagUser.get_all_users():
|
||||
# props = {}
|
||||
# if x.props:
|
||||
# for prop in [p for p in x.props.split(",") if p]:
|
||||
# if props.get(prop):
|
||||
# props[prop] += 1
|
||||
# else:
|
||||
# props[prop] = 1
|
||||
# logger.info(
|
||||
# f"__database_script USER {x.user_qq} GROUP {x.group_id} 更新数据 {props}"
|
||||
# )
|
||||
# await x.update(
|
||||
# property=props,
|
||||
# props="",
|
||||
# ).apply()
|
||||
|
||||
|
||||
# 自动更新城市列表
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
from nonebot.drivers import Driver
|
||||
|
||||
from configs.config import Config
|
||||
from utils.decorator.shop import shop_register
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -7,8 +14,14 @@ Config.add_plugin_config(
|
||||
"IMPORT_DEFAULT_SHOP_GOODS",
|
||||
True,
|
||||
help_="导入商店自带的三个商品",
|
||||
default_value=True
|
||||
default_value=True,
|
||||
type=bool,
|
||||
)
|
||||
|
||||
|
||||
nonebot.load_plugins("basic_plugins/shop")
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def _():
|
||||
await shop_register.load_register()
|
||||
|
||||
+48
-33
@@ -1,14 +1,15 @@
|
||||
from nonebot import on_command
|
||||
from services.log import logger
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message
|
||||
from nonebot.params import CommandArg
|
||||
from utils.utils import is_number
|
||||
from models.bag_user import BagUser
|
||||
from services.db_context import db
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from models.goods_info import GoodsInfo
|
||||
import time
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from nonebot.params import CommandArg
|
||||
|
||||
from models.bag_user import BagUser
|
||||
from models.goods_info import GoodsInfo
|
||||
from models.user_shop_gold_log import UserShopGoldLog
|
||||
from services.log import logger
|
||||
from utils.utils import is_number
|
||||
|
||||
__zx_plugin_name__ = "商店 - 购买道具"
|
||||
__plugin_usage__ = """
|
||||
@@ -46,10 +47,7 @@ async def _(event: GroupMessageEvent, arg: Message = CommandArg()):
|
||||
for x in await GoodsInfo.get_all_goods()
|
||||
if x.goods_limit_time > time.time() or x.goods_limit_time == 0
|
||||
]
|
||||
goods_name_list = [
|
||||
x.goods_name
|
||||
for x in goods_list
|
||||
]
|
||||
goods_name_list = [x.goods_name for x in goods_list]
|
||||
msg = arg.extract_plain_text().strip().split()
|
||||
num = 1
|
||||
if len(msg) > 1:
|
||||
@@ -72,23 +70,40 @@ async def _(event: GroupMessageEvent, arg: Message = CommandArg()):
|
||||
await buy.finish("请输入正确的商品名称!")
|
||||
else:
|
||||
await buy.finish("请输入正确的商品名称!", at_sender=True)
|
||||
async with db.transaction():
|
||||
if (
|
||||
await BagUser.get_gold(event.user_id, event.group_id)
|
||||
) < goods.goods_price * num * goods.goods_discount:
|
||||
await buy.finish("您的金币好像不太够哦", at_sender=True)
|
||||
if await BagUser.buy_property(event.user_id, event.group_id, goods, num):
|
||||
await buy.send(
|
||||
f"花费 {goods.goods_price * num * goods.goods_discount} 金币购买 {goods.goods_name} ×{num} 成功!",
|
||||
at_sender=True,
|
||||
)
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP {event.group_id} "
|
||||
f"花费 {goods.goods_price*num} 金币购买 {goods.goods_name} ×{num} 成功!"
|
||||
)
|
||||
else:
|
||||
await buy.send(f"{goods.goods_name} 购买失败!", at_sender=True)
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP {event.group_id} "
|
||||
f"花费 {goods.goods_price * num * goods.goods_discount} 金币购买 {goods.goods_name} ×{num} 失败!"
|
||||
)
|
||||
if (
|
||||
await BagUser.get_gold(event.user_id, event.group_id)
|
||||
) < goods.goods_price * num * goods.goods_discount:
|
||||
await buy.finish("您的金币好像不太够哦", at_sender=True)
|
||||
flag, n = await GoodsInfo.check_user_daily_purchase(
|
||||
goods, event.user_id, event.group_id, num
|
||||
)
|
||||
if flag:
|
||||
await buy.finish(f"该次购买将超过每日次数限制,目前该道具还可以购买{n}次哦", at_sender=True)
|
||||
spend_gold = int(goods.goods_discount * goods.goods_price * num)
|
||||
await BagUser.spend_gold(event.user_id, event.group_id, spend_gold)
|
||||
await BagUser.add_property(event.user_id, event.group_id, goods.goods_name, num)
|
||||
await GoodsInfo.add_user_daily_purchase(goods, event.user_id, event.group_id, num)
|
||||
await buy.send(
|
||||
f"花费 {goods.goods_price * num * goods.goods_discount} 金币购买 {goods.goods_name} ×{num} 成功!",
|
||||
at_sender=True,
|
||||
)
|
||||
logger.info(
|
||||
f"花费 {goods.goods_price*num} 金币购买 {goods.goods_name} ×{num} 成功!",
|
||||
"购买道具",
|
||||
event.user_id,
|
||||
event.group_id,
|
||||
)
|
||||
await UserShopGoldLog.create(
|
||||
user_qq=event.user_id,
|
||||
group_id=event.group_id,
|
||||
type=0,
|
||||
name=goods.goods_name,
|
||||
num=num,
|
||||
spend_gold=goods.goods_price * num * goods.goods_discount,
|
||||
)
|
||||
# else:
|
||||
# await buy.send(f"{goods.goods_name} 购买失败!", at_sender=True)
|
||||
# logger.info(
|
||||
# f"USER {event.user_id} GROUP {event.group_id} "
|
||||
# f"花费 {goods.goods_price * num * goods.goods_discount} 金币购买 {goods.goods_name} ×{num} 失败!"
|
||||
# )
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message, ActionFailed
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.adapters.onebot.v11 import ActionFailed, GroupMessageEvent, Message
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from utils.data_utils import init_rank
|
||||
from nonebot.params import CommandArg
|
||||
|
||||
from models.bag_user import BagUser
|
||||
from utils.data_utils import init_rank
|
||||
from utils.image_utils import text2image
|
||||
from utils.message_builder import image
|
||||
from utils.utils import is_number
|
||||
@@ -39,7 +40,9 @@ async def _(event: GroupMessageEvent):
|
||||
try:
|
||||
await my_gold.send(msg)
|
||||
except ActionFailed:
|
||||
await my_gold.send(image(b64=(await text2image(msg, color="#f9f6f2", padding=10)).pic2bs4()))
|
||||
await my_gold.send(
|
||||
image(b64=(await text2image(msg, color="#f9f6f2", padding=10)).pic2bs4())
|
||||
)
|
||||
|
||||
|
||||
@gold_rank.handle()
|
||||
@@ -49,9 +52,11 @@ async def _(event: GroupMessageEvent, arg: Message = CommandArg()):
|
||||
num = int(num)
|
||||
else:
|
||||
num = 10
|
||||
all_users = await BagUser.get_all_users(event.group_id)
|
||||
all_users = await BagUser.filter(group_id=event.group_id)
|
||||
all_user_id = [user.user_qq for user in all_users]
|
||||
all_user_data = [user.gold for user in all_users]
|
||||
rank_image = await init_rank("金币排行", all_user_id, all_user_data, event.group_id, num)
|
||||
rank_image = await init_rank(
|
||||
"金币排行", all_user_id, all_user_data, event.group_id, num
|
||||
)
|
||||
if rank_image:
|
||||
await gold_rank.finish(image(b64=rank_image.pic2bs4()))
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
from nonebot import on_command
|
||||
from services.log import logger
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent
|
||||
from models.bag_user import BagUser
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
|
||||
from models.bag_user import BagUser
|
||||
from services.log import logger
|
||||
from utils.message_builder import image
|
||||
|
||||
from ._data_source import create_bag_image
|
||||
|
||||
__zx_plugin_name__ = "商店 - 我的道具"
|
||||
__plugin_usage__ = """
|
||||
@@ -14,7 +17,7 @@ usage:
|
||||
""".strip()
|
||||
__plugin_des__ = "商店 - 我的道具"
|
||||
__plugin_cmd__ = ["我的道具"]
|
||||
__plugin_type__ = ('商店',)
|
||||
__plugin_type__ = ("商店",)
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
@@ -32,10 +35,7 @@ my_props = on_command("我的道具", priority=5, block=True, permission=GROUP)
|
||||
async def _(event: GroupMessageEvent):
|
||||
props = await BagUser.get_property(event.user_id, event.group_id)
|
||||
if props:
|
||||
rst = ""
|
||||
for i, p in enumerate(props.keys()):
|
||||
rst += f"{i+1}.{p}\t×{props[p]}\n"
|
||||
await my_props.send("\n" + rst[:-1], at_sender=True)
|
||||
await my_props.send(image(b64=await create_bag_image(props)))
|
||||
logger.info(f"USER {event.user_id} GROUP {event.group_id} 查看我的道具")
|
||||
else:
|
||||
await my_props.finish("您的背包里没有任何的道具噢~", at_sender=True)
|
||||
@@ -0,0 +1,121 @@
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from models.bag_user import BagUser
|
||||
from models.goods_info import GoodsInfo
|
||||
from utils.image_utils import BuildImage
|
||||
from configs.path_config import IMAGE_PATH
|
||||
|
||||
|
||||
icon_path = IMAGE_PATH / "shop_icon"
|
||||
|
||||
|
||||
async def create_bag_image(props: Dict[str, int]):
|
||||
"""
|
||||
说明:
|
||||
创建背包道具图片
|
||||
参数:
|
||||
:param props: 道具仓库字典
|
||||
"""
|
||||
goods_list = await GoodsInfo.get_all_goods()
|
||||
active_props = await _init_prop(props, [x for x in goods_list if not x.is_passive])
|
||||
passive_props = await _init_prop(props, [x for x in goods_list if x.is_passive])
|
||||
active_w = 0
|
||||
active_h = 0
|
||||
passive_w = 0
|
||||
passive_h = 0
|
||||
if active_props:
|
||||
img = BuildImage(
|
||||
active_props.w,
|
||||
active_props.h + 70,
|
||||
font="CJGaoDeGuo.otf",
|
||||
font_size=30,
|
||||
color="#f9f6f2",
|
||||
)
|
||||
await img.apaste(active_props, (0, 70))
|
||||
await img.atext((0, 30), "主动道具")
|
||||
active_props = img
|
||||
active_w = img.w
|
||||
active_h = img.h
|
||||
if passive_props:
|
||||
img = BuildImage(
|
||||
passive_props.w,
|
||||
passive_props.h + 70,
|
||||
font="CJGaoDeGuo.otf",
|
||||
font_size=30,
|
||||
color="#f9f6f2",
|
||||
)
|
||||
await img.apaste(passive_props, (0, 70))
|
||||
await img.atext((0, 30), "被动道具")
|
||||
passive_props = img
|
||||
passive_w = img.w
|
||||
passive_h = img.h
|
||||
A = BuildImage(
|
||||
active_w + passive_w + 100,
|
||||
max(active_h, passive_h) + 60,
|
||||
font="CJGaoDeGuo.otf",
|
||||
font_size=30,
|
||||
color="#f9f6f2",
|
||||
)
|
||||
curr_w = 50
|
||||
if active_props:
|
||||
await A.apaste(active_props, (curr_w, 0))
|
||||
curr_w += active_props.w + 10
|
||||
if passive_props:
|
||||
await A.apaste(passive_props, (curr_w, 0))
|
||||
if active_props and passive_props:
|
||||
await A.aline(
|
||||
(active_props.w + 45, 70, active_props.w + 45, A.h - 20), fill=(0, 0, 0)
|
||||
)
|
||||
return A.pic2bs4()
|
||||
|
||||
|
||||
async def _init_prop(
|
||||
props: Dict[str, int], _props: List[GoodsInfo]
|
||||
) -> Optional[BuildImage]:
|
||||
"""
|
||||
说明:
|
||||
构造道具列表图片
|
||||
参数:
|
||||
:param props: 道具仓库字典
|
||||
:param _props: 道具列表
|
||||
"""
|
||||
active_name = [x.goods_name for x in _props]
|
||||
name_list = [x for x in props.keys() if x in active_name]
|
||||
if not name_list:
|
||||
return None
|
||||
temp_img = BuildImage(0, 0, font_size=20)
|
||||
image_list = []
|
||||
num_list = []
|
||||
for i, name in enumerate(name_list):
|
||||
img = BuildImage(
|
||||
temp_img.getsize(name)[0] + 50,
|
||||
30,
|
||||
font="msyh.ttf",
|
||||
font_size=20,
|
||||
color="#f9f6f2",
|
||||
)
|
||||
await img.atext((30, 5), f"{i + 1}.{name}")
|
||||
goods = [x for x in _props if x.goods_name == name][0]
|
||||
if goods.icon and (icon_path / goods.icon).exists():
|
||||
icon = BuildImage(30, 30, background=icon_path / goods.icon)
|
||||
await img.apaste(icon, alpha=True)
|
||||
image_list.append(img)
|
||||
num_list.append(
|
||||
BuildImage(
|
||||
30, 30, font_size=20, font="msyh.ttf", plain_text=f"×{props[name]}"
|
||||
)
|
||||
)
|
||||
max_w = 0
|
||||
num_max_w = 0
|
||||
h = 0
|
||||
for img, num in zip(image_list, num_list):
|
||||
h += img.h
|
||||
max_w = max_w if max_w > img.w else img.w
|
||||
num_max_w = num_max_w if num_max_w > num.w else num.w
|
||||
A = BuildImage(max_w + num_max_w + 30, h, color="#f9f6f2")
|
||||
curr_h = 0
|
||||
for img, num in zip(image_list, num_list):
|
||||
await A.apaste(img, (0, curr_h))
|
||||
await A.apaste(num, (max_w + 20, curr_h + 5), True)
|
||||
curr_h += img.h
|
||||
return A
|
||||
@@ -1,26 +0,0 @@
|
||||
from utils.utils import scheduler
|
||||
from models.bag_user import BagUser
|
||||
from services.log import logger
|
||||
|
||||
|
||||
__zx_plugin_name__ = "每日金币重置 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
|
||||
# 重置每日金币
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=0,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
user_list = await BagUser.get_all_users()
|
||||
for user in user_list:
|
||||
await user.update(
|
||||
get_today_gold=0,
|
||||
spend_today_gold=0,
|
||||
).apply()
|
||||
except Exception as e:
|
||||
logger.error(f"重置每日金币错误 e:{e}")
|
||||
@@ -1,15 +1,24 @@
|
||||
from .data_source import create_shop_help, delete_goods, update_goods, register_goods, parse_goods_info
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent, Message
|
||||
from nonebot import on_command
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from utils.message_builder import image
|
||||
from nonebot.permission import SUPERUSER
|
||||
from utils.utils import is_number
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.plugin import export
|
||||
from services.log import logger
|
||||
import os
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Message, MessageEvent
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from models.bag_user import BagUser
|
||||
from services.log import logger
|
||||
from utils.message_builder import image
|
||||
from utils.utils import is_number, scheduler
|
||||
|
||||
from .data_source import (
|
||||
GoodsInfo,
|
||||
create_shop_help,
|
||||
delete_goods,
|
||||
parse_goods_info,
|
||||
register_goods,
|
||||
update_goods,
|
||||
)
|
||||
|
||||
__zx_plugin_name__ = "商店"
|
||||
__plugin_usage__ = """
|
||||
@@ -38,7 +47,7 @@ __plugin_cmd__ = [
|
||||
"删除商品 [名称或序号] [_superuser]",
|
||||
"修改商品 name:[名称或序号] price:[价格] des:[描述] discount:[折扣] limit_time:[限时] [_superuser]",
|
||||
]
|
||||
__plugin_type__ = ('商店',)
|
||||
__plugin_type__ = ("商店",)
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
@@ -47,15 +56,8 @@ __plugin_settings__ = {
|
||||
"limit_superuser": False,
|
||||
"cmd": ["商店"],
|
||||
}
|
||||
__plugin_block_limit__ = {
|
||||
"limit_type": "group"
|
||||
}
|
||||
__plugin_block_limit__ = {"limit_type": "group"}
|
||||
|
||||
# 导出方法供其他插件使用
|
||||
export = export()
|
||||
export.register_goods = register_goods
|
||||
export.delete_goods = delete_goods
|
||||
export.update_goods = update_goods
|
||||
|
||||
shop_help = on_command("商店", priority=5, block=True)
|
||||
|
||||
@@ -81,12 +83,15 @@ async def _(event: MessageEvent, arg: Message = CommandArg()):
|
||||
if not data.get("name") or not data.get("price") or not data.get("des"):
|
||||
await shop_add_goods.finish("name:price:des 参数不可缺少!")
|
||||
if await register_goods(**data):
|
||||
await shop_add_goods.send(f"添加商品 {data['name']} 成功!\n"
|
||||
f"名称:{data['name']}\n"
|
||||
f"价格:{data['price']}金币\n"
|
||||
f"简介:{data['des']}\n"
|
||||
f"折扣:{data.get('discount')}\n"
|
||||
f"限时:{data.get('limit_time')}", at_sender=True)
|
||||
await shop_add_goods.send(
|
||||
f"添加商品 {data['name']} 成功!\n"
|
||||
f"名称:{data['name']}\n"
|
||||
f"价格:{data['price']}金币\n"
|
||||
f"简介:{data['des']}\n"
|
||||
f"折扣:{data.get('discount')}\n"
|
||||
f"限时:{data.get('limit_time')}",
|
||||
at_sender=True,
|
||||
)
|
||||
logger.info(f"USER {event.user_id} 添加商品 {msg} 成功")
|
||||
else:
|
||||
await shop_add_goods.send(f"添加商品 {msg} 失败了...", at_sender=True)
|
||||
@@ -128,6 +133,30 @@ async def _(event: MessageEvent, arg: Message = CommandArg()):
|
||||
await shop_update_goods.send(f"修改商品 {name} 成功了...\n{text}", at_sender=True)
|
||||
logger.info(f"USER {event.user_id} 修改商品 {name} 数据 {text} 成功")
|
||||
else:
|
||||
await shop_update_goods.send(f"修改商品 {name} 失败了...", at_sender=True)
|
||||
await shop_update_goods.send(name, at_sender=True)
|
||||
logger.info(f"USER {event.user_id} 修改商品 {name} 数据 {text} 失败")
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=0,
|
||||
minute=0,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
await GoodsInfo.all().update(daily_purchase_limit={})
|
||||
logger.info("商品每日限购次数重置成功...")
|
||||
except Exception as e:
|
||||
logger.error(f"商品每日限购次数重置出错 {type(e)}:{e}")
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=0,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
try:
|
||||
await BagUser.all().update(get_today_gold=0, spend_today_gold=0)
|
||||
except Exception as e:
|
||||
logger.error(f"重置每日金币", "定时任务", e=e)
|
||||
|
||||
@@ -1,47 +1,18 @@
|
||||
from models.goods_info import GoodsInfo
|
||||
from utils.image_utils import BuildImage
|
||||
from models.sign_group_user import SignGroupUser
|
||||
from utils.utils import is_number
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from typing import Optional, Union
|
||||
from configs.config import Config
|
||||
from nonebot import Driver
|
||||
from nonebot.plugin import require
|
||||
from utils.decorator.shop import shop_register
|
||||
import nonebot
|
||||
import time
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
from PIL import Image
|
||||
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from models.goods_info import GoodsInfo
|
||||
from utils.image_utils import BuildImage, text2image
|
||||
from utils.utils import GDict, is_number
|
||||
|
||||
icon_path = IMAGE_PATH / "shop_icon"
|
||||
|
||||
|
||||
use = require("use")
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def init_default_shop_goods():
|
||||
"""
|
||||
导入内置的三个商品
|
||||
"""
|
||||
|
||||
@shop_register(
|
||||
name=("好感度双倍加持卡Ⅰ", "好感度双倍加持卡Ⅱ", "好感度双倍加持卡Ⅲ"),
|
||||
price=(30, 150, 250),
|
||||
des=(
|
||||
"下次签到双倍好感度概率 + 10%(谁才是真命天子?)(同类商品将覆盖)",
|
||||
"下次签到双倍好感度概率 + 20%(平平庸庸)(同类商品将覆盖)",
|
||||
"下次签到双倍好感度概率 + 30%(金币才是真命天子!)(同类商品将覆盖)",
|
||||
),
|
||||
load_status=Config.get_config("shop", "IMPORT_DEFAULT_SHOP_GOODS"),
|
||||
** {"好感度双倍加持卡Ⅰ_prob": 0.1, "好感度双倍加持卡Ⅱ_prob": 0.2, "好感度双倍加持卡Ⅲ_prob": 0.3},
|
||||
)
|
||||
async def sign_card(user_id: int, group_id: int, prob: float):
|
||||
user = await SignGroupUser.ensure(user_id, group_id)
|
||||
await user.update(add_probability=prob).apply()
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def _():
|
||||
await shop_register.load_register()
|
||||
GDict["run_sql"].append("ALTER TABLE goods_info ADD is_passive boolean DEFAULT False;")
|
||||
GDict["run_sql"].append("ALTER TABLE goods_info ADD icon VARCHAR(255);")
|
||||
|
||||
|
||||
# 创建商店界面
|
||||
@@ -51,56 +22,124 @@ async def create_shop_help() -> str:
|
||||
:return: 图片base64
|
||||
"""
|
||||
goods_lst = await GoodsInfo.get_all_goods()
|
||||
idx = 1
|
||||
_dc = {}
|
||||
font_h = BuildImage(0, 0).getsize("正")[1]
|
||||
h = 10
|
||||
_list = []
|
||||
for goods in goods_lst:
|
||||
if goods.goods_limit_time == 0 or time.time() < goods.goods_limit_time:
|
||||
h += len(goods.goods_description.strip().split("\n")) * font_h + 80
|
||||
_list.append(goods)
|
||||
A = BuildImage(1000, h, color="#f9f6f2")
|
||||
current_h = 0
|
||||
for goods in _list:
|
||||
bk = BuildImage(700, 80, font_size=15, color="#f9f6f2", font="CJGaoDeGuo.otf")
|
||||
goods_image = BuildImage(
|
||||
600, 80, font_size=20, color="#a29ad6", font="CJGaoDeGuo.otf"
|
||||
)
|
||||
# A = BuildImage(1100, h, color="#f9f6f2")
|
||||
total_n = 0
|
||||
image_list = []
|
||||
for idx, goods in enumerate(_list):
|
||||
name_image = BuildImage(
|
||||
580, 40, font_size=25, color="#e67b6b", font="CJGaoDeGuo.otf"
|
||||
)
|
||||
await name_image.atext(
|
||||
(15, 0), f"{idx}.{goods.goods_name}", center_type="by_height"
|
||||
(15, 0), f"{idx + 1}.{goods.goods_name}", center_type="by_height"
|
||||
)
|
||||
await name_image.aline((380, -5, 280, 45), "#a29ad6", 5)
|
||||
await name_image.atext((390, 0), "售价:", center_type="by_height")
|
||||
await name_image.atext(
|
||||
(440, 0), str(goods.goods_price), (255, 255, 255), center_type="by_height"
|
||||
)
|
||||
if goods.goods_discount != 1:
|
||||
discount_price = int(goods.goods_discount * goods.goods_price)
|
||||
old_price_image = BuildImage(
|
||||
0,
|
||||
0,
|
||||
plain_text=str(goods.goods_price),
|
||||
font_color=(194, 194, 194),
|
||||
font="CJGaoDeGuo.otf",
|
||||
font_size=15,
|
||||
)
|
||||
await old_price_image.aline(
|
||||
(
|
||||
0,
|
||||
int(old_price_image.h / 2),
|
||||
old_price_image.w + 1,
|
||||
int(old_price_image.h / 2),
|
||||
),
|
||||
(0, 0, 0),
|
||||
)
|
||||
await name_image.apaste(old_price_image, (440, 0), True)
|
||||
await name_image.atext((440, 15), str(discount_price), (255, 255, 255))
|
||||
else:
|
||||
await name_image.atext(
|
||||
(440, 0),
|
||||
str(goods.goods_price),
|
||||
(255, 255, 255),
|
||||
center_type="by_height",
|
||||
)
|
||||
await name_image.atext(
|
||||
(
|
||||
440
|
||||
+ BuildImage(0, 0, plain_text=str(goods.goods_price), font_size=25).w,
|
||||
0,
|
||||
),
|
||||
" 金币",
|
||||
f" 金币",
|
||||
center_type="by_height",
|
||||
)
|
||||
des_image = None
|
||||
font_img = BuildImage(
|
||||
600, 80, font_size=20, color="#a29ad6", font="CJGaoDeGuo.otf"
|
||||
)
|
||||
p = font_img.getsize("简介:")[0] + 20
|
||||
if goods.goods_description:
|
||||
des_list = goods.goods_description.split("\n")
|
||||
desc = ""
|
||||
for des in des_list:
|
||||
if font_img.getsize(des)[0] > font_img.w - p - 20:
|
||||
msg = ""
|
||||
tmp = ""
|
||||
for i in range(len(des)):
|
||||
if font_img.getsize(tmp)[0] < font_img.w - p - 20:
|
||||
tmp += des[i]
|
||||
else:
|
||||
msg += tmp + "\n"
|
||||
tmp = des[i]
|
||||
desc += msg
|
||||
if tmp:
|
||||
desc += tmp
|
||||
else:
|
||||
desc += des + "\n"
|
||||
if desc[-1] == "\n":
|
||||
desc = desc[:-1]
|
||||
des_image = await text2image(desc, color="#a29ad6")
|
||||
goods_image = BuildImage(
|
||||
600,
|
||||
(50 + des_image.h) if des_image else 50,
|
||||
font_size=20,
|
||||
color="#a29ad6",
|
||||
font="CJGaoDeGuo.otf",
|
||||
)
|
||||
if des_image:
|
||||
await goods_image.atext((15, 50), "简介:")
|
||||
await goods_image.apaste(des_image, (p, 50))
|
||||
await name_image.acircle_corner(5)
|
||||
await goods_image.apaste(name_image, (0, 5), True, center_type="by_width")
|
||||
await goods_image.atext((15, 50), f"简介:{goods.goods_description}")
|
||||
await goods_image.acircle_corner(20)
|
||||
await bk.apaste(goods_image, alpha=True)
|
||||
bk = BuildImage(
|
||||
1180,
|
||||
(50 + des_image.h) if des_image else 50,
|
||||
font_size=15,
|
||||
color="#f9f6f2",
|
||||
font="CJGaoDeGuo.otf",
|
||||
)
|
||||
if goods.icon and (icon_path / goods.icon).exists():
|
||||
icon = BuildImage(70, 70, background=icon_path / goods.icon)
|
||||
await bk.apaste(icon)
|
||||
await bk.apaste(goods_image, (70, 0), alpha=True)
|
||||
n = 0
|
||||
_w = 650
|
||||
# 添加限时图标和时间
|
||||
if goods.goods_limit_time > 0:
|
||||
n += 140
|
||||
_limit_time_logo = BuildImage(
|
||||
40, 40, background=f"{IMAGE_PATH}/other/time.png"
|
||||
)
|
||||
await bk.apaste(_limit_time_logo, (600, 0), True)
|
||||
await bk.apaste(_limit_time_logo, (_w + 50, 0), True)
|
||||
await bk.apaste(
|
||||
BuildImage(0, 0, plain_text="限时!", font_size=23, font="CJGaoDeGuo.otf"),
|
||||
(640, 10),
|
||||
(_w + 90, 10),
|
||||
True,
|
||||
)
|
||||
limit_time = time.strftime(
|
||||
@@ -109,28 +148,91 @@ async def create_shop_help() -> str:
|
||||
y_m_d = limit_time[0]
|
||||
_h_m = limit_time[1].split(":")
|
||||
h_m = _h_m[0] + "时 " + _h_m[1] + "分"
|
||||
await bk.atext((605, 38), str(y_m_d))
|
||||
await bk.atext((615, 57), str(h_m))
|
||||
await bk.aline((550, -1, 710, -1), "#a29ad6", 5)
|
||||
await bk.aline((550, 80, 710, 80), "#a29ad6", 5)
|
||||
idx += 1
|
||||
await A.apaste(bk, (0, current_h), True)
|
||||
current_h += 90
|
||||
w = 1000
|
||||
await bk.atext((_w + 55, 38), str(y_m_d))
|
||||
await bk.atext((_w + 65, 57), str(h_m))
|
||||
_w += 140
|
||||
if goods.goods_discount != 1:
|
||||
n += 140
|
||||
_discount_logo = BuildImage(
|
||||
30, 30, background=f"{IMAGE_PATH}/other/discount.png"
|
||||
)
|
||||
await bk.apaste(_discount_logo, (_w + 50, 10), True)
|
||||
await bk.apaste(
|
||||
BuildImage(0, 0, plain_text="折扣!", font_size=23, font="CJGaoDeGuo.otf"),
|
||||
(_w + 90, 15),
|
||||
True,
|
||||
)
|
||||
await bk.apaste(
|
||||
BuildImage(
|
||||
0,
|
||||
0,
|
||||
plain_text=f"{10 * goods.goods_discount:.1f} 折",
|
||||
font_size=30,
|
||||
font="CJGaoDeGuo.otf",
|
||||
font_color=(85, 156, 75),
|
||||
),
|
||||
(_w + 50, 44),
|
||||
True,
|
||||
)
|
||||
_w += 140
|
||||
if goods.daily_limit != 0:
|
||||
n += 140
|
||||
_daily_limit_logo = BuildImage(
|
||||
35, 35, background=f"{IMAGE_PATH}/other/daily_limit.png"
|
||||
)
|
||||
await bk.apaste(_daily_limit_logo, (_w + 50, 10), True)
|
||||
await bk.apaste(
|
||||
BuildImage(0, 0, plain_text="限购!", font_size=23, font="CJGaoDeGuo.otf"),
|
||||
(_w + 90, 20),
|
||||
True,
|
||||
)
|
||||
await bk.apaste(
|
||||
BuildImage(
|
||||
0,
|
||||
0,
|
||||
plain_text=f"{goods.daily_limit}",
|
||||
font_size=30,
|
||||
font="CJGaoDeGuo.otf",
|
||||
),
|
||||
(_w + 72, 45),
|
||||
True,
|
||||
)
|
||||
if total_n < n:
|
||||
total_n = n
|
||||
if n:
|
||||
await bk.aline((650, -1, 650 + n, -1), "#a29ad6", 5)
|
||||
# await bk.aline((650, 80, 650 + n, 80), "#a29ad6", 5)
|
||||
|
||||
# 添加限时图标和时间
|
||||
image_list.append(bk)
|
||||
# await A.apaste(bk, (0, current_h), True)
|
||||
# current_h += 90
|
||||
h = 0
|
||||
current_h = 0
|
||||
for img in image_list:
|
||||
h += img.h + 10
|
||||
A = BuildImage(1100, h, color="#f9f6f2")
|
||||
for img in image_list:
|
||||
await A.apaste(img, (0, current_h), True)
|
||||
current_h += img.h + 10
|
||||
w = 950
|
||||
if total_n:
|
||||
w += total_n
|
||||
h = A.h + 230 + 100
|
||||
h = 1000 if h < 1000 else h
|
||||
shop_logo = BuildImage(100, 100, background=f"{IMAGE_PATH}/other/shop_text.png")
|
||||
shop = BuildImage(w, h, font_size=20, color="#f9f6f2")
|
||||
shop.paste(A, (20, 230))
|
||||
zx_img = BuildImage(0, 0, background=f"{IMAGE_PATH}/zhenxun/toukan.png")
|
||||
zx_img = BuildImage(0, 0, background=f"{IMAGE_PATH}/zhenxun/toukan_3.png")
|
||||
zx_img.transpose(Image.FLIP_LEFT_RIGHT)
|
||||
zx_img.replace_color_tran(((240, 240, 240), (255, 255, 255)), (249, 246, 242))
|
||||
await shop.apaste(zx_img, (780, 100))
|
||||
await shop.apaste(zx_img, (0, 100))
|
||||
shop.paste(A, (20 + zx_img.w, 230))
|
||||
await shop.apaste(shop_logo, (450, 30), True)
|
||||
shop.text(
|
||||
(int((1000 - shop.getsize("注【通过 序号 或者 商品名称 购买】")[0]) / 2), 170),
|
||||
"注【通过 序号 或者 商品名称 购买】",
|
||||
)
|
||||
shop.text((20, h - 100), "神秘药水\t\t售价:9999999金币\n\t\t鬼知道会有什么效果~")
|
||||
shop.text((20 + zx_img.w, h - 100), "神秘药水\t\t售价:9999999金币\n\t\t鬼知道会有什么效果~")
|
||||
return shop.pic2bs4()
|
||||
|
||||
|
||||
@@ -140,6 +242,9 @@ async def register_goods(
|
||||
des: str,
|
||||
discount: Optional[float] = 1,
|
||||
limit_time: Optional[int] = 0,
|
||||
daily_limit: Optional[int] = 0,
|
||||
is_passive: Optional[bool] = False,
|
||||
icon: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
添加商品
|
||||
@@ -151,24 +256,35 @@ async def register_goods(
|
||||
:param des: 商品简介
|
||||
:param discount: 商品折扣
|
||||
:param limit_time: 商品限时销售时间,单位为小时
|
||||
:param daily_limit: 每日购买次数限制
|
||||
:param is_passive: 是否为被动
|
||||
:param icon: 图标
|
||||
:return: 是否添加成功
|
||||
"""
|
||||
if not await GoodsInfo.get_goods_info(name):
|
||||
limit_time = float(limit_time) if limit_time else limit_time
|
||||
if not await GoodsInfo.get_or_none(goods_name=name):
|
||||
limit_time_ = float(limit_time) if limit_time else limit_time
|
||||
discount = discount if discount is not None else 1
|
||||
limit_time = (
|
||||
int(time.time() + limit_time * 60 * 60)
|
||||
if limit_time is not None and limit_time != 0
|
||||
limit_time_ = (
|
||||
int(time.time() + limit_time_ * 60 * 60)
|
||||
if limit_time_ is not None and limit_time_ != 0
|
||||
else 0
|
||||
)
|
||||
return await GoodsInfo.add_goods(
|
||||
name, int(price), des, float(discount), limit_time
|
||||
await GoodsInfo.create(
|
||||
goods_name=name,
|
||||
goods_price=int(price),
|
||||
goods_description=des,
|
||||
goods_discount=float(discount),
|
||||
goods_limit_time=limit_time_,
|
||||
daily_limit=daily_limit,
|
||||
is_passive=is_passive,
|
||||
icon=icon,
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# 删除商品
|
||||
async def delete_goods(name: str, id_: int) -> "str, str, int":
|
||||
async def delete_goods(name: str, id_: int) -> Tuple[str, str, int]:
|
||||
"""
|
||||
删除商品
|
||||
:param name: 商品名称
|
||||
@@ -189,10 +305,11 @@ async def delete_goods(name: str, id_: int) -> "str, str, int":
|
||||
return f"删除商品 {name} 成功!", name, 200
|
||||
else:
|
||||
return f"删除商品 {name} 失败!", name, 999
|
||||
return "获取商品失败", "", 999
|
||||
|
||||
|
||||
# 更新商品信息
|
||||
async def update_goods(**kwargs) -> "str, str, int":
|
||||
async def update_goods(**kwargs) -> Tuple[bool, str, str]:
|
||||
"""
|
||||
更新商品信息
|
||||
:param kwargs: kwargs
|
||||
@@ -202,17 +319,19 @@ async def update_goods(**kwargs) -> "str, str, int":
|
||||
goods_lst = await GoodsInfo.get_all_goods()
|
||||
if is_number(kwargs["name"]):
|
||||
if int(kwargs["name"]) < 1 or int(kwargs["name"]) > len(goods_lst):
|
||||
return "序号错误,没有该序号的商品...", "", 999
|
||||
return False, "序号错误,没有该序号的商品...", ""
|
||||
goods = goods_lst[int(kwargs["name"]) - 1]
|
||||
else:
|
||||
goods = await GoodsInfo.get_goods_info(kwargs["name"])
|
||||
goods = await GoodsInfo.filter(goods_name=kwargs["name"]).first()
|
||||
if not goods:
|
||||
return "名称错误,没有该名称的商品...", "", 999
|
||||
name = goods.goods_name
|
||||
return False, "名称错误,没有该名称的商品...", ""
|
||||
name: str = goods.goods_name
|
||||
price = goods.goods_price
|
||||
des = goods.goods_description
|
||||
discount = goods.goods_discount
|
||||
limit_time = goods.goods_limit_time
|
||||
daily_limit = goods.daily_limit
|
||||
is_passive = goods.is_passive
|
||||
new_time = 0
|
||||
tmp = ""
|
||||
if kwargs.get("price"):
|
||||
@@ -226,24 +345,41 @@ async def update_goods(**kwargs) -> "str, str, int":
|
||||
discount = kwargs["discount"]
|
||||
if kwargs.get("limit_time"):
|
||||
kwargs["limit_time"] = float(kwargs["limit_time"])
|
||||
new_time = time.strftime(
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
time.localtime(time.time() + kwargs["limit_time"] * 60 * 60),
|
||||
new_time = (
|
||||
time.strftime(
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
time.localtime(time.time() + kwargs["limit_time"] * 60 * 60),
|
||||
)
|
||||
if kwargs["limit_time"] != 0
|
||||
else 0
|
||||
)
|
||||
tmp += f"限时至: {new_time}\n"
|
||||
tmp += f"限时至: {new_time}\n" if new_time else "取消了限时\n"
|
||||
limit_time = kwargs["limit_time"]
|
||||
return (
|
||||
await GoodsInfo.update_goods(
|
||||
name,
|
||||
int(price),
|
||||
des,
|
||||
float(discount),
|
||||
int(
|
||||
time.time() + limit_time * 60 * 60
|
||||
if limit_time != 0 and new_time
|
||||
else 0
|
||||
),
|
||||
if kwargs.get("daily_limit"):
|
||||
tmp += (
|
||||
f'每日购买限制:{daily_limit} --> {kwargs["daily_limit"]}\n'
|
||||
if daily_limit
|
||||
else "取消了购买限制\n"
|
||||
)
|
||||
daily_limit = int(kwargs["daily_limit"])
|
||||
if kwargs.get("is_passive"):
|
||||
tmp += f'被动道具:{is_passive} --> {kwargs["is_passive"]}\n'
|
||||
des = kwargs["is_passive"]
|
||||
await GoodsInfo.update_goods(
|
||||
name,
|
||||
int(price),
|
||||
des,
|
||||
float(discount),
|
||||
int(
|
||||
time.time() + limit_time * 60 * 60
|
||||
if limit_time != 0 and new_time
|
||||
else 0
|
||||
),
|
||||
daily_limit,
|
||||
is_passive,
|
||||
)
|
||||
return (
|
||||
True,
|
||||
name,
|
||||
tmp[:-1],
|
||||
)
|
||||
@@ -276,6 +412,10 @@ def parse_goods_info(msg: str) -> Union[dict, str]:
|
||||
data["discount"] = sp[1]
|
||||
elif sp[0] == "limit_time":
|
||||
if not is_number(sp[1]) or float(sp[1]) < 0:
|
||||
return "limit_time参数不合法,必须大于0!"
|
||||
return "limit_time参数不合法,必须为数字且大于0!"
|
||||
data["limit_time"] = sp[1]
|
||||
elif sp[0] == "daily_limit":
|
||||
if not is_number(sp[1]) or float(sp[1]) < 0:
|
||||
return "daily_limit参数不合法,必须为数字且大于0!"
|
||||
data["daily_limit"] = sp[1]
|
||||
return data
|
||||
|
||||
@@ -1,26 +1,33 @@
|
||||
from nonebot import on_command
|
||||
from services.log import logger
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message
|
||||
from nonebot.params import CommandArg
|
||||
from utils.utils import is_number
|
||||
from models.bag_user import BagUser
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from services.db_context import db
|
||||
from nonebot.plugin import export
|
||||
from .data_source import effect, register_use, func_manager
|
||||
from typing import Any, Tuple
|
||||
|
||||
from nonebot import on_command, on_regex
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from nonebot.params import CommandArg, RegexGroup
|
||||
|
||||
from models.bag_user import BagUser
|
||||
from models.user_shop_gold_log import UserShopGoldLog
|
||||
from services.log import logger
|
||||
from utils.decorator.shop import NotMeetUseConditionsException
|
||||
from utils.utils import is_number
|
||||
|
||||
from .data_source import build_params, effect, func_manager, register_use
|
||||
|
||||
__zx_plugin_name__ = "商店 - 使用道具"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
普通的使用道具
|
||||
指令:
|
||||
使用道具 [序号或道具名称] ?[数量]=1
|
||||
使用道具 [序号或道具名称] ?[数量]=1 ?[其他信息]
|
||||
示例:使用道具好感度双倍加持卡 使用道具好感度双倍加持卡
|
||||
示例:使用道具1 使用第一个道具
|
||||
示例:使用道具1 10 使用10个第一个道具
|
||||
示例:使用道具1 1 来点色图 使用第一个道具并附带信息
|
||||
* 序号以 ”我的道具“ 为准 *
|
||||
""".strip()
|
||||
__plugin_des__ = "商店 - 使用道具"
|
||||
__plugin_cmd__ = ["使用道具 [序号或道具名称]"]
|
||||
__plugin_type__ = ('商店',)
|
||||
__plugin_type__ = ("商店",)
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
@@ -30,52 +37,81 @@ __plugin_settings__ = {
|
||||
"cmd": ["商店", "使用道具"],
|
||||
}
|
||||
|
||||
# 导出方法供其他插件使用
|
||||
export = export()
|
||||
export.register_use = register_use
|
||||
|
||||
use_props = on_command("使用道具", priority=5, block=True, permission=GROUP)
|
||||
use_props = on_command(r"使用道具", priority=5, block=True, permission=GROUP)
|
||||
|
||||
|
||||
@use_props.handle()
|
||||
async def _(bot: Bot, event: GroupMessageEvent, arg: Message = CommandArg()):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
msg = arg.extract_plain_text()
|
||||
num = 1
|
||||
msg_sp = msg.split()
|
||||
if len(msg_sp) > 1 and is_number(msg_sp[-1]) and int(msg_sp[-1]) > 0:
|
||||
num = int(msg.split()[-1])
|
||||
msg = " ".join(msg.split()[:-1])
|
||||
property_ = await BagUser.get_property(event.user_id, event.group_id)
|
||||
text = ""
|
||||
prop_n = None
|
||||
index = None
|
||||
split = msg.split()
|
||||
if size := len(split):
|
||||
if size == 1:
|
||||
prop_n = split[0].strip()
|
||||
index = 1
|
||||
if size > 1 and is_number(split[1].strip()):
|
||||
prop_n = split[0].strip()
|
||||
num = int(split[1].strip())
|
||||
index = 2
|
||||
else:
|
||||
await use_props.finish("缺少参数,请查看帮助", at_sender=True)
|
||||
if index:
|
||||
text = " ".join(split[index:])
|
||||
property_ = await BagUser.get_property(event.user_id, event.group_id, True)
|
||||
if property_:
|
||||
async with db.transaction():
|
||||
if is_number(msg):
|
||||
if 0 < int(msg) <= len(property_):
|
||||
name = list(property_.keys())[int(msg) - 1]
|
||||
else:
|
||||
await use_props.finish("仔细看看自己的道具仓库有没有这个道具?", at_sender=True)
|
||||
name = None
|
||||
if prop_n and is_number(prop_n):
|
||||
if 0 < int(prop_n) <= len(property_):
|
||||
name = list(property_.keys())[int(prop_n) - 1]
|
||||
else:
|
||||
if msg not in property_.keys():
|
||||
await use_props.finish("道具名称错误!", at_sender=True)
|
||||
name = msg
|
||||
_user_prop_count = property_[name]
|
||||
if num > _user_prop_count:
|
||||
await use_props.finish(f"道具数量不足,无法使用{num}次!")
|
||||
if num > (n := func_manager.get_max_num_limit(name)):
|
||||
await use_props.finish(f"该道具单次只能使用 {n} 个!")
|
||||
if await BagUser.delete_property(
|
||||
event.user_id, event.group_id, name, num
|
||||
):
|
||||
await use_props.finish("仔细看看自己的道具仓库有没有这个道具?", at_sender=True)
|
||||
else:
|
||||
if prop_n not in property_.keys():
|
||||
await use_props.finish("道具名称错误!", at_sender=True)
|
||||
name = prop_n
|
||||
if not name:
|
||||
await use_props.finish("未获取到道具名称", at_sender=True)
|
||||
_user_prop_count = property_[name]
|
||||
if num > _user_prop_count:
|
||||
await use_props.finish(f"道具数量不足,无法使用{num}次!")
|
||||
if num > (n := func_manager.get_max_num_limit(name)):
|
||||
await use_props.finish(f"该道具单次只能使用 {n} 个!")
|
||||
try:
|
||||
model, kwargs = build_params(bot, event, name, num, text)
|
||||
except KeyError:
|
||||
logger.warning(f"{name} 未注册使用函数")
|
||||
await use_props.finish(f"{name} 未注册使用方法")
|
||||
else:
|
||||
try:
|
||||
await func_manager.run_handle(
|
||||
type_="before_handle", param=model, **kwargs
|
||||
)
|
||||
except NotMeetUseConditionsException as e:
|
||||
await use_props.finish(e.get_info(), at_sender=True)
|
||||
if await BagUser.delete_property(event.user_id, event.group_id, name, num):
|
||||
if func_manager.check_send_success_message(name):
|
||||
await use_props.send(f"使用道具 {name} {num} 次成功!", at_sender=True)
|
||||
if msg := await effect(bot, event, name, num):
|
||||
if msg := await effect(bot, event, name, num, text, event.message):
|
||||
await use_props.send(msg, at_sender=True)
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP {event.group_id} 使用道具 {name} {num} 次成功"
|
||||
)
|
||||
await UserShopGoldLog.create(
|
||||
user_qq=event.user_id,
|
||||
group_id=event.group_id,
|
||||
type=1,
|
||||
name=name,
|
||||
num=num,
|
||||
)
|
||||
else:
|
||||
await use_props.send(f"使用道具 {name} {num} 次失败!", at_sender=True)
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP {event.group_id} 使用道具 {name} {num} 次失败"
|
||||
)
|
||||
await func_manager.run_handle(type_="after_handle", param=model, **kwargs)
|
||||
else:
|
||||
await use_props.send("您的背包里没有任何的道具噢", at_sender=True)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment, Message
|
||||
from services.log import logger
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
from pydantic import create_model
|
||||
from utils.models import ShopParam
|
||||
from typing import Optional, Union
|
||||
from typing import Optional, Union, Callable, List, Tuple, Dict, Any
|
||||
from types import MappingProxyType
|
||||
import inspect
|
||||
import asyncio
|
||||
@@ -13,6 +13,30 @@ class GoodsUseFuncManager:
|
||||
def __init__(self):
|
||||
self._data = {}
|
||||
|
||||
def register_use_before_handle(self, goods_name: str, fun_list: List[Callable]):
|
||||
"""
|
||||
说明:
|
||||
注册商品使用前函数
|
||||
参数:
|
||||
:param goods_name: 商品名称
|
||||
:param fun_list: 函数列表
|
||||
"""
|
||||
if fun_list:
|
||||
self._data[goods_name]["before_handle"] = fun_list
|
||||
logger.info(f"register_use_before_handle 成功注册商品:{goods_name} 的{len(fun_list)}个使用前函数")
|
||||
|
||||
def register_use_after_handle(self, goods_name: str, fun_list: List[Callable]):
|
||||
"""
|
||||
说明:
|
||||
注册商品使用后函数
|
||||
参数:
|
||||
:param goods_name: 商品名称
|
||||
:param fun_list: 函数列表
|
||||
"""
|
||||
if fun_list:
|
||||
self._data[goods_name]["after_handle"] = fun_list
|
||||
logger.info(f"register_use_after_handle 成功注册商品:{goods_name} 的{len(fun_list)}个使用后函数")
|
||||
|
||||
def register_use(self, goods_name: str, **kwargs):
|
||||
"""
|
||||
注册商品使用方法
|
||||
@@ -26,7 +50,7 @@ class GoodsUseFuncManager:
|
||||
判断商品使用方法是否被注册
|
||||
:param goods_name: 商品名称
|
||||
"""
|
||||
return bool(self ._data.get(goods_name))
|
||||
return bool(self._data.get(goods_name))
|
||||
|
||||
def get_max_num_limit(self, goods_name: str) -> int:
|
||||
"""
|
||||
@@ -37,6 +61,21 @@ class GoodsUseFuncManager:
|
||||
return self._data[goods_name]["kwargs"]["max_num_limit"]
|
||||
return 1
|
||||
|
||||
def _parse_args(self, args_: MappingProxyType, param: ShopParam, **kwargs):
|
||||
param_list_ = []
|
||||
_bot = param.bot
|
||||
param.bot = None
|
||||
param_json = param.dict()
|
||||
param_json["bot"] = _bot
|
||||
for par in args_.keys():
|
||||
if par in ["shop_param"]:
|
||||
param_list_.append(param)
|
||||
elif par not in ["args", "kwargs"]:
|
||||
param_list_.append(param_json.get(par))
|
||||
if kwargs.get(par) is not None:
|
||||
del kwargs[par]
|
||||
return param_list_
|
||||
|
||||
async def use(
|
||||
self, param: ShopParam, **kwargs
|
||||
) -> Optional[Union[str, MessageSegment]]:
|
||||
@@ -45,31 +84,18 @@ class GoodsUseFuncManager:
|
||||
:param param: BaseModel
|
||||
:param kwargs: kwargs
|
||||
"""
|
||||
def parse_args(args_: MappingProxyType):
|
||||
param_list_ = []
|
||||
_bot = param.bot
|
||||
param.bot = None
|
||||
param_json = param.dict()
|
||||
param_json["bot"] = _bot
|
||||
for par in args_.keys():
|
||||
if par in ["shop_param"]:
|
||||
param_list_.append(param)
|
||||
elif par not in ["args", "kwargs"]:
|
||||
param_list_.append(param_json.get(par))
|
||||
if kwargs.get(par) is not None:
|
||||
del kwargs[par]
|
||||
return param_list_
|
||||
goods_name = param.goods_name
|
||||
if self.exists(goods_name):
|
||||
# 使用方法
|
||||
args = inspect.signature(self._data[goods_name]["func"]).parameters
|
||||
if args and list(args.keys())[0] != "kwargs":
|
||||
if asyncio.iscoroutinefunction(self._data[goods_name]["func"]):
|
||||
return await self._data[goods_name]["func"](
|
||||
*parse_args(args)
|
||||
*self._parse_args(args, param, **kwargs)
|
||||
)
|
||||
else:
|
||||
return self._data[goods_name]["func"](
|
||||
*parse_args(args)
|
||||
*self._parse_args(args, param, **kwargs)
|
||||
)
|
||||
else:
|
||||
if asyncio.iscoroutinefunction(self._data[goods_name]["func"]):
|
||||
@@ -81,6 +107,21 @@ class GoodsUseFuncManager:
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
async def run_handle(self, goods_name: str, type_: str, param: ShopParam, **kwargs):
|
||||
if self._data.get(goods_name) and self._data[goods_name].get(type_):
|
||||
for func in self._data[goods_name].get(type_):
|
||||
args = inspect.signature(func).parameters
|
||||
if args and list(args.keys())[0] != "kwargs":
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
await func(*self._parse_args(args, param, **kwargs))
|
||||
else:
|
||||
func(*self._parse_args(args, param, **kwargs))
|
||||
else:
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
await func(**kwargs)
|
||||
else:
|
||||
func(**kwargs)
|
||||
|
||||
def check_send_success_message(self, goods_name: str) -> bool:
|
||||
"""
|
||||
检查是否发送使用成功信息
|
||||
@@ -99,7 +140,7 @@ class GoodsUseFuncManager:
|
||||
return self._data[goods_name]["kwargs"]
|
||||
return {}
|
||||
|
||||
def init_model(self, goods_name: str, bot: Bot, event: GroupMessageEvent, num: int):
|
||||
def init_model(self, goods_name: str, bot: Bot, event: GroupMessageEvent, num: int, text: str):
|
||||
return self._data[goods_name]["model"](
|
||||
**{
|
||||
"goods_name": goods_name,
|
||||
@@ -108,6 +149,8 @@ class GoodsUseFuncManager:
|
||||
"user_id": event.user_id,
|
||||
"group_id": event.group_id,
|
||||
"num": num,
|
||||
"message": event.message,
|
||||
"text": text
|
||||
}
|
||||
)
|
||||
|
||||
@@ -115,8 +158,38 @@ class GoodsUseFuncManager:
|
||||
func_manager = GoodsUseFuncManager()
|
||||
|
||||
|
||||
def build_params(
|
||||
bot: Bot, event: GroupMessageEvent, goods_name: str, num: int, text: str
|
||||
) -> Tuple[ShopParam, Dict[str, Any]]:
|
||||
"""
|
||||
说明:
|
||||
构造参数
|
||||
参数:
|
||||
:param bot: bot
|
||||
:param event: event
|
||||
:param goods_name: 商品名称
|
||||
:param num: 数量
|
||||
:param text: 其他信息
|
||||
"""
|
||||
_kwargs = func_manager.get_kwargs(goods_name)
|
||||
return (
|
||||
func_manager.init_model(goods_name, bot, event, num, text),
|
||||
{
|
||||
**_kwargs,
|
||||
"_bot": bot,
|
||||
"event": event,
|
||||
"group_id": event.group_id,
|
||||
"user_id": event.user_id,
|
||||
"num": num,
|
||||
"text": text,
|
||||
"message": event.message,
|
||||
"goods_name": goods_name,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def effect(
|
||||
bot: Bot, event: GroupMessageEvent, goods_name: str, num: int
|
||||
bot: Bot, event: GroupMessageEvent, goods_name: str, num: int, text: str, message: Message
|
||||
) -> Optional[Union[str, MessageSegment]]:
|
||||
"""
|
||||
商品生效
|
||||
@@ -124,30 +197,22 @@ async def effect(
|
||||
:param event: GroupMessageEvent
|
||||
:param goods_name: 商品名称
|
||||
:param num: 使用数量
|
||||
:param text: 其他信息
|
||||
:param message: Message
|
||||
:return: 使用是否成功
|
||||
"""
|
||||
# 优先使用注册的商品插件
|
||||
# try:
|
||||
if func_manager.exists(goods_name):
|
||||
_kwargs = func_manager.get_kwargs(goods_name)
|
||||
return await func_manager.use(
|
||||
func_manager.init_model(goods_name, bot, event, num),
|
||||
**{
|
||||
**_kwargs,
|
||||
"_bot": bot,
|
||||
"event": event,
|
||||
"group_id": event.group_id,
|
||||
"user_id": event.user_id,
|
||||
"num": num,
|
||||
"goods_name": goods_name,
|
||||
},
|
||||
)
|
||||
model, kwargs = build_params(bot, event, goods_name, num, text)
|
||||
return await func_manager.use(model, **kwargs)
|
||||
# except Exception as e:
|
||||
# logger.error(f"use 商品生效函数effect 发生错误 {type(e)}:{e}")
|
||||
return None
|
||||
|
||||
|
||||
def register_use(goods_name: str, func, **kwargs):
|
||||
def register_use(goods_name: str, func: Callable, **kwargs):
|
||||
"""
|
||||
注册商品使用方法
|
||||
:param goods_name: 商品名称
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
|
||||
|
||||
nonebot.load_plugins('basic_plugins/super_cmd')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.adapters.onebot.v11 import Bot, Message
|
||||
from nonebot.params import Command, CommandArg
|
||||
from typing import Tuple
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Bot, Message, MessageEvent
|
||||
from nonebot.params import Command, CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import to_me
|
||||
from utils.utils import is_number
|
||||
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
from utils.depends import OneCommand
|
||||
from utils.manager import requests_manager
|
||||
from utils.message_builder import image
|
||||
from models.group_info import GroupInfo
|
||||
|
||||
from utils.utils import is_number
|
||||
|
||||
__zx_plugin_name__ = "显示所有好友群组 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
@@ -76,8 +79,7 @@ async def _(bot: Bot):
|
||||
|
||||
|
||||
@friend_handle.handle()
|
||||
async def _(bot: Bot, cmd: Tuple[str, ...] = Command(), arg: Message = CommandArg()):
|
||||
cmd = cmd[0]
|
||||
async def _(bot: Bot, cmd: str = OneCommand(), arg: Message = CommandArg()):
|
||||
id_ = arg.extract_plain_text().strip()
|
||||
if is_number(id_):
|
||||
id_ = int(id_)
|
||||
@@ -97,8 +99,9 @@ async def _(bot: Bot, cmd: Tuple[str, ...] = Command(), arg: Message = CommandAr
|
||||
|
||||
|
||||
@group_handle.handle()
|
||||
async def _(bot: Bot, cmd: Tuple[str, ...] = Command(), arg: Message = CommandArg()):
|
||||
cmd = cmd[0]
|
||||
async def _(
|
||||
bot: Bot, event: MessageEvent, cmd: str = OneCommand(), arg: Message = CommandArg()
|
||||
):
|
||||
id_ = arg.extract_plain_text().strip()
|
||||
flag = None
|
||||
if is_number(id_):
|
||||
@@ -106,20 +109,22 @@ async def _(bot: Bot, cmd: Tuple[str, ...] = Command(), arg: Message = CommandAr
|
||||
if cmd[:2] == "同意":
|
||||
rid = requests_manager.get_group_id(id_)
|
||||
if rid:
|
||||
if await GroupInfo.get_group_info(rid):
|
||||
await GroupInfo.set_group_flag(rid, 1)
|
||||
if group := await GroupInfo.get_or_none(group_id=rid):
|
||||
group.group_flag = 1
|
||||
await group.save(update_fields=["group_flag"])
|
||||
else:
|
||||
group_info = await bot.get_group_info(group_id=rid)
|
||||
await GroupInfo.add_group_info(
|
||||
rid,
|
||||
group_info["group_name"],
|
||||
group_info["max_member_count"],
|
||||
group_info["member_count"],
|
||||
1
|
||||
await GroupInfo.create(
|
||||
group_id=rid,
|
||||
group_name=group_info["group_name"],
|
||||
max_member_count=group_info["max_member_count"],
|
||||
member_count=group_info["member_count"],
|
||||
group_flag=1,
|
||||
)
|
||||
flag = await requests_manager.approve(bot, id_, "group")
|
||||
else:
|
||||
await group_handle.send("同意群聊请求失败,未找到此id的请求..")
|
||||
logger.info("同意群聊请求失败,未找到此id的请求..", cmd, event.user_id)
|
||||
else:
|
||||
flag = await requests_manager.refused(bot, id_, "group")
|
||||
if flag == 1:
|
||||
@@ -139,15 +144,15 @@ async def _():
|
||||
for type_ in ["private", "group"]:
|
||||
msg = await requests_manager.show(type_)
|
||||
if msg:
|
||||
_str += image(b64=msg)
|
||||
_str += image(msg)
|
||||
else:
|
||||
_str += "没有任何好友请求.." if type_ == "private" else "没有任何群聊请求.."
|
||||
if type_ == "private":
|
||||
_str += '\n--------------------\n'
|
||||
_str += "\n--------------------\n"
|
||||
await cls_request.send(Message(_str))
|
||||
|
||||
|
||||
@clear_request.handle()
|
||||
async def _():
|
||||
requests_manager.clear()
|
||||
await clear_request.send("已清空所有好友/群聊请求..")
|
||||
await clear_request.send("已清空所有好友/群聊请求..")
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.permission import SUPERUSER
|
||||
from configs.path_config import TEMP_PATH
|
||||
from nonebot.rule import to_me
|
||||
from utils.utils import scheduler
|
||||
|
||||
from configs.path_config import TEMP_PATH
|
||||
from services.log import logger
|
||||
from utils.manager import resources_manager
|
||||
import asyncio
|
||||
import time
|
||||
import os
|
||||
from utils.utils import scheduler
|
||||
|
||||
__zx_plugin_name__ = "清理临时数据 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
@@ -37,22 +39,29 @@ async def _():
|
||||
await clear_data.send("开始清理临时数据....")
|
||||
size = await asyncio.get_event_loop().run_in_executor(None, _clear_data)
|
||||
await clear_data.send("共清理了 {:.2f}MB 的数据...".format(size / 1024 / 1024))
|
||||
logger.info("清理临时数据完成," + "共清理了 {:.2f}MB 的数据...".format(size / 1024 / 1024))
|
||||
|
||||
|
||||
def _clear_data() -> float:
|
||||
logger.debug("开始清理临时文件...")
|
||||
size = 0
|
||||
for dir_ in resources_manager.get_temp_data_dir():
|
||||
if dir_.exists():
|
||||
for file in os.listdir(dir_):
|
||||
file = dir_ / file
|
||||
if file.is_file():
|
||||
try:
|
||||
if time.time() - os.path.getatime(file) > 300:
|
||||
file_size = os.path.getsize(file)
|
||||
file.unlink()
|
||||
size += file_size
|
||||
except Exception as e:
|
||||
logger.error(f"清理临时数据错误...{type(e)}:{e}")
|
||||
dir_list = [dir_ for dir_ in resources_manager.get_temp_data_dir() if dir_.exists()]
|
||||
for dir_ in dir_list:
|
||||
logger.debug(f"尝试清理文件夹: {dir_.absolute()}", "清理临时数据")
|
||||
dir_size = 0
|
||||
for file in os.listdir(dir_):
|
||||
file = dir_ / file
|
||||
if file.is_file():
|
||||
try:
|
||||
if time.time() - os.path.getatime(file) > 10:
|
||||
file_size = os.path.getsize(file)
|
||||
file.unlink()
|
||||
size += file_size
|
||||
dir_size += file_size
|
||||
logger.debug(f"移除临时文件: {file.absolute()}", "清理临时数据")
|
||||
except Exception as e:
|
||||
logger.error(f"清理临时数据错误,临时文件夹: {dir_.absolute()}...", "清理临时数据", e=e)
|
||||
logger.debug("清理临时文件夹大小: {:.2f}MB".format(size / 1024 / 1024), "清理临时数据")
|
||||
return float(size)
|
||||
|
||||
|
||||
|
||||
@@ -1,37 +1,92 @@
|
||||
from nonebot.adapters.onebot.v11 import Message
|
||||
import asyncio
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import to_me
|
||||
from services.db_context import db
|
||||
from nonebot.params import CommandArg
|
||||
from tortoise import Tortoise
|
||||
|
||||
from services.db_context import TestSQL
|
||||
from services.log import logger
|
||||
from utils.message_builder import custom_forward_msg
|
||||
|
||||
__zx_plugin_name__ = "执行sql [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
执行一段sql语句
|
||||
指令:
|
||||
exec [sql语句]
|
||||
exec [sql语句] ([查询页数,19条/页])
|
||||
查看所有表
|
||||
""".strip()
|
||||
__plugin_des__ = "执行一段sql语句"
|
||||
__plugin_cmd__ = [
|
||||
"exec [sql语句]",
|
||||
]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_cmd__ = ["exec [sql语句]", "查看所有表"]
|
||||
__plugin_version__ = 0.2
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
|
||||
exec_ = on_command("exec", rule=to_me(), permission=SUPERUSER, priority=1, block=True)
|
||||
tables = on_command("查看所有表", rule=to_me(), permission=SUPERUSER, priority=1, block=True)
|
||||
|
||||
|
||||
@exec_.handle()
|
||||
async def _(arg: Message = CommandArg()):
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
sql = arg.extract_plain_text().strip()
|
||||
async with db.transaction():
|
||||
if not sql:
|
||||
await exec_.finish("未接受到sql语句")
|
||||
db = Tortoise.get_connection("default")
|
||||
# try:
|
||||
# 判断是否为SELECT语句
|
||||
if sql.lower().startswith("select"):
|
||||
pass
|
||||
# # 分割语句
|
||||
try:
|
||||
query = db.text(sql)
|
||||
await db.first(query)
|
||||
await exec_.send("执行 sql 语句成功.")
|
||||
except Exception as e:
|
||||
await exec_.send(f"执行 sql 语句失败 {type(e)}:{e}")
|
||||
logger.error(f"执行 sql 语句失败 {type(e)}:{e}")
|
||||
page = int(sql.split(" ")[-1]) - 1
|
||||
sql_list = sql.split(" ")[:-1]
|
||||
except ValueError:
|
||||
page = 0
|
||||
sql_list = sql.split(" ")
|
||||
# 拼接语句
|
||||
sql = " ".join(sql_list)
|
||||
res = await db.execute_query_dict(sql)
|
||||
msg_list = [f"第{page+1}页查询结果:"]
|
||||
# logger.info(res)
|
||||
# 获取所有字段
|
||||
keys = res[0].keys()
|
||||
# 每页10条
|
||||
for i in res[page * 10 : (page + 1) * 10]:
|
||||
msg = ""
|
||||
for key in keys:
|
||||
msg += f"{key}: {i[key]}\n"
|
||||
msg += f"第{page+1}页第{res.index(i)+1}条"
|
||||
msg_list.append(msg)
|
||||
# 检查是私聊还是群聊
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
forward_msg_list = custom_forward_msg(msg_list, bot.self_id)
|
||||
await bot.send_group_forward_msg(
|
||||
group_id=event.group_id, messages=forward_msg_list
|
||||
)
|
||||
else:
|
||||
for msg in msg_list:
|
||||
await exec_.send(msg)
|
||||
await asyncio.sleep(0.2)
|
||||
return
|
||||
else:
|
||||
await TestSQL.raw(sql)
|
||||
await exec_.send("执行 sql 语句成功.")
|
||||
# except Exception as e:
|
||||
# await exec_.send(f"执行 sql 语句失败 {type(e)}:{e}")
|
||||
# logger.error(f"执行 sql 语句失败 {type(e)}:{e}")
|
||||
|
||||
|
||||
@tables.handle()
|
||||
async def _(bot: Bot, event: MessageEvent):
|
||||
# 获取所有表
|
||||
db = Tortoise.get_connection("default")
|
||||
query = await db.execute_query_dict(
|
||||
"select tablename from pg_tables where schemaname = 'public'"
|
||||
)
|
||||
msg = "数据库中的所有表名:\n"
|
||||
for tablename in query:
|
||||
msg += str(tablename["tablename"]) + "\n"
|
||||
await tables.finish(msg)
|
||||
|
||||
@@ -1,29 +1,39 @@
|
||||
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, GROUP, GroupMessageEvent, Message
|
||||
from nonebot import on_command, on_regex
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.rule import to_me
|
||||
from utils.utils import is_number
|
||||
from utils.manager import group_manager, plugins2settings_manager
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
from configs.config import NICKNAME
|
||||
from nonebot.adapters.onebot.v11.exception import ActionFailed
|
||||
from nonebot.params import Command, CommandArg
|
||||
from typing import Tuple
|
||||
|
||||
from nonebot import on_command, on_regex
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
GROUP,
|
||||
Bot,
|
||||
GroupMessageEvent,
|
||||
Message,
|
||||
MessageEvent,
|
||||
)
|
||||
from nonebot.params import Command, CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import to_me
|
||||
|
||||
from configs.config import NICKNAME
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
from utils.depends import OneCommand
|
||||
from utils.image_utils import text2image
|
||||
from utils.manager import group_manager, plugins2settings_manager
|
||||
from utils.message_builder import image
|
||||
from utils.utils import is_number
|
||||
|
||||
__zx_plugin_name__ = "管理群操作 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
群权限 | 群白名单 | 退出群 操作
|
||||
指令:
|
||||
退群 [group_id]
|
||||
退群,添加/删除群白名单,添加/删除群认证,当在群聊中这五个命令且没有指定群号时,默认指定当前群聊
|
||||
指令:
|
||||
退群 ?[group_id]
|
||||
修改群权限 [group_id] [等级]
|
||||
添加群白名单 *[group_id]
|
||||
删除群白名单 *[group_id]
|
||||
添加群认证 *[group_id]
|
||||
删除群认证 *[group_id]
|
||||
修改群权限 [等级]: 该命令仅在群聊时生效,默认修改当前群聊
|
||||
添加群白名单 ?*[group_id]
|
||||
删除群白名单 ?*[group_id]
|
||||
添加群认证 ?*[group_id]
|
||||
删除群认证 ?*[group_id]
|
||||
查看群白名单
|
||||
""".strip()
|
||||
__plugin_des__ = "管理群操作"
|
||||
@@ -47,7 +57,7 @@ my_group_level = on_command(
|
||||
"查看群权限", aliases={"群权限"}, priority=5, permission=GROUP, block=True
|
||||
)
|
||||
what_up_group_level = on_regex(
|
||||
".*?(提高|提升|升高|增加|加上)(.*?)群权限.*?",
|
||||
"(:?提高|提升|升高|增加|加上).*?群权限",
|
||||
rule=to_me(),
|
||||
priority=5,
|
||||
permission=GROUP,
|
||||
@@ -67,47 +77,57 @@ group_auth = on_command(
|
||||
|
||||
|
||||
@del_group.handle()
|
||||
async def _(bot: Bot, arg: Message = CommandArg()):
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
group_id = arg.extract_plain_text().strip()
|
||||
if not group_id and isinstance(event, GroupMessageEvent):
|
||||
group_id = event.group_id
|
||||
if group_id:
|
||||
if is_number(group_id):
|
||||
group_list = [x["group_id"] for x in await bot.get_group_list()]
|
||||
group_id = int(group_id)
|
||||
if group_id not in group_list:
|
||||
logger.debug("群聊不存在", "退群", event.user_id, target=group_id)
|
||||
await del_group.finish(f"{NICKNAME}未在该群聊中...")
|
||||
try:
|
||||
await bot.set_group_leave(group_id=int(group_id))
|
||||
logger.info(f"退出群聊 {group_id} 成功")
|
||||
await bot.set_group_leave(group_id=group_id)
|
||||
logger.info(f"{NICKNAME}退出群聊成功", "退群", event.user_id, target=group_id)
|
||||
await del_group.send(f"退出群聊 {group_id} 成功", at_sender=True)
|
||||
group_manager.delete_group(int(group_id))
|
||||
await GroupInfo.delete_group_info(int(group_id))
|
||||
group_manager.delete_group(group_id)
|
||||
await GroupInfo.filter(group_id=group_id).delete()
|
||||
except Exception as e:
|
||||
logger.info(f"退出群聊 {group_id} 失败 e:{e}")
|
||||
logger.error(f"退出群聊失败", "退群", event.user_id, target=group_id, e=e)
|
||||
await del_group.send(f"退出群聊 {group_id} 失败", at_sender=True)
|
||||
else:
|
||||
await del_group.finish(f"请输入正确的群号", at_sender=True)
|
||||
await del_group.send(f"请输入正确的群号", at_sender=True)
|
||||
else:
|
||||
await del_group.finish(f"请输入群号", at_sender=True)
|
||||
await del_group.send(f"请输入群号", at_sender=True)
|
||||
|
||||
|
||||
@add_group_level.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, arg: Message = CommandArg()):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
msg = msg.split()
|
||||
group_id = 0
|
||||
level = 0
|
||||
if isinstance(event, GroupMessageEvent) and len(msg) == 1:
|
||||
msg = [event.group_id, msg[0]]
|
||||
if not msg:
|
||||
await add_group_level.finish("用法:修改群权限 [group] [level]")
|
||||
msg = msg.split()
|
||||
await add_group_level.finish("缺失参数...")
|
||||
if len(msg) < 2:
|
||||
await add_group_level.finish("参数不完全..[group] [level]")
|
||||
await add_group_level.finish("缺失参数...")
|
||||
if is_number(msg[0]) and is_number(msg[1]):
|
||||
group_id = msg[0]
|
||||
group_id = int(msg[0])
|
||||
level = int(msg[1])
|
||||
else:
|
||||
await add_group_level.finish("参数错误...group和level必须是数字..")
|
||||
await add_group_level.finish("参数错误...群号和等级必须是数字..")
|
||||
old_level = group_manager.get_group_level(group_id)
|
||||
group_manager.set_group_level(group_id, level)
|
||||
await add_group_level.send("修改成功...", at_sender=True)
|
||||
if level > -1:
|
||||
await bot.send_group_msg(
|
||||
group_id=int(group_id), message=f"管理员修改了此群权限:{old_level} -> {level}"
|
||||
group_id=group_id, message=f"管理员修改了此群权限:{old_level} -> {level}"
|
||||
)
|
||||
logger.info(f"{event.user_id} 修改了 {group_id} 的权限:{level}")
|
||||
logger.info(f"修改群权限:{level}", "修改群权限", event.user_id, target=group_id)
|
||||
|
||||
|
||||
@my_group_level.handle()
|
||||
@@ -116,89 +136,109 @@ async def _(event: GroupMessageEvent):
|
||||
tmp = ""
|
||||
data = plugins2settings_manager.get_data()
|
||||
for module in data:
|
||||
if data[module]["level"] > level:
|
||||
plugin_name = data[module]["cmd"][0]
|
||||
if data[module].level > level:
|
||||
plugin_name = data[module].cmd[0]
|
||||
if plugin_name == "pixiv":
|
||||
plugin_name = "搜图 p站排行"
|
||||
tmp += f"{plugin_name}\n"
|
||||
if tmp:
|
||||
tmp = "\n目前无法使用的功能:\n" + tmp
|
||||
await my_group_level.finish(f"当前群权限:{level}{tmp}")
|
||||
if not tmp:
|
||||
await my_group_level.finish(f"当前群权限:{level}")
|
||||
await my_group_level.finish(
|
||||
f"当前群权限:{level}\n目前无法使用的功能:\n"
|
||||
+ image(await text2image(tmp, padding=10, color="#f9f6f2"))
|
||||
)
|
||||
|
||||
|
||||
@what_up_group_level.handle()
|
||||
async def _():
|
||||
await what_up_group_level.finish(
|
||||
f"[此功能用于防止内鬼,如果引起不便那真是抱歉了]\n" f"目前提高群权限的方法:\n" f"\t1.管理员修改权限"
|
||||
f"[此功能用于防止内鬼,如果引起不便那真是抱歉了]\n" f"目前提高群权限的方法:\n" f"\t1.超级管理员修改权限"
|
||||
)
|
||||
|
||||
|
||||
@manager_group_whitelist.handle()
|
||||
async def _(bot: Bot, cmd: Tuple[str, ...] = Command(), arg: Message = CommandArg()):
|
||||
cmd = cmd[0]
|
||||
async def _(
|
||||
bot: Bot, event: MessageEvent, cmd: str = OneCommand(), arg: Message = CommandArg()
|
||||
):
|
||||
msg = arg.extract_plain_text().strip().split()
|
||||
all_group = [
|
||||
g["group_id"] for g in await bot.get_group_list()
|
||||
]
|
||||
if not msg and isinstance(event, GroupMessageEvent):
|
||||
msg = [event.group_id]
|
||||
if not msg:
|
||||
await manager_group_whitelist.finish("请输入群号")
|
||||
all_group = [g["group_id"] for g in await bot.get_group_list()]
|
||||
error_group = []
|
||||
group_list = []
|
||||
for group in msg:
|
||||
if is_number(group) and int(group) in all_group:
|
||||
group_list.append(int(group))
|
||||
for group_id in msg:
|
||||
if is_number(group_id) and int(group_id) in all_group:
|
||||
group_list.append(int(group_id))
|
||||
else:
|
||||
logger.debug(f"群号不合法或不存在", cmd, target=group_id)
|
||||
error_group.append(group_id)
|
||||
if group_list:
|
||||
for group in group_list:
|
||||
for group_id in group_list:
|
||||
if cmd in ["添加群白名单"]:
|
||||
group_manager.add_group_white_list(group)
|
||||
group_manager.add_group_white_list(group_id)
|
||||
else:
|
||||
group_manager.delete_group_white_list(group)
|
||||
group_manager.delete_group_white_list(group_id)
|
||||
group_list = [str(x) for x in group_list]
|
||||
await manager_group_whitelist.send(
|
||||
"已成功将 " + "\n".join(group_list) + " " + cmd
|
||||
)
|
||||
else:
|
||||
await manager_group_whitelist.send(f"添加失败,请检查{NICKNAME}是否已加入这些群聊或重复添加/删除群白单名")
|
||||
await manager_group_whitelist.send("已成功将 " + "\n".join(group_list) + " " + cmd)
|
||||
group_manager.save()
|
||||
if error_group:
|
||||
await manager_group_whitelist.send("以下群聊不合法或不存在:\n" + "\n".join(error_group))
|
||||
|
||||
|
||||
@show_group_whitelist.handle()
|
||||
async def _():
|
||||
x = group_manager.get_group_white_list()
|
||||
x = [str(g) for g in x]
|
||||
if x:
|
||||
await show_group_whitelist.send("目前的群白名单:\n" + "\n".join(x))
|
||||
else:
|
||||
await show_group_whitelist.send("没有任何群在群白名单...")
|
||||
group = [str(g) for g in group_manager.get_group_white_list()]
|
||||
if not group:
|
||||
await show_group_whitelist.finish("没有任何群在群白名单...")
|
||||
await show_group_whitelist.send("目前的群白名单:\n" + "\n".join(group))
|
||||
|
||||
|
||||
@group_auth.handle()
|
||||
async def _(bot: Bot, cmd: Tuple[str, ...] = Command(), arg: Message = CommandArg()):
|
||||
cmd = cmd[0]
|
||||
async def _(
|
||||
bot: Bot, event: MessageEvent, cmd: str = OneCommand(), arg: Message = CommandArg()
|
||||
):
|
||||
msg = arg.extract_plain_text().strip().split()
|
||||
if isinstance(event, GroupMessageEvent) and not msg:
|
||||
msg = [event.group_id]
|
||||
if not msg:
|
||||
await manager_group_whitelist.finish("请输入群号")
|
||||
error_group = []
|
||||
all_group = [g["group_id"] for g in await bot.get_group_list()]
|
||||
for group_id in msg:
|
||||
if not is_number(group_id):
|
||||
await group_auth.send(f"{group_id}非纯数字,已跳过该项..")
|
||||
group_id = int(group_id)
|
||||
if cmd[:2] == "添加":
|
||||
if await GroupInfo.get_group_info(group_id):
|
||||
await GroupInfo.set_group_flag(group_id, 1)
|
||||
else:
|
||||
if is_number(group_id) and group_id in all_group:
|
||||
if cmd[:2] == "添加":
|
||||
try:
|
||||
group_info = await bot.get_group_info(group_id=group_id)
|
||||
except ActionFailed:
|
||||
group_info = {
|
||||
"group_id": group_id,
|
||||
"group_name": "_",
|
||||
"max_member_count": -1,
|
||||
"member_count": -1,
|
||||
}
|
||||
await GroupInfo.add_group_info(
|
||||
group_info["group_id"],
|
||||
group_info["group_name"],
|
||||
group_info["max_member_count"],
|
||||
group_info["member_count"],
|
||||
1,
|
||||
)
|
||||
await GroupInfo.update_or_create(
|
||||
group_id=group_info["group_id"],
|
||||
defaults={
|
||||
"group_flag": 1,
|
||||
"group_name": group_info["group_name"],
|
||||
"max_member_count": group_info["max_member_count"],
|
||||
"member_count": group_info["member_count"],
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
await group_auth.send(f"添加群认证 {group_id} 发生错误!")
|
||||
logger.error(f"添加群认证发生错误", cmd, target=group_id, e=e)
|
||||
else:
|
||||
await group_auth.send(f"已为 {group_id} {cmd[:2]}群认证..")
|
||||
logger.info(f"添加群认证成功", cmd, target=group_id)
|
||||
else:
|
||||
if group := await GroupInfo.filter(group_id=group_id).first():
|
||||
await group.update_or_create(
|
||||
group_id=group_id, defaults={"group_flag": 0}
|
||||
)
|
||||
await group_auth.send(f"已删除 {group_id} 群认证..")
|
||||
logger.info(f"删除群认证成功", cmd, target=group_id)
|
||||
else:
|
||||
await group_auth.send(f"未查找到群聊: {group_id}")
|
||||
logger.info(f"未找到群聊", cmd, target=group_id)
|
||||
else:
|
||||
if await GroupInfo.get_group_info(group_id):
|
||||
await GroupInfo.set_group_flag(group_id, 0)
|
||||
await group_auth.send(
|
||||
f'已为 {group_id} {cmd[:2]}群认证..'
|
||||
)
|
||||
logger.debug(f"群号不合法或不存在", cmd, target=group_id)
|
||||
error_group.append(str(group_id))
|
||||
if error_group:
|
||||
await manager_group_whitelist.send("以下群聊不合法或不存在:\n" + "\n".join(error_group))
|
||||
|
||||
@@ -1,45 +1,37 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import to_me
|
||||
from utils.manager import (
|
||||
plugins2cd_manager,
|
||||
plugins2settings_manager,
|
||||
plugins2block_manager,
|
||||
group_manager,
|
||||
)
|
||||
|
||||
from configs.config import Config
|
||||
from services.log import logger
|
||||
from utils.manager import (
|
||||
group_manager,
|
||||
plugins2block_manager,
|
||||
plugins2cd_manager,
|
||||
plugins2settings_manager,
|
||||
)
|
||||
from utils.utils import scheduler
|
||||
|
||||
|
||||
__zx_plugin_name__ = "重载插件配置 [Superuser]"
|
||||
__zx_plugin_name__ = "重载配置 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
重载插件配置
|
||||
重载配置
|
||||
plugins2settings,
|
||||
plugins2cd
|
||||
plugins2block
|
||||
group_manager
|
||||
指令:
|
||||
重载插件配置
|
||||
重载配置
|
||||
""".strip()
|
||||
__plugin_des__ = "重载插件配置"
|
||||
__plugin_des__ = "重载配置"
|
||||
__plugin_cmd__ = [
|
||||
"重载插件配置",
|
||||
"重载配置",
|
||||
]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_version__ = 0.2
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_configs__ = {
|
||||
"AUTO_RELOAD": {
|
||||
"value": False,
|
||||
"help": "自动重载配置文件",
|
||||
"default_value": False
|
||||
},
|
||||
"AUTO_RELOAD_TIME": {
|
||||
"value": 180,
|
||||
"help": "控制自动重载配置文件时长",
|
||||
"default_value": 180
|
||||
}
|
||||
"AUTO_RELOAD": {"value": False, "help": "自动重载配置文件", "default_value": False, "type": bool},
|
||||
"AUTO_RELOAD_TIME": {"value": 180, "help": "控制自动重载配置文件时长", "default_value": 180, "type": int},
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +51,7 @@ async def _():
|
||||
|
||||
|
||||
@scheduler.scheduled_job(
|
||||
'interval',
|
||||
"interval",
|
||||
seconds=Config.get_config("reload_setting", "AUTO_RELOAD_TIME", 180),
|
||||
)
|
||||
async def _():
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
|
||||
from nonebot.exception import ActionFailed
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from models.level_user import LevelUser
|
||||
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, Message, GroupMessageEvent
|
||||
from utils.utils import get_message_at, is_number
|
||||
from services.log import logger
|
||||
from utils.depends import AtList, OneCommand
|
||||
from utils.message_builder import at
|
||||
from nonebot.params import Command, CommandArg
|
||||
from typing import Tuple
|
||||
from utils.utils import is_number
|
||||
|
||||
__zx_plugin_name__ = "用户权限管理 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
@@ -16,12 +20,14 @@ usage:
|
||||
添加权限 [at] [权限]
|
||||
添加权限 [qq] [group_id] [权限]
|
||||
删除权限 [at]
|
||||
删除权限 [qq] [group_id]
|
||||
""".strip()
|
||||
__plugin_des__ = "增删改用户的权限"
|
||||
__plugin_cmd__ = [
|
||||
"添加权限 [at] [权限]",
|
||||
"添加权限 [qq] [group_id] [权限]",
|
||||
"删除权限 [at]",
|
||||
"删除权限 [qq] [group_id]",
|
||||
]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
@@ -40,21 +46,22 @@ super_cmd = on_command(
|
||||
async def _(
|
||||
bot: Bot,
|
||||
event: MessageEvent,
|
||||
cmd: Tuple[str, ...] = Command(),
|
||||
cmd: str = OneCommand(),
|
||||
arg: Message = CommandArg(),
|
||||
at_list: List[int] = AtList(),
|
||||
):
|
||||
group_id = event.group_id if isinstance(event, GroupMessageEvent) else -1
|
||||
level = None
|
||||
args = arg.extract_plain_text().strip().split()
|
||||
qq = get_message_at(event.json())
|
||||
flag = 2
|
||||
qq = None
|
||||
try:
|
||||
if qq:
|
||||
qq = qq[0]
|
||||
if cmd[0][:2] == "添加" and args and is_number(args[0]):
|
||||
if at_list:
|
||||
qq = at_list[0]
|
||||
if cmd[:2] == "添加" and args and is_number(args[0]):
|
||||
level = int(args[0])
|
||||
else:
|
||||
if cmd[0][:2] == "添加":
|
||||
if cmd[:2] == "添加":
|
||||
if (
|
||||
len(args) > 2
|
||||
and is_number(args[0])
|
||||
@@ -69,33 +76,39 @@ async def _(
|
||||
qq = int(args[0])
|
||||
group_id = int(args[1])
|
||||
flag = 1
|
||||
level = -1 if cmd[0][:2] == "删除" else level
|
||||
level = -1 if cmd[:2] == "删除" else level
|
||||
if group_id == -1 or not level or not qq:
|
||||
raise IndexError()
|
||||
except IndexError:
|
||||
await super_cmd.finish(__plugin_usage__)
|
||||
if not qq:
|
||||
await super_cmd.finish("未指定对象...")
|
||||
try:
|
||||
if cmd[0][:2] == "添加":
|
||||
if await LevelUser.set_level(qq, group_id, level, 1):
|
||||
result = f"添加管理成功, 权限: {level}"
|
||||
else:
|
||||
result = f"管理已存在, 更新权限: {level}"
|
||||
if cmd[:2] == "添加":
|
||||
await LevelUser.set_level(qq, group_id, level, 1)
|
||||
result = f"设置权限成功, 权限: {level}"
|
||||
else:
|
||||
if await LevelUser.delete_level(qq, event.group_id):
|
||||
if await LevelUser.delete_level(qq, group_id):
|
||||
result = "删除管理成功!"
|
||||
else:
|
||||
result = "该账号无管理权限!"
|
||||
if flag == 2:
|
||||
await super_cmd.send(result)
|
||||
elif flag == 1:
|
||||
await bot.send_group_msg(
|
||||
group_id=group_id,
|
||||
message=Message(
|
||||
f"{at(qq)}管理员修改了你的权限"
|
||||
f"\n--------\n你当前的权限等级:{level if level != -1 else 0}"
|
||||
),
|
||||
)
|
||||
try:
|
||||
await bot.send_group_msg(
|
||||
group_id=group_id,
|
||||
message=Message(
|
||||
f"{at(qq)}管理员修改了你的权限"
|
||||
f"\n--------\n你当前的权限等级:{level if level != -1 else 0}"
|
||||
),
|
||||
)
|
||||
except ActionFailed:
|
||||
pass
|
||||
await super_cmd.send("修改成功")
|
||||
logger.info(
|
||||
f"修改权限: {level if level != -1 else 0}", cmd, event.user_id, group_id, qq
|
||||
)
|
||||
except Exception as e:
|
||||
await super_cmd.send("执行指令失败!")
|
||||
logger.error(f"执行指令失败 e:{e}")
|
||||
logger.error(f"执行指令失败", cmd, event.user_id, group_id, qq, e=e)
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, Message
|
||||
from nonebot.rule import to_me
|
||||
from utils.utils import is_number
|
||||
from services.log import logger
|
||||
from utils.manager import group_manager
|
||||
from nonebot.params import Command, CommandArg
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
__zx_plugin_name__ = "超级用户被动开关 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
超级用户被动开关
|
||||
指令:
|
||||
开启/关闭广播通知
|
||||
""".strip()
|
||||
__plugin_des__ = "超级用户被动开关"
|
||||
__plugin_cmd__ = [
|
||||
"开启/关闭广播通知",
|
||||
]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
|
||||
oc_gb = on_command(
|
||||
"开启广播通知",
|
||||
aliases={"关闭广播通知"},
|
||||
rule=to_me(),
|
||||
permission=SUPERUSER,
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
|
||||
@oc_gb.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, cmd: Tuple[str, ...] = Command(), arg: Message = CommandArg()):
|
||||
cmd = cmd[0]
|
||||
group = arg.extract_plain_text().strip()
|
||||
if group:
|
||||
if is_number(group):
|
||||
group = int(group)
|
||||
for g in await bot.get_group_list():
|
||||
if g["group_id"] == group:
|
||||
break
|
||||
else:
|
||||
await oc_gb.finish("没有加入这个群...", at_sender=True)
|
||||
if cmd == "开启广播通知":
|
||||
logger.info(f"USER {event.user_id} 开启了 GROUP {group} 的广播")
|
||||
await oc_gb.finish(await group_manager.open_group_task(group, "broadcast",), at_sender=True)
|
||||
else:
|
||||
logger.info(f"USER {event.user_id} 关闭了 GROUP {group} 的广播")
|
||||
await oc_gb.finish(await group_manager.close_group_task(group, "broadcast"), at_sender=True)
|
||||
else:
|
||||
await oc_gb.finish("请输入正确的群号", at_sender=True)
|
||||
else:
|
||||
await oc_gb.finish("请输入要关闭广播的群号", at_sender=True)
|
||||
@@ -1,11 +1,11 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.onebot.v11 import Bot, MessageEvent
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import to_me
|
||||
from utils.utils import get_bot
|
||||
from services.log import logger
|
||||
from models.group_info import GroupInfo
|
||||
from models.friend_user import FriendUser
|
||||
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_info import GroupInfo
|
||||
from services.log import logger
|
||||
|
||||
__zx_plugin_name__ = "更新群/好友信息 [Superuser]"
|
||||
__plugin_usage__ = """
|
||||
@@ -32,39 +32,46 @@ update_friend_info = on_command(
|
||||
|
||||
|
||||
@update_group_info.handle()
|
||||
async def _():
|
||||
bot = get_bot()
|
||||
async def _(bot: Bot, event: MessageEvent):
|
||||
gl = await bot.get_group_list()
|
||||
gl = [g["group_id"] for g in gl]
|
||||
num = 0
|
||||
rst = ""
|
||||
for g in gl:
|
||||
group_info = await bot.get_group_info(group_id=g)
|
||||
if await GroupInfo.add_group_info(
|
||||
group_info["group_id"],
|
||||
group_info["group_name"],
|
||||
group_info["max_member_count"],
|
||||
group_info["member_count"],
|
||||
1
|
||||
):
|
||||
try:
|
||||
group_info = await bot.get_group_info(group_id=g)
|
||||
await GroupInfo.update_or_create(
|
||||
group_id=group_info["group_id"],
|
||||
defaults={
|
||||
"group_name": group_info["group_name"],
|
||||
"max_member_count": group_info["max_member_count"],
|
||||
"member_count": group_info["member_count"],
|
||||
},
|
||||
)
|
||||
num += 1
|
||||
logger.info(f"自动更新群组 {g} 信息成功")
|
||||
else:
|
||||
logger.info(f"自动更新群组 {g} 信息失败")
|
||||
rst += f"{g} 更新失败\n"
|
||||
await update_group_info.send(f"成功更新了 {num} 个群的信息\n{rst[:-1]}")
|
||||
logger.debug(
|
||||
"群聊信息更新成功", "更新群信息", event.user_id, target=group_info["group_id"]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"更新群聊信息失败", "更新群信息", event.user_id, target=g, e=e)
|
||||
await update_group_info.send(f"成功更新了 {len(gl)} 个群的信息")
|
||||
logger.info(f"更新群聊信息完成,共更新了 {len(gl)} 个群的信息", "更新群信息", event.user_id)
|
||||
|
||||
|
||||
@update_friend_info.handle()
|
||||
async def _():
|
||||
async def _(bot: Bot, event: MessageEvent):
|
||||
num = 0
|
||||
rst = ""
|
||||
fl = await get_bot().get_friend_list()
|
||||
error_list = []
|
||||
fl = await bot.get_friend_list()
|
||||
for f in fl:
|
||||
if await FriendUser.add_friend_info(f["user_id"], f["nickname"]):
|
||||
logger.info(f'自动更新好友 {f["user_id"]} 信息成功')
|
||||
try:
|
||||
await FriendUser.update_or_create(
|
||||
user_id=f["user_id"], defaults={"nickname": f["nickname"]}
|
||||
)
|
||||
logger.debug(f"更新好友信息成功", "更新好友信息", event.user_id, target=f["user_id"])
|
||||
num += 1
|
||||
else:
|
||||
logger.warning(f'自动更新好友 {f["user_id"]} 信息失败')
|
||||
rst += f'{f["user_id"]} 更新失败\n'
|
||||
await update_friend_info.send(f"成功更新了 {num} 个好友的信息\n{rst[:-1]}")
|
||||
except Exception as e:
|
||||
logger.error(f"更新好友信息失败", "更新好友信息", event.user_id, target=f["user_id"], e=e)
|
||||
await update_friend_info.send(f"成功更新了 {num} 个好友的信息")
|
||||
if error_list:
|
||||
await update_friend_info.send(f"以下好友更新失败:\n" + "\n".join(error_list))
|
||||
logger.info(f"更新好友信息完成,共更新了 {num} 个群的信息", "更新好友信息", event.user_id)
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.rule import to_me
|
||||
from configs.path_config import IMAGE_PATH
|
||||
|
||||
from utils.message_builder import image
|
||||
from .data_source import create_help_image
|
||||
|
||||
from .data_source import SUPERUSER_HELP_IMAGE, create_help_image
|
||||
|
||||
__zx_plugin_name__ = "超级用户帮助 [Superuser]"
|
||||
|
||||
|
||||
__zx_plugin_name__ = '超级用户帮助 [Superuser]'
|
||||
|
||||
|
||||
superuser_help_image = IMAGE_PATH / 'superuser_help.png'
|
||||
|
||||
if superuser_help_image.exists():
|
||||
superuser_help_image.unlink()
|
||||
if SUPERUSER_HELP_IMAGE.exists():
|
||||
SUPERUSER_HELP_IMAGE.unlink()
|
||||
|
||||
super_help = on_command(
|
||||
"超级用户帮助", rule=to_me(), priority=1, permission=SUPERUSER, block=True
|
||||
@@ -21,7 +19,6 @@ super_help = on_command(
|
||||
|
||||
@super_help.handle()
|
||||
async def _():
|
||||
if not superuser_help_image.exists():
|
||||
if not SUPERUSER_HELP_IMAGE.exists():
|
||||
await create_help_image()
|
||||
x = image(superuser_help_image)
|
||||
await super_help.finish(x)
|
||||
await super_help.finish(image(SUPERUSER_HELP_IMAGE))
|
||||
|
||||
@@ -1,79 +1,84 @@
|
||||
from utils.image_utils import BuildImage
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from services.log import logger
|
||||
from utils.utils import get_matchers
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
from nonebot import Driver
|
||||
import asyncio
|
||||
import nonebot
|
||||
|
||||
import nonebot
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from nonebot import Driver
|
||||
from services.log import logger
|
||||
from utils.image_template import help_template
|
||||
from utils.image_utils import (BuildImage, build_sort_image, group_image,
|
||||
text2image)
|
||||
from utils.manager import plugin_data_manager
|
||||
from utils.manager.models import PluginType
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
background = IMAGE_PATH / "background" / "0.png"
|
||||
|
||||
superuser_help_image = IMAGE_PATH / "superuser_help.png"
|
||||
SUPERUSER_HELP_IMAGE = IMAGE_PATH / "superuser_help.png"
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def create_help_image(bot: Bot = None):
|
||||
async def create_help_image():
|
||||
"""
|
||||
创建超级用户帮助图片
|
||||
"""
|
||||
await asyncio.get_event_loop().run_in_executor(None, _create_help_image)
|
||||
|
||||
|
||||
def _create_help_image():
|
||||
"""
|
||||
创建管理员帮助图片
|
||||
"""
|
||||
_matchers = get_matchers()
|
||||
_plugin_name_list = []
|
||||
width = 0
|
||||
help_str = "超级用户帮助\n\n* 注: ‘*’ 代表可有多个相同参数 ‘?’ 代表可省略该参数 *\n\n"
|
||||
tmp_img = BuildImage(0, 0, plain_text='1', font_size=24)
|
||||
for matcher in _matchers:
|
||||
plugin_name = ""
|
||||
if SUPERUSER_HELP_IMAGE.exists():
|
||||
return
|
||||
plugin_data_ = plugin_data_manager.get_data()
|
||||
image_list = []
|
||||
task_list = []
|
||||
for plugin_data in [
|
||||
plugin_data_[x]
|
||||
for x in plugin_data_
|
||||
if plugin_data_[x].name != "超级用户帮助 [Superuser]"
|
||||
]:
|
||||
try:
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
_module = _plugin.module
|
||||
try:
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
except AttributeError:
|
||||
continue
|
||||
is_superuser_usage = False
|
||||
try:
|
||||
_ = _module.__getattribute__("__plugin_superuser_usage__")
|
||||
is_superuser_usage = True
|
||||
except AttributeError:
|
||||
pass
|
||||
if (
|
||||
("[superuser]" in plugin_name.lower() or is_superuser_usage)
|
||||
and plugin_name != "超级用户帮助 [Superuser]"
|
||||
and plugin_name not in _plugin_name_list
|
||||
and "[hidden]" not in plugin_name.lower()
|
||||
):
|
||||
_plugin_name_list.append(plugin_name)
|
||||
try:
|
||||
plugin_des = _module.__getattribute__("__plugin_des__")
|
||||
except AttributeError:
|
||||
plugin_des = '_'
|
||||
plugin_cmd = _module.__getattribute__("__plugin_cmd__")
|
||||
if is_superuser_usage:
|
||||
plugin_cmd = [x for x in plugin_cmd if "[_superuser]" in x]
|
||||
plugin_cmd = " / ".join(plugin_cmd).replace('[_superuser]', '').strip()
|
||||
help_str += f"{plugin_des} -> {plugin_cmd}\n\n"
|
||||
x = tmp_img.getsize(f"{plugin_des} -> {plugin_cmd}")[0]
|
||||
width = width if width > x else x
|
||||
if plugin_data.plugin_type in [PluginType.SUPERUSER, PluginType.ADMIN]:
|
||||
usage = None
|
||||
if (
|
||||
plugin_data.plugin_type == PluginType.SUPERUSER
|
||||
and plugin_data.usage
|
||||
):
|
||||
usage = await text2image(
|
||||
plugin_data.usage, padding=5, color=(204, 196, 151)
|
||||
)
|
||||
if plugin_data.superuser_usage:
|
||||
usage = await text2image(
|
||||
plugin_data.superuser_usage, padding=5, color=(204, 196, 151)
|
||||
)
|
||||
if usage:
|
||||
await usage.acircle_corner()
|
||||
image = await help_template(plugin_data.name, usage)
|
||||
image_list.append(image)
|
||||
if plugin_data.task:
|
||||
for x in plugin_data.task.keys():
|
||||
task_list.append(plugin_data.task[x])
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"获取超级用户插件 {matcher.plugin_name}: {plugin_name} 设置失败... {type(e)}:{e}"
|
||||
f"获取超级用户插件 {plugin_data.model}: {plugin_data.name} 设置失败... {type(e)}:{e}"
|
||||
)
|
||||
height = len(help_str.split("\n")) * 33
|
||||
width += 500
|
||||
A = BuildImage(width, height, font_size=24)
|
||||
_background = BuildImage(width, height, background=background)
|
||||
A.text((300, 140), help_str)
|
||||
A.paste(_background, alpha=True)
|
||||
A.save(superuser_help_image)
|
||||
logger.info(f"已成功加载 {len(_plugin_name_list)} 条超级用户命令")
|
||||
task_str = "\n".join(task_list)
|
||||
task_str = "通过私聊 开启被动/关闭被动 + [被动名称] 来控制全局被动\n----------\n" + task_str
|
||||
task_image = await text2image(
|
||||
task_str, padding=5, color=(204, 196, 151)
|
||||
)
|
||||
task_image = await help_template("被动任务", task_image)
|
||||
image_list.append(task_image)
|
||||
image_group, _ = group_image(image_list)
|
||||
A = await build_sort_image(image_group, color="#f9f6f2", padding_top=180)
|
||||
await A.apaste(
|
||||
BuildImage(0, 0, font="CJGaoDeGuo.otf", plain_text="超级用户帮助", font_size=50),
|
||||
(50, 30),
|
||||
True,
|
||||
)
|
||||
await A.apaste(
|
||||
BuildImage(
|
||||
0,
|
||||
0,
|
||||
font="CJGaoDeGuo.otf",
|
||||
plain_text="注: ‘*’ 代表可有多个相同参数 ‘?’ 代表可省略该参数",
|
||||
font_size=30,
|
||||
font_color="red",
|
||||
),
|
||||
(50, 90),
|
||||
True,
|
||||
)
|
||||
await A.asave(SUPERUSER_HELP_IMAGE)
|
||||
logger.info(f"已成功加载 {len(image_list)} 条超级用户命令")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from nonebot import on_command
|
||||
from utils.message_builder import image
|
||||
|
||||
from utils.message_builder import image
|
||||
|
||||
__zx_plugin_name__ = "更新信息"
|
||||
__plugin_usage__ = """
|
||||
@@ -26,8 +26,7 @@ update_info = on_command("更新信息", aliases={"更新日志"}, priority=5, b
|
||||
|
||||
@update_info.handle()
|
||||
async def _():
|
||||
img = image("update_info.png")
|
||||
if img:
|
||||
if img := image("update_info.png"):
|
||||
await update_info.finish(image("update_info.png"))
|
||||
else:
|
||||
await update_info.finish("目前没有更新信息哦")
|
||||
|
||||
+15
-3
@@ -1,16 +1,27 @@
|
||||
from typing import Optional
|
||||
import platform
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from .utils import ConfigsManager
|
||||
|
||||
if platform.system() == "Linux":
|
||||
import os
|
||||
|
||||
hostip = (
|
||||
os.popen("cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'")
|
||||
.read()
|
||||
.replace("\n", "")
|
||||
)
|
||||
|
||||
|
||||
# 回复消息名称
|
||||
NICKNAME: str = "小真寻"
|
||||
|
||||
# 数据库(必要)
|
||||
# 如果填写了bind就不需要再填写后面的字段了#)
|
||||
# 示例:"bind": "postgresql://user:password@127.0.0.1:5432/database"
|
||||
# 示例:"bind": "postgres://user:password@127.0.0.1:5432/database"
|
||||
bind: str = "" # 数据库连接链接
|
||||
sql_name: str = "postgresql"
|
||||
sql_name: str = "postgres"
|
||||
user: str = "" # 数据用户名
|
||||
password: str = "" # 数据库密码
|
||||
address: str = "" # 数据库地址
|
||||
@@ -18,6 +29,7 @@ port: str = "" # 数据库端口
|
||||
database: str = "" # 数据库名称
|
||||
|
||||
# 代理,例如 "http://127.0.0.1:7890"
|
||||
# 如果是WLS 可以 f"http://{hostip}:7890" 使用寄主机的代理
|
||||
SYSTEM_PROXY: Optional[str] = None # 全局代理
|
||||
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@ FONT_PATH = Path() / "resources" / "font"
|
||||
DATA_PATH = Path() / "data"
|
||||
# 临时数据路径
|
||||
TEMP_PATH = Path() / "resources" / "temp"
|
||||
# 网页模板路径
|
||||
TEMPLATE_PATH = Path() / "resources" / "template"
|
||||
|
||||
|
||||
def load_path():
|
||||
|
||||
+199
-106
@@ -1,9 +1,53 @@
|
||||
from typing import Optional, Any, Union
|
||||
import copy
|
||||
from pathlib import Path
|
||||
from ruamel.yaml import YAML
|
||||
from typing import Any, Callable, Dict, Optional, Type, Union
|
||||
|
||||
import cattrs
|
||||
from pydantic import BaseModel
|
||||
from ruamel import yaml
|
||||
from ruamel.yaml import YAML
|
||||
from ruamel.yaml.scanner import ScannerError
|
||||
|
||||
from services.log import logger
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
|
||||
"""
|
||||
配置项
|
||||
"""
|
||||
|
||||
value: Any
|
||||
"""配置项值"""
|
||||
name: Optional[str]
|
||||
"""插件名称"""
|
||||
help: Optional[str]
|
||||
"""配置注解"""
|
||||
default_value: Optional[Any] = None
|
||||
"""默认值"""
|
||||
level_module: Optional[str]
|
||||
"""受权限模块"""
|
||||
type: Any = None
|
||||
"""参数类型"""
|
||||
arg_parser: Optional[Callable] = None
|
||||
"""参数解析"""
|
||||
|
||||
|
||||
class ConfigGroup(BaseModel):
|
||||
|
||||
"""
|
||||
配置组
|
||||
"""
|
||||
|
||||
module: str
|
||||
"""模块名"""
|
||||
configs: Dict[str, Config] = {}
|
||||
"""配置项列表"""
|
||||
|
||||
|
||||
class NoSuchConfig(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ConfigsManager:
|
||||
"""
|
||||
@@ -11,35 +55,25 @@ class ConfigsManager:
|
||||
"""
|
||||
|
||||
def __init__(self, file: Path):
|
||||
self._data: dict = {}
|
||||
self._data: Dict[str, ConfigGroup] = {}
|
||||
self._simple_data: dict = {}
|
||||
self._admin_level_data = []
|
||||
self._simple_file = Path() / "configs" / "config.yaml"
|
||||
_yaml = YAML()
|
||||
if file:
|
||||
file.parent.mkdir(exist_ok=True, parents=True)
|
||||
self.file = file
|
||||
_yaml = YAML()
|
||||
if file.exists():
|
||||
with open(file, "r", encoding="utf8") as f:
|
||||
self._data = _yaml.load(f)
|
||||
if not self._data:
|
||||
self.file.unlink()
|
||||
raise ValueError(
|
||||
"配置文件为空!\n"
|
||||
"***********************************************************\n"
|
||||
"****** 配置文件 plugins2config.yaml 为空,已删除,请重启 ******\n"
|
||||
"***********************************************************"
|
||||
)
|
||||
if self._simple_file.exists():
|
||||
try:
|
||||
with open(self._simple_file, "r", encoding="utf8") as f:
|
||||
self._simple_data = _yaml.load(f)
|
||||
except ScannerError as e:
|
||||
raise ScannerError(
|
||||
f"{e}\n**********************************************\n"
|
||||
f"****** 可能为config.yaml配置文件填写不规范 ******\n"
|
||||
f"**********************************************"
|
||||
)
|
||||
self.load_data()
|
||||
if self._simple_file.exists():
|
||||
try:
|
||||
with open(self._simple_file, "r", encoding="utf8") as f:
|
||||
self._simple_data = _yaml.load(f)
|
||||
except ScannerError as e:
|
||||
raise ScannerError(
|
||||
f"{e}\n**********************************************\n"
|
||||
f"****** 可能为config.yaml配置文件填写不规范 ******\n"
|
||||
f"**********************************************"
|
||||
)
|
||||
|
||||
def add_plugin_config(
|
||||
self,
|
||||
@@ -49,7 +83,9 @@ class ConfigsManager:
|
||||
*,
|
||||
name: Optional[str] = None,
|
||||
help_: Optional[str] = None,
|
||||
default_value: Optional[str] = None,
|
||||
default_value: Optional[Any] = None,
|
||||
type: Optional[Type] = None,
|
||||
arg_parser: Optional[Callable] = None,
|
||||
_override: bool = False,
|
||||
):
|
||||
"""
|
||||
@@ -60,79 +96,66 @@ class ConfigsManager:
|
||||
:param name: 插件名称
|
||||
:param help_: 配置注解
|
||||
:param default_value: 默认值
|
||||
:param _override: 覆盖前值
|
||||
:param _override: 强制覆盖值
|
||||
"""
|
||||
if (
|
||||
not (module in self._data.keys() and self._data[module].get(key))
|
||||
or _override
|
||||
):
|
||||
if not module or not key:
|
||||
raise ValueError("add_plugin_config: module和key不能为为空")
|
||||
if module in self._data and (config := self._data[module].configs.get(key)):
|
||||
config.help = help_
|
||||
config.arg_parser = arg_parser
|
||||
config.type = type
|
||||
if _override:
|
||||
config.value = value
|
||||
config.name = name
|
||||
config.default_value = default_value
|
||||
else:
|
||||
_module = None
|
||||
if ":" in module:
|
||||
module = module.split(":")
|
||||
_module = module[-1]
|
||||
module = module[0]
|
||||
module_split = module.split(":")
|
||||
if len(module_split) < 2:
|
||||
raise ValueError(f"module: {module} 填写错误")
|
||||
_module = module_split[-1]
|
||||
module = module_split[0]
|
||||
if "[LEVEL]" in key and _module:
|
||||
key = key.replace("[LEVEL]", "").strip()
|
||||
self._admin_level_data.append((_module, value))
|
||||
if self._data.get(module) is None:
|
||||
self._data[module] = {}
|
||||
key = key.upper()
|
||||
self._data[module][key] = {
|
||||
"value": value,
|
||||
"name": name.strip() if isinstance(name, str) else name,
|
||||
"help": help_.strip() if isinstance(help_, str) else help_,
|
||||
"default_value": default_value,
|
||||
"level_module": _module,
|
||||
}
|
||||
if not self._data.get(module):
|
||||
self._data[module] = ConfigGroup(module=module)
|
||||
self._data[module].configs[key] = Config(
|
||||
value=value,
|
||||
name=name,
|
||||
help=help_,
|
||||
default_value=default_value,
|
||||
level_module=_module,
|
||||
type=type,
|
||||
)
|
||||
|
||||
def remove_plugin_config(self, module: str):
|
||||
"""
|
||||
为插件删除一个配置
|
||||
:param module: 模块名
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
del self._data[module]
|
||||
self.save()
|
||||
|
||||
def set_config(self, module: str, key: str, value: str):
|
||||
def set_config(
|
||||
self,
|
||||
module: str,
|
||||
key: str,
|
||||
value: Any,
|
||||
auto_save: bool = False,
|
||||
save_simple_data: bool = True,
|
||||
):
|
||||
"""
|
||||
设置配置值
|
||||
:param module: 模块名
|
||||
:param key: 配置名称
|
||||
:param value: 值
|
||||
:param auto_save: 自动保存
|
||||
:param save_simple_data: 保存至config.yaml
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
if module in self._data:
|
||||
if (
|
||||
self._data[module].get(key) is not None
|
||||
and self._data[module][key] != value
|
||||
self._data[module].configs.get(key)
|
||||
and self._data[module].configs[key] != value
|
||||
):
|
||||
self._data[module][key]["value"] = value
|
||||
self._data[module].configs[key].value = value
|
||||
self._simple_data[module][key] = value
|
||||
self.save()
|
||||
|
||||
def set_help(self, module: str, key: str, help_: str):
|
||||
"""
|
||||
设置配置注释
|
||||
:param module: 模块名
|
||||
:param key: 配置名称
|
||||
:param help_: 注释文本
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
if self._data[module].get(key) is not None:
|
||||
self._data[module][key]["help"] = help_
|
||||
self.save()
|
||||
|
||||
def set_default_value(self, module: str, key: str, value: str):
|
||||
"""
|
||||
设置配置默认值
|
||||
:param module: 模块名
|
||||
:param key: 配置名称
|
||||
:param value: 值
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
if self._data[module].get(key) is not None:
|
||||
self._data[module][key]["default_value"] = value
|
||||
self.save()
|
||||
if auto_save:
|
||||
self.save(save_simple_data=save_simple_data)
|
||||
|
||||
def get_config(
|
||||
self, module: str, key: str, default: Optional[Any] = None
|
||||
@@ -143,37 +166,68 @@ class ConfigsManager:
|
||||
:param key: 配置名称
|
||||
:param default: 没有key值内容的默认返回值
|
||||
"""
|
||||
logger.debug(
|
||||
f"尝试获取配置 MODULE: [<u><y>{module}</y></u>] | KEY: [<u><y>{key}</y></u>]"
|
||||
)
|
||||
key = key.upper()
|
||||
value = None
|
||||
if module in self._data.keys():
|
||||
for key in [key, f"{key} [LEVEL]"]:
|
||||
if self._data[module].get(key) is not None:
|
||||
if self._data[module][key]["value"] is None:
|
||||
return self._data[module][key]["default_value"]
|
||||
return self._data[module][key]["value"]
|
||||
if default is not None:
|
||||
return default
|
||||
return None
|
||||
config = self._data[module].configs.get(key)
|
||||
if not config:
|
||||
config = self._data[module].configs.get(f"{key} [LEVEL]")
|
||||
if not config:
|
||||
raise NoSuchConfig(f"未查询到配置项 MODULE: [ {module} ] | KEY: [ {key} ]")
|
||||
if config.arg_parser:
|
||||
value = config.arg_parser(value or config.default_value)
|
||||
else:
|
||||
try:
|
||||
if config.value is not None:
|
||||
value = (
|
||||
cattrs.structure(config.value, config.type)
|
||||
if config.type
|
||||
else config.value
|
||||
)
|
||||
else:
|
||||
if config.default_value is not None:
|
||||
value = (
|
||||
cattrs.structure(config.default_value, config.type)
|
||||
if config.type
|
||||
else config.default_value
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"配置项类型转换 MODULE: [<u><y>{module}</y></u>] | KEY: [<u><y>{key}</y></u>]",
|
||||
e=e,
|
||||
)
|
||||
value = config.value or config.default_value
|
||||
if value is None:
|
||||
value = default
|
||||
logger.debug(
|
||||
f"获取配置 MODULE: [<u><y>{module}</y></u>] | KEY: [<u><y>{key}</y></u>] -> [<u><c>{value}</c></u>]"
|
||||
)
|
||||
return value
|
||||
|
||||
def get_level2module(self, module: str, key: str) -> Optional[str]:
|
||||
"""
|
||||
获取指定key所绑定的module,一般为权限等级
|
||||
获取指定key所绑定的module,一般为权限等级
|
||||
:param module: 模块名
|
||||
:param key: 配置名称
|
||||
:return:
|
||||
"""
|
||||
if self._data.get(module) is not None:
|
||||
if self._data[module].get(key) is not None:
|
||||
return self._data[module][key].get("level_module")
|
||||
if config := self._data[module].configs.get(key):
|
||||
return config.level_module
|
||||
|
||||
def get(self, key: str):
|
||||
def get(self, key: str) -> Optional[ConfigGroup]:
|
||||
"""
|
||||
获取插件配置数据
|
||||
:param key: 名称
|
||||
"""
|
||||
if key in self._data.keys():
|
||||
return self._data[key]
|
||||
return self._data.get(key)
|
||||
|
||||
def save(self, path: Union[str, Path] = None, save_simple_data: bool = False):
|
||||
def save(
|
||||
self, path: Optional[Union[str, Path]] = None, save_simple_data: bool = False
|
||||
):
|
||||
"""
|
||||
保存数据
|
||||
:param path: 路径
|
||||
@@ -188,10 +242,17 @@ class ConfigsManager:
|
||||
Dumper=yaml.RoundTripDumper,
|
||||
allow_unicode=True,
|
||||
)
|
||||
path = path if path else self.file
|
||||
path = path or self.file
|
||||
data = {}
|
||||
for module in self._data:
|
||||
data[module] = {}
|
||||
for config in self._data[module].configs:
|
||||
value = self._data[module].configs[config].dict()
|
||||
del value["type"]
|
||||
data[module][config] = value
|
||||
with open(path, "w", encoding="utf8") as f:
|
||||
yaml.dump(
|
||||
self._data, f, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
data, f, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
)
|
||||
|
||||
def reload(self):
|
||||
@@ -199,21 +260,53 @@ class ConfigsManager:
|
||||
重新加载配置文件
|
||||
"""
|
||||
_yaml = YAML()
|
||||
temp_file = Path() / "configs" / "config.yaml"
|
||||
if temp_file.exists():
|
||||
with open(temp_file, "r", encoding="utf8") as f:
|
||||
temp = _yaml.load(f)
|
||||
for key in temp.keys():
|
||||
for k in temp[key].keys():
|
||||
self._data[key][k]["value"] = temp[key][k]
|
||||
if self._simple_file.exists():
|
||||
with open(self._simple_file, "r", encoding="utf8") as f:
|
||||
self._simple_data = _yaml.load(f)
|
||||
for key in self._simple_data.keys():
|
||||
for k in self._simple_data[key].keys():
|
||||
self._data[key].configs[k].value = self._simple_data[key][k]
|
||||
self.save()
|
||||
|
||||
def load_data(self):
|
||||
"""
|
||||
加载数据
|
||||
|
||||
Raises:
|
||||
ValueError: _description_
|
||||
"""
|
||||
if self.file.exists():
|
||||
_yaml = YAML()
|
||||
with open(self.file, "r", encoding="utf8") as f:
|
||||
temp_data = _yaml.load(f)
|
||||
if not temp_data:
|
||||
self.file.unlink()
|
||||
raise ValueError(
|
||||
"配置文件为空!\n"
|
||||
"***********************************************************\n"
|
||||
"****** 配置文件 plugins2config.yaml 为空,已删除,请重启 ******\n"
|
||||
"***********************************************************"
|
||||
)
|
||||
count = 0
|
||||
for module in temp_data:
|
||||
config_group = ConfigGroup(module=module)
|
||||
for config in temp_data[module]:
|
||||
config_group.configs[config] = Config(**temp_data[module][config])
|
||||
count += 1
|
||||
self._data[module] = config_group
|
||||
logger.info(
|
||||
f"加载配置完成,共加载 <u><y>{len(temp_data)}</y></u> 个配置组及对应 <u><y>{count}</y></u> 个配置项"
|
||||
)
|
||||
|
||||
def get_admin_level_data(self):
|
||||
"""
|
||||
获取管理插件等级
|
||||
"""
|
||||
return self._admin_level_data
|
||||
|
||||
def get_data(self) -> Dict[str, ConfigGroup]:
|
||||
return copy.deepcopy(self._data)
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
return not bool(self._data)
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"抽卡":{
|
||||
"buff": ["欧气满满,十连出金","出金不歪"],
|
||||
"debuff": ["武器大师","保底出金","金色会是痛苦大剑"]
|
||||
},
|
||||
"刷世界boss":{
|
||||
"buff": ["双攻双爆角斗士"],
|
||||
"debuff": ["只有保底材料","贪生怕死角斗士"]
|
||||
},
|
||||
"刷风本":{
|
||||
"buff": ["会有极品猎人套","会掉真正的少女心","治疗加成少女头"],
|
||||
"debuff": ["勇往直前少女心","少女飘摇的杀意","少女暴怒的容颜"]
|
||||
},
|
||||
"刷火本":{
|
||||
"buff": ["魔女帽子火伤杯","暴伤魔女帽!","火伤魔女心!"],
|
||||
"debuff": ["幡 然 醒 悟","这么阴间的地方真的会有魔女套吗?","不务正业火魔女","会匹配到3个卢姥爷"]
|
||||
},
|
||||
"刷岩本":{
|
||||
"buff": ["悠久的磐岩伴你左右","岩神的庇护常在"],
|
||||
"debuff": ["防御流星杯,你值得拥有"]
|
||||
},
|
||||
"刷宗室":{
|
||||
"buff": ["物理伤害骑士道,元素精通宗室套"],
|
||||
"debuff": ["贪生怕死骑士道,物理伤害宗室杯"]
|
||||
},
|
||||
"刷冰本":{
|
||||
"buff": ["双暴词条概率up"],
|
||||
"debuff": ["防御力船帽,无人可及"]
|
||||
},
|
||||
"刷雷本":{
|
||||
"buff": ["愿雷鸟伴你左右"],
|
||||
"debuff": ["来表演一个只掉平雷套的绝活","风神忽悠雷凶兆"]
|
||||
},
|
||||
"锄大地":{
|
||||
"buff": ["会掉一大堆紫色材料"],
|
||||
"debuff": ["深渊法师爱你哟","会被冰水法控到死"]
|
||||
},
|
||||
"挖矿":{
|
||||
"buff": ["开矿出双材料"],
|
||||
"debuff": ["去别人世界会被拒"]
|
||||
},
|
||||
"刷天赋本":{
|
||||
"buff": ["金色!我看到了金色的书!"],
|
||||
"debuff": ["2蓝2绿不会变"]
|
||||
},
|
||||
"刷突破材料":{
|
||||
"buff": ["金色!我看到了金色的材料!"],
|
||||
"debuff": ["2蓝2绿不会变"]
|
||||
},
|
||||
"升级圣遗物":{
|
||||
"buff": ["稀有词条跳跳跳","会双爆拉满"],
|
||||
"debuff": ["女 仆 狂 喜","无中生有防御力","生命拉满","完美避开双爆"]
|
||||
},
|
||||
"打风魔龙":{
|
||||
"buff": ["看我一箭一个风魔鸡","今天特瓦林可以给想要的突破材料","5金加原胚!"],
|
||||
"debuff": ["会不小心掉下平台","不小心被地板烫死了"]
|
||||
},
|
||||
"打狼王":{
|
||||
"buff": ["今天安德琉斯的心情不错,可以py一下","5金加原胚!"],
|
||||
"debuff": ["狼尾巴*1"]
|
||||
},
|
||||
"打公子":{
|
||||
"buff": ["今天可以和公子py想要的突破材料","5金加原胚!"],
|
||||
"debuff": ["要角没有!要命一条!"]
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 772 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 799 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.3 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 694 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 694 KiB After Width: | Height: | Size: 326 KiB |
+96
-153
@@ -1,40 +1,48 @@
|
||||
from services.db_context import db
|
||||
from typing import Dict
|
||||
from typing import Optional, List
|
||||
from services.log import logger
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
from .goods_info import GoodsInfo
|
||||
|
||||
|
||||
class BagUser(db.Model):
|
||||
__tablename__ = "bag_users"
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False)
|
||||
group_id = db.Column(db.BigInteger(), nullable=False)
|
||||
gold = db.Column(db.Integer(), default=100)
|
||||
props = db.Column(db.TEXT(), nullable=False, default="") # 旧道具字段(废弃)
|
||||
spend_total_gold = db.Column(db.Integer(), default=0)
|
||||
get_total_gold = db.Column(db.Integer(), default=0)
|
||||
get_today_gold = db.Column(db.Integer(), default=0)
|
||||
spend_today_gold = db.Column(db.Integer(), default=0)
|
||||
property = db.Column(db.JSON(), nullable=False, default={}) # 新道具字段
|
||||
class BagUser(Model):
|
||||
|
||||
_idx1 = db.Index("bag_group_users_idx1", "user_qq", "group_id", unique=True)
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
"""用户id"""
|
||||
group_id = fields.BigIntField()
|
||||
"""群聊id"""
|
||||
gold = fields.IntField(default=100)
|
||||
"""金币数量"""
|
||||
spend_total_gold = fields.IntField(default=0)
|
||||
"""花费金币总数"""
|
||||
get_total_gold = fields.IntField(default=0)
|
||||
"""获取金币总数"""
|
||||
get_today_gold = fields.IntField(default=0)
|
||||
"""今日获取金币"""
|
||||
spend_today_gold = fields.IntField(default=0)
|
||||
"""今日获取金币"""
|
||||
property: Dict[str, int] = fields.JSONField(default={})
|
||||
"""道具"""
|
||||
|
||||
class Meta:
|
||||
table = "bag_users"
|
||||
table_description = "用户道具数据表"
|
||||
unique_together = ("user_qq", "group_id")
|
||||
|
||||
@classmethod
|
||||
async def get_user_total_gold(cls, user_qq: int, group_id: int) -> str:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取金币概况
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 所在群号
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
user = await query.gino.first()
|
||||
if not user:
|
||||
user = await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
)
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
return (
|
||||
f"当前金币:{user.gold}\n今日获取金币:{user.get_today_gold}\n今日花费金币:{user.spend_today_gold}"
|
||||
f"\n今日收益:{user.get_today_gold - user.spend_today_gold}"
|
||||
@@ -44,183 +52,118 @@ class BagUser(db.Model):
|
||||
@classmethod
|
||||
async def get_gold(cls, user_qq: int, group_id: int) -> int:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取当前金币
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 所在群号
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
return user.gold
|
||||
else:
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
)
|
||||
return 100
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
return user.gold
|
||||
|
||||
@classmethod
|
||||
async def get_property(cls, user_qq: int, group_id: int) -> Dict[str, int]:
|
||||
async def get_property(
|
||||
cls, user_qq: int, group_id: int, only_active: bool = False
|
||||
) -> Dict[str, int]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取当前道具
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 所在群号
|
||||
:param only_active: 仅仅获取主动使用的道具
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
return user.property
|
||||
else:
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
)
|
||||
return {}
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
if only_active and user.property:
|
||||
data = {}
|
||||
name_list = [
|
||||
x.goods_name
|
||||
for x in await GoodsInfo.get_all_goods()
|
||||
if not x.is_passive
|
||||
]
|
||||
for key in [x for x in user.property if x in name_list]:
|
||||
data[key] = user.property[key]
|
||||
return data
|
||||
return user.property
|
||||
|
||||
@classmethod
|
||||
async def add_gold(cls, user_qq: int, group_id: int, num: int):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
增加金币
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 所在群号
|
||||
:param num: 金币数量
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
await user.update(
|
||||
gold=user.gold + num,
|
||||
get_total_gold=user.get_total_gold + num,
|
||||
get_today_gold=user.get_today_gold + num,
|
||||
).apply()
|
||||
else:
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
gold=100 + num,
|
||||
get_total_gold=num,
|
||||
get_today_gold=num,
|
||||
)
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
user.gold = user.gold + num
|
||||
user.get_total_gold = user.get_total_gold + num
|
||||
user.get_today_gold = user.get_today_gold + num
|
||||
await user.save(update_fields=["gold", "get_today_gold", "get_total_gold"])
|
||||
|
||||
@classmethod
|
||||
async def spend_gold(cls, user_qq: int, group_id: int, num: int):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
花费金币
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 所在群号
|
||||
:param num: 金币数量
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
await user.update(
|
||||
gold=user.gold - num,
|
||||
spend_total_gold=user.spend_total_gold + num,
|
||||
spend_today_gold=user.spend_today_gold + num,
|
||||
).apply()
|
||||
else:
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
gold=100 - num,
|
||||
spend_total_gold=num,
|
||||
spend_today_gold=num,
|
||||
)
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
user.gold = user.gold - num
|
||||
user.spend_total_gold = user.spend_total_gold + num
|
||||
user.spend_today_gold = user.spend_today_gold + num
|
||||
await user.save(update_fields=["gold", "spend_total_gold", "spend_today_gold"])
|
||||
|
||||
@classmethod
|
||||
async def add_property(cls, user_qq: int, group_id: int, name: str):
|
||||
async def add_property(cls, user_qq: int, group_id: int, name: str, num: int = 1):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
增加道具
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 所在群号
|
||||
:param name: 道具名称
|
||||
:param num: 道具数量
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
p = user.property
|
||||
if p.get(name) is None:
|
||||
p[name] = 1
|
||||
else:
|
||||
p[name] += 1
|
||||
await user.update(property=p).apply()
|
||||
else:
|
||||
await cls.create(user_qq=user_qq, group_id=group_id, property={name: 1})
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
property_ = user.property
|
||||
if property_.get(name) is None:
|
||||
property_[name] = 0
|
||||
property_[name] += num
|
||||
user.property = property_
|
||||
await user.save(update_fields=["property"])
|
||||
|
||||
@classmethod
|
||||
async def delete_property(
|
||||
cls, user_qq: int, group_id: int, name: str, num: int = 1
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
使用/删除 道具
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 所在群号
|
||||
:param name: 道具名称
|
||||
:param num: 使用个数
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
property_ = user.property
|
||||
if name in property_:
|
||||
if property_.get(name) == num:
|
||||
del property_[name]
|
||||
else:
|
||||
property_[name] -= num
|
||||
await user.update(property=property_).apply()
|
||||
return True
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
property_ = user.property
|
||||
if name in property_:
|
||||
if (n := property_.get(name, 0)) < num:
|
||||
return False
|
||||
if n == num:
|
||||
del property_[name]
|
||||
else:
|
||||
property_[name] -= num
|
||||
await user.save(update_fields=["property"])
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def buy_property(
|
||||
cls, user_qq: int, group_id: int, goods: "GoodsInfo", goods_num: int
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
购买道具
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param group_id: 所在群聊
|
||||
:param goods: 商品
|
||||
:param goods_num: 商品数量
|
||||
"""
|
||||
try:
|
||||
# 折扣后金币
|
||||
spend_gold = goods.goods_discount * goods.goods_price * goods_num
|
||||
await BagUser.spend_gold(user_qq, group_id, spend_gold)
|
||||
for _ in range(goods_num):
|
||||
await BagUser.add_property(user_qq, group_id, goods.goods_name)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"buy_property 发生错误 {type(e)}:{e}")
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_all_users(cls, group_id: Optional[int] = None) -> List["BagUser"]:
|
||||
"""
|
||||
说明:
|
||||
获取所有用户数据
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
if not group_id:
|
||||
query = await cls.query.gino.all()
|
||||
else:
|
||||
query = await cls.query.where((cls.group_id == group_id)).gino.all()
|
||||
return query
|
||||
async def _run_script(cls):
|
||||
await cls.raw("ALTER TABLE bag_users DROP props;")
|
||||
"""删除 props 字段"""
|
||||
|
||||
+70
-65
@@ -1,119 +1,124 @@
|
||||
from services.db_context import db
|
||||
import time
|
||||
from typing import Union
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
from services.log import logger
|
||||
|
||||
|
||||
class BanUser(db.Model):
|
||||
__tablename__ = "ban_users"
|
||||
class BanUser(Model):
|
||||
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False, primary_key=True)
|
||||
ban_level = db.Column(db.Integer(), nullable=False)
|
||||
ban_time = db.Column(db.BigInteger())
|
||||
duration = db.Column(db.BigInteger())
|
||||
user_qq = fields.IntField(pk=True)
|
||||
"""用户id"""
|
||||
ban_level = fields.IntField()
|
||||
"""使用ban命令的用户等级"""
|
||||
ban_time = fields.BigIntField()
|
||||
"""ban开始的时间"""
|
||||
duration = fields.BigIntField()
|
||||
"""ban时长"""
|
||||
|
||||
_idx1 = db.Index("ban_group_users_idx1", "user_qq", unique=True)
|
||||
class Meta:
|
||||
table = "ban_users"
|
||||
table_description = ".ban/b了 封禁人员数据表"
|
||||
|
||||
@classmethod
|
||||
async def check_ban_level(cls, user_qq: int, level: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
检测ban掉目标的用户与unban用户的权限等级大小
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: unban用户的qq号
|
||||
:param level: ban掉目标用户的权限等级
|
||||
"""
|
||||
user = await cls.query.where((cls.user_qq == user_qq)).gino.first()
|
||||
if not user:
|
||||
return False
|
||||
if user.ban_level > level:
|
||||
return True
|
||||
user = await cls.filter(user_qq=user_qq).first()
|
||||
if user:
|
||||
logger.debug(
|
||||
f"检测用户被ban等级,user_level: {user.ban_level},level: {level}",
|
||||
target=user_qq,
|
||||
)
|
||||
return bool(user and user.ban_level > level)
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def check_ban_time(cls, user_qq: int) -> str:
|
||||
async def check_ban_time(cls, user_qq: int) -> Union[str, int]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
检测用户被ban时长
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq))
|
||||
user = await query.gino.first()
|
||||
if not user:
|
||||
return ""
|
||||
if time.time() - (user.ban_time + user.duration) > 0 and user.duration != -1:
|
||||
return ""
|
||||
if user.duration == -1:
|
||||
return "∞"
|
||||
return time.time() - user.ban_time - user.duration
|
||||
logger.debug(f"获取用户ban时长", target=user_qq)
|
||||
if user := await cls.filter(user_qq=user_qq).first():
|
||||
if (
|
||||
time.time() - (user.ban_time + user.duration) > 0
|
||||
and user.duration != -1
|
||||
):
|
||||
return ""
|
||||
if user.duration == -1:
|
||||
return "∞"
|
||||
return int(time.time() - user.ban_time - user.duration)
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
async def is_ban(cls, user_qq: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
判断用户是否被ban
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
"""
|
||||
logger.debug(f"检测是否被ban", target=user_qq)
|
||||
if await cls.check_ban_time(user_qq):
|
||||
return True
|
||||
else:
|
||||
await cls.unban(user_qq)
|
||||
return False
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def is_super_ban(cls, user_qq: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
判断用户是否被ban
|
||||
参数:
|
||||
说明:
|
||||
判断用户是否被超级用户ban / b了
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
"""
|
||||
user = await cls.query.where((cls.user_qq == user_qq)).gino.first()
|
||||
if not user:
|
||||
return False
|
||||
if user.ban_level == 10:
|
||||
return True
|
||||
logger.debug(f"检测是否被超级用户权限封禁", target=user_qq)
|
||||
if user := await cls.filter(user_qq=user_qq).first():
|
||||
if user.ban_level == 10:
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def ban(cls, user_qq: int, ban_level: int, duration: int) -> bool:
|
||||
async def ban(cls, user_qq: int, ban_level: int, duration: int):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
ban掉目标用户
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: 目标用户qq号
|
||||
:param ban_level: 使用ban命令用户的权限
|
||||
:param duration: ban时长,秒
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
if not await cls.check_ban_time(user_qq):
|
||||
logger.debug(f"封禁用户,等级:{ban_level},时长: {duration}", target=user_qq)
|
||||
if await cls.filter(user_qq=user_qq).first():
|
||||
await cls.unban(user_qq)
|
||||
user = None
|
||||
if user is None:
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
ban_level=ban_level,
|
||||
ban_time=time.time(),
|
||||
duration=duration,
|
||||
)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
ban_level=ban_level,
|
||||
ban_time=time.time(),
|
||||
duration=duration,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def unban(cls, user_qq: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
unban用户
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
if user is None:
|
||||
return False
|
||||
else:
|
||||
await cls.delete.where((cls.user_qq == user_qq)).gino.status()
|
||||
if user := await cls.filter(user_qq=user_qq).first():
|
||||
logger.debug("解除封禁", target=user_qq)
|
||||
await user.delete()
|
||||
return True
|
||||
return False
|
||||
|
||||
+63
-160
@@ -1,81 +1,30 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import List, Literal, Optional, Tuple, Union
|
||||
from typing import Any, List, Literal, Optional, Tuple, Union
|
||||
|
||||
from services.db_context import db
|
||||
from tortoise import fields
|
||||
from tortoise.functions import Count
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class ChatHistory(db.Model):
|
||||
__tablename__ = "chat_history"
|
||||
class ChatHistory(Model):
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False)
|
||||
group_id = db.Column(db.BigInteger())
|
||||
text = db.Column(db.Text())
|
||||
plain_text = db.Column(db.Text())
|
||||
create_time = db.Column(db.DateTime(timezone=True), nullable=False)
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
"""用户id"""
|
||||
group_id = fields.BigIntField(null=True)
|
||||
"""群聊id"""
|
||||
text = fields.TextField(null=True)
|
||||
"""文本内容"""
|
||||
plain_text = fields.TextField(null=True)
|
||||
"""纯文本"""
|
||||
create_time = fields.DatetimeField(auto_now_add=True)
|
||||
"""创建时间"""
|
||||
|
||||
@classmethod
|
||||
async def add_chat_msg(cls, user_qq: int, group_id: Optional[int], text: str, plain_text: str):
|
||||
await cls.create(
|
||||
user_qq=user_qq, group_id=group_id, text=text, plain_text=plain_text, create_time=datetime.now()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_user_msg(
|
||||
cls,
|
||||
uid: int,
|
||||
msg_type: Optional[Literal["private", "group"]],
|
||||
days: Optional[int] = None,
|
||||
) -> List["ChatHistory"]:
|
||||
"""
|
||||
说明:
|
||||
获取用户消息
|
||||
参数:
|
||||
:param uid: 用户qq
|
||||
:param msg_type: 消息类型,私聊或群聊
|
||||
:param days: 限制日期
|
||||
"""
|
||||
return await cls._get_msg(uid, None, "user", msg_type, days).gino.all()
|
||||
|
||||
@classmethod
|
||||
async def get_group_user_msg(
|
||||
cls,
|
||||
uid: int,
|
||||
gid: int,
|
||||
limit: int = 10,
|
||||
date_scope: Tuple[datetime, datetime] = None,
|
||||
) -> List["ChatHistory"]:
|
||||
"""
|
||||
说明:
|
||||
获取群聊指定用户聊天记录
|
||||
参数:
|
||||
:param uid: qq
|
||||
:param gid: 群号
|
||||
:param limit: 获取数量
|
||||
:param date_scope: 日期范围,默认None为全搜索
|
||||
"""
|
||||
return (
|
||||
await cls._get_msg(uid, gid, "group", days=date_scope)
|
||||
.limit(limit)
|
||||
.gino.all()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_group_user_msg_count(cls, uid: int, gid: int) -> Optional[int]:
|
||||
"""
|
||||
说明:
|
||||
查询群聊指定用户的聊天记录数量
|
||||
参数:
|
||||
:param uid: qq
|
||||
:param gid: 群号
|
||||
"""
|
||||
if x := await db.first(
|
||||
db.text(
|
||||
f"SELECT COUNT(id) as sum FROM public.chat_history WHERE user_qq = {uid} AND group_id = {gid}"
|
||||
)
|
||||
):
|
||||
return x[0]
|
||||
return None
|
||||
class Meta:
|
||||
table = "chat_history"
|
||||
table_description = "聊天记录数据表"
|
||||
|
||||
@classmethod
|
||||
async def get_group_msg_rank(
|
||||
@@ -84,131 +33,85 @@ class ChatHistory(db.Model):
|
||||
limit: int = 10,
|
||||
order: str = "DESC",
|
||||
date_scope: Optional[Tuple[datetime, datetime]] = None,
|
||||
) -> Optional[Tuple[int, int]]:
|
||||
) -> List["ChatHistory"]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取排行数据
|
||||
参数:
|
||||
参数:
|
||||
:param gid: 群号
|
||||
:param limit: 获取数量
|
||||
:param order: 排序类型,desc,des
|
||||
:param date_scope: 日期范围
|
||||
"""
|
||||
sql = f"SELECT user_qq, COUNT(id) as sum FROM public.chat_history WHERE group_id = {gid} "
|
||||
o = "-" if order == "DESC" else ""
|
||||
query = cls.filter(group_id=gid)
|
||||
if date_scope:
|
||||
sql += f"AND create_time BETWEEN '{date_scope[0]}' AND '{date_scope[1]}' "
|
||||
sql += f"GROUP BY user_qq ORDER BY sum {order if order and order.upper() != 'DES' else ''} LIMIT {limit}"
|
||||
return await db.all(db.text(sql))
|
||||
query = query.filter(create_time__range=date_scope)
|
||||
return list(
|
||||
await query.annotate(count=Count("user_qq"))
|
||||
.order_by(o + "count")
|
||||
.group_by("user_qq")
|
||||
.limit(limit)
|
||||
.values_list("user_qq", "count")
|
||||
) # type: ignore
|
||||
|
||||
@classmethod
|
||||
async def get_group_first_msg_datetime(cls, gid: int) -> Optional[datetime]:
|
||||
async def get_group_first_msg_datetime(cls, group_id: int) -> Optional[datetime]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取群第一条记录消息时间
|
||||
参数:
|
||||
:param gid:
|
||||
参数:
|
||||
:param group_id: 群聊id
|
||||
"""
|
||||
if (
|
||||
msg := await cls.query.where(cls.group_id == gid)
|
||||
.order_by(cls.create_time)
|
||||
.gino.first()
|
||||
message := await cls.filter(group_id=group_id)
|
||||
.order_by("create_time")
|
||||
.first()
|
||||
):
|
||||
return msg.create_time
|
||||
return None
|
||||
return message.create_time
|
||||
|
||||
@classmethod
|
||||
async def get_user_msg_count(
|
||||
cls,
|
||||
uid: int,
|
||||
msg_type: Optional[Literal["private", "group"]],
|
||||
days: Optional[int] = None,
|
||||
) -> int:
|
||||
"""
|
||||
说明:
|
||||
获取用户消息数量
|
||||
参数:
|
||||
:param uid: 用户qq
|
||||
:param msg_type: 消息类型,私聊或群聊
|
||||
:param days: 限制日期
|
||||
"""
|
||||
return (
|
||||
await cls._get_msg(uid, None, "user", msg_type, days, True).gino.first()
|
||||
)[0]
|
||||
|
||||
@classmethod
|
||||
async def get_group_msg(
|
||||
cls,
|
||||
gid: int,
|
||||
days: Optional[int] = None,
|
||||
) -> List["ChatHistory"]:
|
||||
"""
|
||||
说明:
|
||||
获取群聊消息
|
||||
参数:
|
||||
:param gid: 用户qq
|
||||
:param days: 限制日期
|
||||
"""
|
||||
return await cls._get_msg(None, gid, "group", None, days).gino.all()
|
||||
|
||||
@classmethod
|
||||
async def get_group_msg_count(
|
||||
cls,
|
||||
gid: int,
|
||||
days: Optional[int] = None,
|
||||
) -> List["ChatHistory"]:
|
||||
"""
|
||||
说明:
|
||||
获取群聊消息数量
|
||||
参数:
|
||||
:param gid: 用户qq
|
||||
:param days: 限制日期
|
||||
"""
|
||||
return (await cls._get_msg(None, gid, "group", None, days, True).gino.first())[
|
||||
0
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _get_msg(
|
||||
async def get_message(
|
||||
cls,
|
||||
uid: Optional[int],
|
||||
gid: Optional[int],
|
||||
type_: Literal["user", "group"],
|
||||
msg_type: Optional[Literal["private", "group"]] = None,
|
||||
days: Optional[Union[int, Tuple[datetime, datetime]]] = None,
|
||||
is_select_count: bool = False,
|
||||
):
|
||||
) -> List["ChatHistory"]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取消息查询query
|
||||
参数:
|
||||
参数:
|
||||
:param uid: 用户qq
|
||||
:param gid: 群号
|
||||
:param type_: 类型,私聊或群聊
|
||||
:param msg_type: 消息类型,用户或群聊
|
||||
:param days: 限制日期
|
||||
"""
|
||||
if is_select_count:
|
||||
setattr(ChatHistory, "count", db.func.count(cls.id).label("count"))
|
||||
query = cls.select("count")
|
||||
else:
|
||||
query = cls.query
|
||||
if type_ == "user":
|
||||
query = query.where(cls.user_qq == uid)
|
||||
query = cls.filter(user_qq=uid)
|
||||
if msg_type == "private":
|
||||
query = query.where(cls.group_id == None)
|
||||
query = query.filter(group_id__isnull=True)
|
||||
elif msg_type == "group":
|
||||
query = query.where(cls.group_id != None)
|
||||
query = query.filter(group_id__not_isnull=True)
|
||||
else:
|
||||
query = query.where(cls.group_id == gid)
|
||||
query = cls.filter(group_id=gid)
|
||||
if uid:
|
||||
query = query.where(cls.user_qq == uid)
|
||||
query = query.filter(user_qq=uid)
|
||||
if days:
|
||||
if isinstance(days, int):
|
||||
query = query.where(
|
||||
cls.create_time >= datetime.now() - timedelta(days=days)
|
||||
query = query.filter(
|
||||
create_time__gte=datetime.now() - timedelta(days=days)
|
||||
)
|
||||
elif isinstance(days, tuple):
|
||||
query = query.where(cls.create_time >= days[0]).where(
|
||||
cls.create_time <= days[1]
|
||||
)
|
||||
return query
|
||||
query = query.filter(create_time__range=days)
|
||||
return await query.all() # type: ignore
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
"alter table chat_history alter group_id drop not null;", # 允许 group_id 为空
|
||||
"alter table chat_history alter text drop not null;", # 允许 text 为空
|
||||
"alter table chat_history alter plain_text drop not null;", # 允许 plain_text 为空
|
||||
]
|
||||
|
||||
+28
-83
@@ -1,115 +1,60 @@
|
||||
from services.db_context import db
|
||||
from tortoise import fields
|
||||
|
||||
from configs.config import Config
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class FriendUser(db.Model):
|
||||
__tablename__ = "friend_users"
|
||||
class FriendUser(Model):
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
user_id = db.Column(db.BigInteger(), nullable=False)
|
||||
user_name = db.Column(db.Unicode(), nullable=False, default="")
|
||||
nickname = db.Column(db.Unicode())
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_id = fields.BigIntField(unique=True)
|
||||
"""用户id"""
|
||||
user_name = fields.CharField(max_length=255, default="")
|
||||
"""用户名称"""
|
||||
nickname = fields.CharField(max_length=255, null=True)
|
||||
"""私聊下自定义昵称"""
|
||||
|
||||
_idx1 = db.Index("friend_users_idx1", "user_id", unique=True)
|
||||
class Meta:
|
||||
table = "friend_users"
|
||||
table_description = "好友信息数据表"
|
||||
|
||||
@classmethod
|
||||
async def get_user_name(cls, user_id: int) -> str:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取好友用户名称
|
||||
参数:
|
||||
参数:
|
||||
:param user_id: qq号
|
||||
"""
|
||||
query = cls.query.where(cls.user_id == user_id)
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
if user := await cls.get_or_none(user_id=user_id):
|
||||
return user.user_name
|
||||
else:
|
||||
return ""
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
async def add_friend_info(cls, user_id: int, user_name: str) -> bool:
|
||||
async def get_user_nickname(cls, user_id: int) -> str:
|
||||
"""
|
||||
说明:
|
||||
添加好友信息
|
||||
参数:
|
||||
:param user_id: qq号
|
||||
:param user_name: 用户名称
|
||||
"""
|
||||
try:
|
||||
query = cls.query.where(cls.user_id == user_id)
|
||||
user = await query.with_for_update().gino.first()
|
||||
if not user:
|
||||
await cls.create(
|
||||
user_id=user_id,
|
||||
user_name=user_name,
|
||||
)
|
||||
else:
|
||||
await user.update(
|
||||
user_name=user_name,
|
||||
).apply()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def delete_friend_info(cls, user_id: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
删除好友信息
|
||||
参数:
|
||||
:param user_id: qq号
|
||||
"""
|
||||
try:
|
||||
query = cls.query.where(cls.user_id == user_id)
|
||||
user = await query.with_for_update().gino.first()
|
||||
if user:
|
||||
await user.delete()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_friend_nickname(cls, user_id: int) -> str:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取用户昵称
|
||||
参数:
|
||||
参数:
|
||||
:param user_id: qq号
|
||||
"""
|
||||
query = cls.query.where(cls.user_id == user_id)
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
if user := await cls.get_or_none(user_id=user_id):
|
||||
if user.nickname:
|
||||
_tmp = ""
|
||||
black_word = Config.get_config("nickname", "BLACK_WORD")
|
||||
if black_word:
|
||||
if black_word := Config.get_config("nickname", "BLACK_WORD"):
|
||||
for x in user.nickname:
|
||||
_tmp += "*" if x in black_word else x
|
||||
return _tmp
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
async def set_friend_nickname(cls, user_id: int, nickname: str) -> bool:
|
||||
async def set_user_nickname(cls, user_id: int, nickname: str):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
设置用户昵称
|
||||
参数:
|
||||
参数:
|
||||
:param user_id: qq号
|
||||
:param nickname: 昵称
|
||||
"""
|
||||
try:
|
||||
query = cls.query.where(cls.user_id == user_id)
|
||||
user = await query.with_for_update().gino.first()
|
||||
if not user:
|
||||
await cls.create(
|
||||
user_id=user_id,
|
||||
nickname=nickname,
|
||||
)
|
||||
else:
|
||||
await user.update(
|
||||
nickname=nickname,
|
||||
).apply()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
await cls.update_or_create(user_id=user_id, defaults={"nickname": nickname})
|
||||
|
||||
+136
-72
@@ -1,19 +1,37 @@
|
||||
from services.db_context import db
|
||||
from typing import Optional, List
|
||||
from services.log import logger
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class GoodsInfo(db.Model):
|
||||
class GoodsInfo(Model):
|
||||
__tablename__ = "goods_info"
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
goods_name = db.Column(db.TEXT(), nullable=False) # 名称
|
||||
goods_price = db.Column(db.Integer(), nullable=False) # 价格
|
||||
goods_description = db.Column(db.TEXT(), nullable=False) # 商品描述
|
||||
goods_discount = db.Column(db.Numeric(scale=3, asdecimal=False), default=1) # 打折
|
||||
goods_limit_time = db.Column(db.BigInteger(), default=0) # 限时
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
goods_name = fields.CharField(255, unique=True)
|
||||
"""商品名称"""
|
||||
goods_price = fields.IntField()
|
||||
"""价格"""
|
||||
goods_description = fields.TextField()
|
||||
"""描述"""
|
||||
goods_discount = fields.FloatField(default=1)
|
||||
"""折扣"""
|
||||
goods_limit_time = fields.BigIntField(default=0)
|
||||
"""限时"""
|
||||
daily_limit = fields.IntField(default=0)
|
||||
"""每日限购"""
|
||||
daily_purchase_limit: Dict[str, Dict[str, int]] = fields.JSONField(default={})
|
||||
"""用户限购记录"""
|
||||
is_passive = fields.BooleanField(default=False)
|
||||
"""是否为被动道具"""
|
||||
icon = fields.TextField(null=True)
|
||||
"""图标路径"""
|
||||
|
||||
_idx1 = db.Index("goods_group_users_idx1", "goods_name", unique=True)
|
||||
class Meta:
|
||||
table = "goods_info"
|
||||
table_description = "商品数据表"
|
||||
|
||||
@classmethod
|
||||
async def add_goods(
|
||||
@@ -23,48 +41,47 @@ class GoodsInfo(db.Model):
|
||||
goods_description: str,
|
||||
goods_discount: float = 1,
|
||||
goods_limit_time: int = 0,
|
||||
) -> bool:
|
||||
daily_limit: int = 0,
|
||||
is_passive: bool = False,
|
||||
icon: Optional[str] = None,
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
添加商品
|
||||
参数:
|
||||
参数:
|
||||
:param goods_name: 商品名称
|
||||
:param goods_price: 商品价格
|
||||
:param goods_description: 商品简介
|
||||
:param goods_discount: 商品折扣
|
||||
:param goods_limit_time: 商品限时
|
||||
:param daily_limit: 每日购买限制
|
||||
:param is_passive: 是否为被动道具
|
||||
:param icon: 图标
|
||||
"""
|
||||
try:
|
||||
if not await cls.get_goods_info(goods_name):
|
||||
await cls.create(
|
||||
goods_name=goods_name,
|
||||
goods_price=goods_price,
|
||||
goods_description=goods_description,
|
||||
goods_discount=goods_discount,
|
||||
goods_limit_time=goods_limit_time,
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"GoodsInfo add_goods 发生错误 {type(e)}:{e}")
|
||||
return False
|
||||
if not await cls.filter(goods_name=goods_name).first():
|
||||
await cls.create(
|
||||
goods_name=goods_name,
|
||||
goods_price=goods_price,
|
||||
goods_description=goods_description,
|
||||
goods_discount=goods_discount,
|
||||
goods_limit_time=goods_limit_time,
|
||||
daily_limit=daily_limit,
|
||||
is_passive=is_passive,
|
||||
icon=icon,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def delete_goods(cls, goods_name: str) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
删除商品
|
||||
参数:
|
||||
参数:
|
||||
:param goods_name: 商品名称
|
||||
"""
|
||||
query = (
|
||||
await cls.query.where(cls.goods_name == goods_name)
|
||||
.with_for_update()
|
||||
.gino.first()
|
||||
)
|
||||
if not query:
|
||||
return False
|
||||
await query.delete()
|
||||
return True
|
||||
if goods := await cls.get_or_none(goods_name=goods_name):
|
||||
await goods.delete()
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def update_goods(
|
||||
@@ -74,55 +91,50 @@ class GoodsInfo(db.Model):
|
||||
goods_description: Optional[str] = None,
|
||||
goods_discount: Optional[float] = None,
|
||||
goods_limit_time: Optional[int] = None,
|
||||
) -> bool:
|
||||
daily_limit: Optional[int] = None,
|
||||
is_passive: Optional[bool] = None,
|
||||
icon: Optional[str] = None,
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
更新商品信息
|
||||
参数:
|
||||
参数:
|
||||
:param goods_name: 商品名称
|
||||
:param goods_price: 商品价格
|
||||
:param goods_description: 商品简介
|
||||
:param goods_discount: 商品折扣
|
||||
:param goods_limit_time: 商品限时时间
|
||||
:param daily_limit: 每日次数限制
|
||||
:param is_passive: 是否为被动
|
||||
:param icon: 图标
|
||||
"""
|
||||
try:
|
||||
query = (
|
||||
await cls.query.where(cls.goods_name == goods_name)
|
||||
.with_for_update()
|
||||
.gino.first()
|
||||
if goods := await cls.get_or_none(goods_name=goods_name):
|
||||
await cls.update_or_create(
|
||||
goods_name=goods_name,
|
||||
defaults={
|
||||
"goods_price": goods_price or goods.goods_price,
|
||||
"goods_description": goods_description or goods.goods_description,
|
||||
"goods_discount": goods_discount or goods.goods_discount,
|
||||
"goods_limit_time": goods_limit_time
|
||||
if goods_limit_time is not None
|
||||
else goods.goods_limit_time,
|
||||
"daily_limit": daily_limit
|
||||
if daily_limit is not None
|
||||
else goods.daily_limit,
|
||||
"is_passive": is_passive
|
||||
if is_passive is not None
|
||||
else goods.is_passive,
|
||||
"icon": icon or goods.icon,
|
||||
},
|
||||
)
|
||||
if not query:
|
||||
return False
|
||||
if goods_price:
|
||||
await query.update(goods_price=goods_price).apply()
|
||||
if goods_description:
|
||||
await query.update(goods_description=goods_description).apply()
|
||||
if goods_discount:
|
||||
await query.update(goods_discount=goods_discount).apply()
|
||||
if goods_limit_time:
|
||||
await query.update(goods_limit_time=goods_limit_time).apply()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"GoodsInfo update_goods 发生错误 {type(e)}:{e}")
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_goods_info(cls, goods_name: str) -> "GoodsInfo":
|
||||
"""
|
||||
说明:
|
||||
获取商品对象
|
||||
参数:
|
||||
:param goods_name: 商品名称
|
||||
"""
|
||||
return await cls.query.where(cls.goods_name == goods_name).gino.first()
|
||||
|
||||
@classmethod
|
||||
async def get_all_goods(cls) -> List["GoodsInfo"]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获得全部有序商品对象
|
||||
"""
|
||||
query = await cls.query.gino.all()
|
||||
query = await cls.all()
|
||||
id_lst = [x.id for x in query]
|
||||
goods_lst = []
|
||||
for _ in range(len(query)):
|
||||
@@ -130,3 +142,55 @@ class GoodsInfo(db.Model):
|
||||
goods_lst.append([x for x in query if x.id == min_id][0])
|
||||
id_lst.remove(min_id)
|
||||
return goods_lst
|
||||
|
||||
@classmethod
|
||||
async def add_user_daily_purchase(
|
||||
cls, goods: "GoodsInfo", user_id_: int, group_id_: int, num: int = 1
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
添加用户明日购买限制
|
||||
参数:
|
||||
:param goods: 商品
|
||||
:param user_id: 用户id
|
||||
:param group_id: 群号
|
||||
:param num: 数量
|
||||
"""
|
||||
user_id = str(user_id_)
|
||||
group_id = str(group_id_)
|
||||
if goods and goods.daily_limit and goods.daily_limit > 0:
|
||||
if not goods.daily_purchase_limit.get(group_id):
|
||||
goods.daily_purchase_limit[group_id] = {}
|
||||
if not goods.daily_purchase_limit[group_id].get(user_id):
|
||||
goods.daily_purchase_limit[group_id][user_id] = 0
|
||||
goods.daily_purchase_limit[group_id][user_id] += num
|
||||
await goods.save(update_fields=["daily_purchase_limit"])
|
||||
|
||||
@classmethod
|
||||
async def check_user_daily_purchase(
|
||||
cls, goods: "GoodsInfo", user_id_: int, group_id_: int, num: int = 1
|
||||
) -> Tuple[bool, int]:
|
||||
"""
|
||||
说明:
|
||||
检测用户每日购买上限
|
||||
参数:
|
||||
:param goods: 商品
|
||||
:param user_id: 用户id
|
||||
:param group_id: 群号
|
||||
:param num: 数量
|
||||
"""
|
||||
user_id = str(user_id_)
|
||||
group_id = str(group_id_)
|
||||
if goods and goods.daily_limit > 0:
|
||||
if (
|
||||
not goods.daily_limit
|
||||
or not goods.daily_purchase_limit.get(group_id)
|
||||
or not goods.daily_purchase_limit[group_id].get(user_id)
|
||||
):
|
||||
return goods.daily_limit - num < 0, goods.daily_limit
|
||||
if goods.daily_purchase_limit[group_id][user_id] + num > goods.daily_limit:
|
||||
return (
|
||||
True,
|
||||
goods.daily_limit - goods.daily_purchase_limit[group_id][user_id],
|
||||
)
|
||||
return False, 0
|
||||
|
||||
+17
-104
@@ -1,111 +1,24 @@
|
||||
from services.db_context import db
|
||||
from services.log import logger
|
||||
from typing import List, Optional
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
class GroupInfo(db.Model):
|
||||
__tablename__ = "group_info"
|
||||
from services.db_context import Model
|
||||
from services.log import logger
|
||||
|
||||
group_id = db.Column(db.BigInteger(), nullable=False, primary_key=True)
|
||||
group_name = db.Column(db.Unicode(), nullable=False, default="")
|
||||
max_member_count = db.Column(db.Integer(), nullable=False, default=0)
|
||||
member_count = db.Column(db.Integer(), nullable=False, default=0)
|
||||
group_flag = db.Column(db.Integer(), nullable=False, default=0)
|
||||
|
||||
_idx1 = db.Index("group_info_idx1", "group_id", unique=True)
|
||||
class GroupInfo(Model):
|
||||
|
||||
@classmethod
|
||||
async def get_group_info(cls, group_id: int) -> "GroupInfo":
|
||||
"""
|
||||
说明:
|
||||
获取群信息
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
query = cls.query.where(cls.group_id == group_id)
|
||||
return await query.gino.first()
|
||||
group_id = fields.BigIntField(pk=True)
|
||||
"""群聊id"""
|
||||
group_name = fields.TextField(default="")
|
||||
"""群聊名称"""
|
||||
max_member_count = fields.IntField(default=0)
|
||||
"""最大人数"""
|
||||
member_count = fields.IntField(default=0)
|
||||
"""当前人数"""
|
||||
group_flag: int = fields.IntField(default=0)
|
||||
"""群认证标记"""
|
||||
|
||||
@classmethod
|
||||
async def add_group_info(
|
||||
cls,
|
||||
group_id: int,
|
||||
group_name: str,
|
||||
max_member_count: int,
|
||||
member_count: int,
|
||||
group_flag: Optional[int] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
添加群信息
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
:param group_name: 群名称
|
||||
:param max_member_count: 群员最大数量
|
||||
:param member_count: 群员数量
|
||||
:param group_flag: 群认证,0为未认证,1为认证
|
||||
"""
|
||||
try:
|
||||
group = (
|
||||
await cls.query.where(cls.group_id == group_id)
|
||||
.with_for_update()
|
||||
.gino.first()
|
||||
)
|
||||
if group:
|
||||
await group.update(
|
||||
group_name=group_name,
|
||||
max_member_count=max_member_count,
|
||||
member_count=member_count,
|
||||
).apply()
|
||||
if group_flag is not None:
|
||||
await group.update(group_flag=group_flag).apply()
|
||||
else:
|
||||
await cls.create(
|
||||
group_id=group_id,
|
||||
group_name=group_name,
|
||||
max_member_count=max_member_count,
|
||||
member_count=member_count,
|
||||
group_flag=group_flag,
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.info(f"GroupInfo 调用 add_group_info 发生错误 {type(e)}:{e}")
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def delete_group_info(cls, group_id: int):
|
||||
"""
|
||||
说明:
|
||||
删除群信息
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
await cls.delete.where(cls.group_id == group_id).gino.status()
|
||||
|
||||
@classmethod
|
||||
async def get_all_group(cls) -> List["GroupInfo"]:
|
||||
"""
|
||||
说明:
|
||||
获取所有群对象
|
||||
"""
|
||||
query = await cls.query.gino.all()
|
||||
return query
|
||||
|
||||
@classmethod
|
||||
async def set_group_flag(cls, group_id: int, group_flag: int) -> bool:
|
||||
"""
|
||||
设置群认证
|
||||
:param group_id: 群号
|
||||
:param group_flag: 群认证,0为未认证,1为认证
|
||||
"""
|
||||
group = (
|
||||
await cls.query.where(cls.group_id == group_id)
|
||||
.with_for_update()
|
||||
.gino.first()
|
||||
)
|
||||
if group:
|
||||
if group.group_flag != group_flag:
|
||||
await group.update(
|
||||
group_flag=group_flag,
|
||||
).apply()
|
||||
return True
|
||||
return False
|
||||
class Meta:
|
||||
table = "group_info"
|
||||
table_description = "群聊信息表"
|
||||
|
||||
+71
-149
@@ -1,195 +1,117 @@
|
||||
from datetime import datetime
|
||||
from typing import List, Optional, Set
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from configs.config import Config
|
||||
from services.db_context import db
|
||||
from typing import List, Optional
|
||||
from services.db_context import Model
|
||||
from services.log import logger
|
||||
|
||||
|
||||
class GroupInfoUser(db.Model):
|
||||
__tablename__ = "group_info_users"
|
||||
class GroupInfoUser(Model):
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False)
|
||||
user_name = db.Column(db.Unicode(), nullable=False)
|
||||
group_id = db.Column(db.BigInteger(), nullable=False)
|
||||
user_join_time = db.Column(db.DateTime(), nullable=False)
|
||||
nickname = db.Column(db.Unicode())
|
||||
uid = db.Column(db.BigInteger())
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
"""用户id"""
|
||||
user_name = fields.CharField(255, default="")
|
||||
"""用户昵称"""
|
||||
group_id = fields.BigIntField()
|
||||
"""群聊id"""
|
||||
user_join_time: datetime = fields.DatetimeField(null=True)
|
||||
"""用户入群时间"""
|
||||
nickname = fields.CharField(255, null=True)
|
||||
"""群聊昵称"""
|
||||
uid = fields.BigIntField(null=True)
|
||||
"""用户uid"""
|
||||
|
||||
_idx1 = db.Index("info_group_users_idx1", "user_qq", "group_id", unique=True)
|
||||
class Meta:
|
||||
table = "group_info_users"
|
||||
table_description = "群员信息数据表"
|
||||
unique_together = ("user_qq", "group_id")
|
||||
|
||||
@classmethod
|
||||
async def add_member_info(
|
||||
cls,
|
||||
user_qq: int,
|
||||
group_id: int,
|
||||
user_name: str,
|
||||
user_join_time: datetime,
|
||||
uid: Optional[int] = None,
|
||||
) -> bool:
|
||||
async def get_group_member_id_list(cls, group_id: int) -> Set[int]:
|
||||
"""
|
||||
说明:
|
||||
添加群内用户信息
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
:param user_name: 用户名称
|
||||
:param user_join_time: 入群时间
|
||||
:param uid: 用户唯一 id(自动生成)
|
||||
"""
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
)
|
||||
try:
|
||||
if not await query.gino.first():
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
user_name=user_name,
|
||||
group_id=group_id,
|
||||
user_join_time=user_join_time,
|
||||
uid=uid
|
||||
)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_member_info(
|
||||
cls, user_qq: int, group_id: int
|
||||
) -> "GroupInfoUser":
|
||||
"""
|
||||
说明:
|
||||
查询群员信息
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
"""
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
)
|
||||
return await query.gino.first()
|
||||
|
||||
@classmethod
|
||||
async def delete_member_info(cls, user_qq: int, group_id: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
删除群员信息
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
"""
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
)
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
try:
|
||||
if user is None:
|
||||
return True
|
||||
else:
|
||||
await cls.delete.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
).gino.status()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_group_member_id_list(cls, group_id: int) -> List[int]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取该群所有用户qq
|
||||
参数:
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
member_list = []
|
||||
query = cls.query.where((cls.group_id == group_id))
|
||||
for user in await query.gino.all():
|
||||
member_list.append(user.user_qq)
|
||||
return member_list
|
||||
return set(
|
||||
await cls.filter(group_id=group_id).values_list("user_qq", flat=True)
|
||||
) # type: ignore
|
||||
|
||||
@classmethod
|
||||
async def set_group_member_nickname(
|
||||
cls, user_qq: int, group_id: int, nickname: str
|
||||
) -> bool:
|
||||
async def set_user_nickname(cls, user_qq: int, group_id: int, nickname: str):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
设置群员在该群内的昵称
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
:param nickname: 昵称
|
||||
"""
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
await cls.update_or_create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
defaults={"nickname": nickname},
|
||||
)
|
||||
user = await query.with_for_update().gino.first()
|
||||
if user:
|
||||
await user.update(nickname=nickname).apply()
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_user_all_group(cls, user_qq: int) -> List[int]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取该用户所在的所有群聊
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
"""
|
||||
query = await cls.query.where(cls.user_qq == user_qq).gino.all()
|
||||
if query:
|
||||
query = [x.group_id for x in query]
|
||||
return query
|
||||
return list(
|
||||
await cls.filter(user_qq=user_qq).values_list("group_id", flat=True)
|
||||
) # type: ignore
|
||||
|
||||
@classmethod
|
||||
async def get_group_member_nickname(cls, user_qq: int, group_id: int) -> str:
|
||||
async def get_user_nickname(cls, user_qq: int, group_id: int) -> str:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取用户在该群的昵称
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
"""
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
)
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
if user := await cls.get_or_none(user_qq=user_qq, group_id=group_id):
|
||||
if user.nickname:
|
||||
_tmp = ""
|
||||
black_word = Config.get_config("nickname", "BLACK_WORD")
|
||||
if black_word:
|
||||
nickname = ""
|
||||
if black_word := Config.get_config("nickname", "BLACK_WORD"):
|
||||
for x in user.nickname:
|
||||
_tmp += "*" if x in black_word else x
|
||||
return _tmp
|
||||
nickname += "*" if x in black_word else x
|
||||
return nickname
|
||||
return user.nickname
|
||||
return ""
|
||||
|
||||
@classmethod
|
||||
async def get_group_member_uid(cls, user_qq: int, group_id: int) -> Optional[str]:
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
async def get_group_member_uid(cls, user_qq: int, group_id: int) -> Optional[int]:
|
||||
logger.debug(
|
||||
f"GroupInfoUser 尝试获取 用户[<u><e>{user_qq}</e></u>] 群聊[<u><e>{group_id}</e></u>] UID"
|
||||
)
|
||||
user = await query.gino.first()
|
||||
_max_uid = cls.query.where((cls.user_qq == 114514) & (cls.group_id == 114514)).with_for_update()
|
||||
_max_uid_user = await _max_uid.gino.first()
|
||||
user, _ = await cls.get_or_create(user_qq=user_qq, group_id=group_id)
|
||||
_max_uid_user, _ = await cls.get_or_create(user_qq=114514, group_id=114514)
|
||||
_max_uid = _max_uid_user.uid
|
||||
if not user or not user.uid:
|
||||
all_user = await cls.query.where(cls.user_qq == user_qq).gino.all()
|
||||
if not user.uid:
|
||||
all_user = await cls.filter(user_qq=user_qq).all()
|
||||
for x in all_user:
|
||||
if x.uid:
|
||||
return x.uid
|
||||
else:
|
||||
if not user:
|
||||
await GroupInfoUser.add_member_info(user_qq, group_id, '', datetime.min)
|
||||
user = await cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
).gino.first()
|
||||
await user.update(
|
||||
uid=_max_uid + 1,
|
||||
).apply()
|
||||
await _max_uid_user.update(
|
||||
uid=_max_uid + 1,
|
||||
).apply()
|
||||
|
||||
return user.uid if user and user.uid else None
|
||||
user.uid = _max_uid + 1
|
||||
_max_uid_user.uid = _max_uid + 1
|
||||
await cls.bulk_update([user, _max_uid_user], ["uid"])
|
||||
logger.debug(
|
||||
f"GroupInfoUser 获取 用户[<u><e>{user_qq}</e></u>] 群聊[<u><e>{group_id}</e></u>] UID: {user.uid}"
|
||||
)
|
||||
return user.uid
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
"alter table group_info_users alter user_join_time drop not null;" # 允许 user_join_time 为空
|
||||
]
|
||||
|
||||
+47
-76
@@ -1,129 +1,100 @@
|
||||
from asyncpg import UniqueViolationError
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import db
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class LevelUser(db.Model):
|
||||
__tablename__ = "level_users"
|
||||
class LevelUser(Model):
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False)
|
||||
group_id = db.Column(db.BigInteger(), nullable=False)
|
||||
user_level = db.Column(db.BigInteger(), nullable=False)
|
||||
group_flag = db.Column(db.Integer(), nullable=False, default=0)
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
"""用户id"""
|
||||
group_id = fields.BigIntField()
|
||||
"""群聊id"""
|
||||
user_level = fields.BigIntField()
|
||||
"""用户权限等级"""
|
||||
group_flag = fields.IntField(default=0)
|
||||
"""特殊标记,是否随群管理员变更而设置权限"""
|
||||
|
||||
_idx1 = db.Index("level_group_users_idx1", "user_qq", "group_id", unique=True)
|
||||
class Meta:
|
||||
table = "level_users"
|
||||
table_description = "用户权限数据库"
|
||||
unique_together = ("user_qq", "group_id")
|
||||
|
||||
@classmethod
|
||||
async def get_user_level(cls, user_qq: int, group_id: int) -> int:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取用户在群内的等级
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
user = await query.gino.first()
|
||||
if user:
|
||||
if user := await cls.get_or_none(user_qq=user_qq, group_id=group_id):
|
||||
return user.user_level
|
||||
else:
|
||||
return -1
|
||||
return -1
|
||||
|
||||
@classmethod
|
||||
async def set_level(
|
||||
cls, user_qq: int, group_id: int, level: int, group_flag: int = 0
|
||||
) -> bool:
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
设置用户在群内的权限
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
:param level: 权限等级
|
||||
:param group_flag: 是否被自动更新刷新权限 0:是,1:否
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
try:
|
||||
if not user:
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
user_level=level,
|
||||
group_flag=group_flag,
|
||||
)
|
||||
return True
|
||||
else:
|
||||
await user.update(user_level=level, group_flag=group_flag).apply()
|
||||
return False
|
||||
except UniqueViolationError:
|
||||
return False
|
||||
await cls.update_or_create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
defaults={"user_level": level, "group_flag": group_flag},
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def delete_level(cls, user_qq: int, group_id: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
删除用户权限
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
"""
|
||||
query = cls.query.where((cls.user_qq == user_qq) & (cls.group_id == group_id))
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
if user is None:
|
||||
return False
|
||||
else:
|
||||
if user := await cls.get_or_none(user_qq=user_qq, group_id=group_id):
|
||||
await user.delete()
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def check_level(cls, user_qq: int, group_id: int, level: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
检查用户权限等级是否大于 level
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
:param level: 权限等级
|
||||
"""
|
||||
if group_id != 0:
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
)
|
||||
user = await query.gino.first()
|
||||
if user is None:
|
||||
return False
|
||||
user_level = user.user_level
|
||||
if group_id:
|
||||
if user := await cls.get_or_none(user_qq=user_qq, group_id=group_id):
|
||||
return user.user_level >= level
|
||||
else:
|
||||
query = cls.query.where(cls.user_qq == user_qq)
|
||||
highest_level = 0
|
||||
for user in await query.gino.all():
|
||||
if user.user_level > highest_level:
|
||||
highest_level = user.user_level
|
||||
user_level = highest_level
|
||||
if user_level >= level:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
user_list = await cls.filter(user_qq=user_qq).all()
|
||||
user = max(user_list, key=lambda x: x.user_level)
|
||||
return user.user_level >= level
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def is_group_flag(cls, user_qq: int, group_id: int) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
检测是否会被自动更新刷新权限
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: qq号
|
||||
:param group_id: 群号
|
||||
"""
|
||||
user = await cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
).gino.first()
|
||||
if not user:
|
||||
return False
|
||||
if user.group_flag == 1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
if user := await cls.get_or_none(user_qq=user_qq, group_id=group_id):
|
||||
return user.group_flag == 1
|
||||
return False
|
||||
|
||||
+49
-77
@@ -1,99 +1,71 @@
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
from services.db_context import db
|
||||
from typing import List, Literal, Optional, Tuple
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class SignGroupUser(db.Model):
|
||||
__tablename__ = "sign_group_users"
|
||||
class SignGroupUser(Model):
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False)
|
||||
group_id = db.Column(db.BigInteger(), nullable=False)
|
||||
checkin_count = db.Column(db.Integer(), nullable=False)
|
||||
checkin_time_last = db.Column(db.DateTime(timezone=True), nullable=False)
|
||||
impression = db.Column(db.Numeric(scale=3, asdecimal=False), nullable=False)
|
||||
add_probability = db.Column(
|
||||
db.Numeric(scale=3, asdecimal=False), nullable=False, default=0
|
||||
)
|
||||
specify_probability = db.Column(
|
||||
db.Numeric(scale=3, asdecimal=False), nullable=False, default=0
|
||||
)
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
"""用户id"""
|
||||
group_id = fields.BigIntField()
|
||||
"""群聊id"""
|
||||
checkin_count = fields.IntField(default=0)
|
||||
"""签到次数"""
|
||||
checkin_time_last = fields.DatetimeField(auto_now=True)
|
||||
"""最后签到时间"""
|
||||
impression = fields.DecimalField(10, 3, default=0)
|
||||
"""好感度"""
|
||||
add_probability = fields.DecimalField(10, 3, default=0)
|
||||
"""双倍签到增加概率"""
|
||||
specify_probability = fields.DecimalField(10, 3, default=0)
|
||||
"""使用指定双倍概率"""
|
||||
# specify_probability = fields.DecimalField(10, 3, default=0)
|
||||
|
||||
_idx1 = db.Index("sign_group_users_idx1", "user_qq", "group_id", unique=True)
|
||||
class Meta:
|
||||
table = "sign_group_users"
|
||||
table_description = "群员签到数据表"
|
||||
unique_together = ("user_qq", "group_id")
|
||||
|
||||
@classmethod
|
||||
async def ensure(
|
||||
cls, user_qq: int, group_id: int, for_update: bool = False
|
||||
) -> "SignGroupUser":
|
||||
"""
|
||||
说明:
|
||||
获取签到用户
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param group_id: 所在群聊
|
||||
:param for_update: 是否存在修改数据
|
||||
"""
|
||||
query = cls.query.where(
|
||||
(cls.user_qq == user_qq) & (cls.group_id == group_id)
|
||||
)
|
||||
if for_update:
|
||||
query = query.with_for_update()
|
||||
user = await query.gino.first()
|
||||
return user or await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
checkin_count=0,
|
||||
checkin_time_last=datetime.min, # 从未签到过
|
||||
impression=0,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_user_all_data(cls, user_qq: int) -> List["SignGroupUser"]:
|
||||
"""
|
||||
说明:
|
||||
获取某用户所有数据
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
"""
|
||||
query = cls.query.where(cls.user_qq == user_qq)
|
||||
query = query.with_for_update()
|
||||
return await query.gino.all()
|
||||
|
||||
@classmethod
|
||||
async def sign(cls, user: "SignGroupUser", impression: float, checkin_time_last: datetime):
|
||||
async def sign(cls, user: "SignGroupUser", impression: float):
|
||||
"""
|
||||
说明:
|
||||
签到
|
||||
说明:
|
||||
:param user: 用户
|
||||
:param impression: 增加的好感度
|
||||
:param checkin_time_last: 签到时间
|
||||
"""
|
||||
await user.update(
|
||||
checkin_count=user.checkin_count + 1,
|
||||
checkin_time_last=checkin_time_last,
|
||||
impression=user.impression + impression,
|
||||
add_probability=0,
|
||||
specify_probability=0,
|
||||
).apply()
|
||||
user.checkin_count = user.checkin_count + 1
|
||||
user.add_probability = 0
|
||||
user.specify_probability = 0
|
||||
user.impression = float(user.impression) + impression
|
||||
await user.save()
|
||||
|
||||
@classmethod
|
||||
async def get_all_impression(cls, group_id: int) -> "list, list, list":
|
||||
async def get_all_impression(
|
||||
cls, group_id: Optional[int]
|
||||
) -> Tuple[List[int], List[int], List[float]]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取该群所有用户 id 及对应 好感度
|
||||
参数:
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
impression_list = []
|
||||
user_qq_list = []
|
||||
user_group = []
|
||||
if group_id:
|
||||
query = cls.query.where(cls.group_id == group_id)
|
||||
query = cls.filter(group_id=group_id)
|
||||
else:
|
||||
query = cls.query
|
||||
for user in await query.gino.all():
|
||||
impression_list.append(user.impression)
|
||||
user_qq_list.append(user.user_qq)
|
||||
user_group.append(user.group_id)
|
||||
return user_qq_list, impression_list, user_group
|
||||
query = cls
|
||||
value_list = await query.all().values_list("user_qq", "group_id", "impression") # type: ignore
|
||||
qq_list = []
|
||||
group_list = []
|
||||
impression_list = []
|
||||
for value in value_list:
|
||||
qq_list.append(value[0])
|
||||
group_list.append(value[1])
|
||||
impression_list.append(float(value[2]))
|
||||
return qq_list, impression_list, group_list
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
from datetime import datetime
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class UserShopGoldLog(Model):
|
||||
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
"""用户id"""
|
||||
group_id = fields.BigIntField()
|
||||
"""群聊id"""
|
||||
type = fields.IntField()
|
||||
"""金币使用类型 0: 购买, 1: 使用, 2: 插件"""
|
||||
name = fields.CharField(255)
|
||||
"""商品/插件 名称"""
|
||||
spend_gold = fields.IntField(default=0)
|
||||
"""花费金币"""
|
||||
num = fields.IntField()
|
||||
"""数量"""
|
||||
create_time = fields.DatetimeField(auto_now_add=True)
|
||||
"""创建时间"""
|
||||
|
||||
class Meta:
|
||||
table = "user_shop_gold_log"
|
||||
table_description = "金币使用日志表"
|
||||
+14
-15
@@ -1,20 +1,20 @@
|
||||
from utils.message_builder import image
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from nonebot import on_command
|
||||
from nonebot.rule import to_me
|
||||
import os
|
||||
import random
|
||||
|
||||
from nonebot import on_command, on_keyword
|
||||
from nonebot.adapters.onebot.v11 import GroupMessageEvent
|
||||
from nonebot.adapters.onebot.v11.permission import GROUP
|
||||
from utils.utils import FreqLimiter
|
||||
from configs.config import NICKNAME
|
||||
import random
|
||||
from nonebot import on_keyword
|
||||
import os
|
||||
from nonebot.rule import to_me
|
||||
|
||||
from configs.config import NICKNAME
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from utils.message_builder import image
|
||||
from utils.utils import FreqLimiter
|
||||
|
||||
__zx_plugin_name__ = "基本设置 [Hidden]"
|
||||
__plugin_usage__ = "用法: 无"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = 'HibiKier'
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
|
||||
_flmt = FreqLimiter(300)
|
||||
@@ -29,7 +29,7 @@ async def _(event: GroupMessageEvent):
|
||||
return
|
||||
_flmt.start_cd(event.group_id)
|
||||
await config_play_game.finish(
|
||||
image(random.choice(os.listdir(IMAGE_PATH / "dayouxi")), "dayouxi")
|
||||
image(IMAGE_PATH / random.choice(os.listdir(IMAGE_PATH / "dayouxi")))
|
||||
)
|
||||
|
||||
|
||||
@@ -40,14 +40,14 @@ self_introduction = on_command(
|
||||
|
||||
@self_introduction.handle()
|
||||
async def _():
|
||||
if NICKNAME.find('真寻') != -1:
|
||||
if NICKNAME.find("真寻") != -1:
|
||||
result = (
|
||||
"我叫绪山真寻\n"
|
||||
"你们可以叫我真寻,小真寻,哪怕你们叫我小寻子我也能接受!\n"
|
||||
"年龄的话我还是个**岁初中生(至少现在是)\n"
|
||||
"身高保密!!!(也就比美波里(姐姐..(妹妹))矮一点)\n"
|
||||
"我生日是在3月6号, 能记住的话我会很高兴的\n现在是自宅警备系的现役JC\n"
|
||||
"最好的朋友是椛!\n" + image("zhenxun")
|
||||
"最好的朋友是椛!\n" + image("zhenxun.jpg")
|
||||
)
|
||||
await self_introduction.finish(result)
|
||||
|
||||
@@ -57,5 +57,4 @@ my_wife = on_keyword({"老婆"}, rule=to_me(), priority=5, block=True)
|
||||
|
||||
@my_wife.handle()
|
||||
async def _():
|
||||
await my_wife.finish(image("laopo.jpg", "other"))
|
||||
|
||||
await my_wife.finish(image(IMAGE_PATH / "other" / "laopo.jpg"))
|
||||
|
||||
+16
-13
@@ -1,17 +1,16 @@
|
||||
from typing import List
|
||||
|
||||
from nonebot import on_message
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
GroupMessageEvent,
|
||||
Message,
|
||||
MessageEvent,
|
||||
)
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
|
||||
from nonebot.rule import to_me
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_member_info import GroupInfoUser
|
||||
from services.log import logger
|
||||
from utils.utils import get_message_img, get_message_text
|
||||
|
||||
from .data_source import get_chat_result, hello, no_result
|
||||
from configs.config import NICKNAME, Config
|
||||
|
||||
__zx_plugin_name__ = "AI"
|
||||
__plugin_usage__ = f"""
|
||||
@@ -25,12 +24,18 @@ __plugin_settings__ = {
|
||||
"cmd": ["Ai", "ai", "AI", "aI"],
|
||||
}
|
||||
__plugin_configs__ = {
|
||||
"TL_KEY": {"value": [], "help": "图灵Key"},
|
||||
"ALAPI_AI_CHECK": {"value": False, "help": "是否检测青云客骂娘回复", "default_value": False},
|
||||
"TL_KEY": {"value": [], "help": "图灵Key", "type": List[str]},
|
||||
"ALAPI_AI_CHECK": {
|
||||
"value": False,
|
||||
"help": "是否检测青云客骂娘回复",
|
||||
"default_value": False,
|
||||
"type": bool,
|
||||
},
|
||||
"TEXT_FILTER": {
|
||||
"value": ["鸡", "口交"],
|
||||
"help": "文本过滤器,将敏感词更改为*",
|
||||
"default_value": [],
|
||||
"type": List[str],
|
||||
},
|
||||
}
|
||||
Config.add_plugin_config(
|
||||
@@ -60,11 +65,9 @@ async def _(bot: Bot, event: MessageEvent):
|
||||
await ai.finish(hello())
|
||||
img = img[0] if img else ""
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
nickname = await GroupInfoUser.get_group_member_nickname(
|
||||
event.user_id, event.group_id
|
||||
)
|
||||
nickname = await GroupInfoUser.get_user_nickname(event.user_id, event.group_id)
|
||||
else:
|
||||
nickname = await FriendUser.get_friend_nickname(event.user_id)
|
||||
nickname = await FriendUser.get_user_nickname(event.user_id)
|
||||
if not nickname:
|
||||
if isinstance(event, GroupMessageEvent):
|
||||
nickname = event.sender.card or event.sender.nickname
|
||||
|
||||
+21
-18
@@ -1,11 +1,13 @@
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from configs.path_config import IMAGE_PATH, DATA_PATH
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from configs.path_config import DATA_PATH, IMAGE_PATH
|
||||
from services.log import logger
|
||||
from utils.message_builder import image, face
|
||||
from configs.config import Config, NICKNAME
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from utils.message_builder import face, image
|
||||
|
||||
from .utils import ai_message_manager
|
||||
|
||||
try:
|
||||
@@ -124,7 +126,9 @@ async def xie_ai(text: str) -> str:
|
||||
:param text: 问题
|
||||
:return: 青云可回复
|
||||
"""
|
||||
res = await AsyncHttpx.get(f"http://api.qingyunke.com/api.php?key=free&appid=0&msg={text}")
|
||||
res = await AsyncHttpx.get(
|
||||
f"http://api.qingyunke.com/api.php?key=free&appid=0&msg={text}"
|
||||
)
|
||||
content = ""
|
||||
try:
|
||||
data = json.loads(res.text)
|
||||
@@ -176,9 +180,9 @@ def hello() -> str:
|
||||
)
|
||||
img = random.choice(os.listdir(IMAGE_PATH / "zai"))
|
||||
if img[-4:] == ".gif":
|
||||
result += image(img, "zai")
|
||||
result += image(IMAGE_PATH / "zai" / img)
|
||||
else:
|
||||
result += image(img, "zai")
|
||||
result += image(IMAGE_PATH / "zai" / img)
|
||||
return result
|
||||
|
||||
|
||||
@@ -187,17 +191,16 @@ def no_result() -> str:
|
||||
"""
|
||||
没有回答时的回复
|
||||
"""
|
||||
return (
|
||||
random.choice(
|
||||
[
|
||||
"你在说啥子?",
|
||||
f"纯洁的{NICKNAME}没听懂",
|
||||
"下次再告诉你(下次一定)",
|
||||
"你觉得我听懂了吗?嗯?",
|
||||
"我!不!知!道!",
|
||||
]
|
||||
)
|
||||
+ image(random.choice(os.listdir(IMAGE_PATH / "noresult")), "noresult")
|
||||
return random.choice(
|
||||
[
|
||||
"你在说啥子?",
|
||||
f"纯洁的{NICKNAME}没听懂",
|
||||
"下次再告诉你(下次一定)",
|
||||
"你觉得我听懂了吗?嗯?",
|
||||
"我!不!知!道!",
|
||||
]
|
||||
) + image(
|
||||
IMAGE_PATH / "noresult" / random.choice(os.listdir(IMAGE_PATH / "noresult"))
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
from configs.config import Config
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
|
||||
from configs.config import Config
|
||||
|
||||
Config.add_plugin_config(
|
||||
"alapi",
|
||||
"ALAPI_TOKEN",
|
||||
None,
|
||||
help_="在https://admin.alapi.cn/user/login登录后获取token"
|
||||
"alapi", "ALAPI_TOKEN", None, help_="在https://admin.alapi.cn/user/login登录后获取token"
|
||||
)
|
||||
|
||||
|
||||
nonebot.load_plugins("plugins/alapi")
|
||||
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
|
||||
@@ -1,24 +1,29 @@
|
||||
from nonebot import on_command
|
||||
from typing import Any, Optional, Tuple
|
||||
|
||||
import nonebot
|
||||
from nonebot import Driver, on_command, on_regex
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent
|
||||
from nonebot.params import ArgStr, CommandArg, RegexGroup
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, GroupMessageEvent, Message
|
||||
|
||||
from configs.config import Config
|
||||
from models.level_user import LevelUser
|
||||
from services.log import logger
|
||||
from utils.image_utils import text2image
|
||||
from utils.manager import group_manager
|
||||
from utils.message_builder import image
|
||||
from utils.utils import get_bot, is_number, scheduler
|
||||
|
||||
from .data_source import (
|
||||
BilibiliSub,
|
||||
SubManager,
|
||||
add_live_sub,
|
||||
delete_sub,
|
||||
add_up_sub,
|
||||
add_season_sub,
|
||||
add_up_sub,
|
||||
delete_sub,
|
||||
get_media_id,
|
||||
get_sub_status,
|
||||
SubManager,
|
||||
BilibiliSub,
|
||||
)
|
||||
from models.level_user import LevelUser
|
||||
from configs.config import Config
|
||||
from utils.utils import is_number, scheduler, get_bot
|
||||
from typing import Optional
|
||||
from services.log import logger
|
||||
from nonebot import Driver
|
||||
from nonebot.params import CommandArg, ArgStr
|
||||
import nonebot
|
||||
|
||||
__zx_plugin_name__ = "B站订阅"
|
||||
__plugin_usage__ = """
|
||||
@@ -27,16 +32,16 @@ usage:
|
||||
主播订阅相当于 直播间订阅 + UP订阅
|
||||
指令:[示例Id乱打的,仅做示例]
|
||||
添加订阅 ['主播'/'UP'/'番剧'] [id/链接/番名]
|
||||
删除订阅 [id]
|
||||
删除订阅 ['主播'/'UP'/'id'] [id]
|
||||
查看订阅
|
||||
示例:添加订阅主播 2345344 <-(直播房间id)
|
||||
示例:添加订阅UP 2355543 <-(个人主页id)
|
||||
示例:添加订阅番剧 史莱姆 <-(支持模糊搜索)
|
||||
示例:添加订阅番剧 125344 <-(番剧id)
|
||||
示例:删除订阅 2324344 <-(任意id,通过查看订阅获取)
|
||||
示例:删除订阅id 2324344 <-(任意id,通过查看订阅获取)
|
||||
""".strip()
|
||||
__plugin_des__ = "非常便利的B站订阅通知"
|
||||
__plugin_cmd__ = ["添加订阅 [主播/UP/番剧] [id/链接/番名]", "删除订阅 [id]", "查看订阅"]
|
||||
__plugin_cmd__ = ["添加订阅 [主播/UP/番剧] [id/链接/番名]", "删除订阅 ['主播'/'UP'/'id'] [id]", "查看订阅"]
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier & NumberSir"
|
||||
__plugin_settings__ = {
|
||||
@@ -50,12 +55,25 @@ __plugin_configs__ = {
|
||||
"value": 5,
|
||||
"help": "群内bilibili订阅需要管理的权限",
|
||||
"default_value": 5,
|
||||
}
|
||||
"type": int,
|
||||
},
|
||||
"LIVE_MSG_AT_ALL": {
|
||||
"value": False,
|
||||
"help": "直播提醒是否AT全体(仅在真寻是管理员时生效)",
|
||||
"default_value": False,
|
||||
"type": bool,
|
||||
},
|
||||
"UP_MSG_AT_ALL": {
|
||||
"value": False,
|
||||
"help": "UP动态投稿提醒是否AT全体(仅在真寻是管理员时生效)",
|
||||
"default_value": False,
|
||||
"type": bool,
|
||||
},
|
||||
}
|
||||
|
||||
add_sub = on_command("添加订阅", priority=5, block=True)
|
||||
del_sub = on_command("删除订阅", priority=5, block=True)
|
||||
show_sub_info = on_command("查看订阅", priority=5, block=True)
|
||||
show_sub_info = on_regex("^查看订阅$", priority=5, block=True)
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
@@ -70,6 +88,7 @@ async def _():
|
||||
|
||||
|
||||
@add_sub.handle()
|
||||
@del_sub.handle()
|
||||
async def _(event: MessageEvent, state: T_State, arg: Message = CommandArg()):
|
||||
msg = arg.extract_plain_text().strip().split()
|
||||
if len(msg) < 2:
|
||||
@@ -143,25 +162,30 @@ async def _(
|
||||
)
|
||||
|
||||
|
||||
@del_sub.handle()
|
||||
async def _(event: MessageEvent, arg: Message = CommandArg()):
|
||||
msg = arg.extract_plain_text().strip()
|
||||
if not is_number(msg):
|
||||
await del_sub.finish("Id必须为数字!", at_sender=True)
|
||||
id_ = (
|
||||
f"{event.user_id}:{event.group_id}"
|
||||
if isinstance(event, GroupMessageEvent)
|
||||
else f"{event.user_id}"
|
||||
)
|
||||
if await BilibiliSub.delete_bilibili_sub(int(msg), id_):
|
||||
await del_sub.send(f"删除订阅id:{msg} 成功...")
|
||||
@del_sub.got("sub_type")
|
||||
@del_sub.got("sub_user")
|
||||
@del_sub.got("id")
|
||||
async def _(
|
||||
event: MessageEvent,
|
||||
id_: str = ArgStr("id"),
|
||||
sub_type: str = ArgStr("sub_type"),
|
||||
sub_user: str = ArgStr("sub_user"),
|
||||
):
|
||||
if sub_type in ["主播", "直播"]:
|
||||
result = await BilibiliSub.delete_bilibili_sub(int(id_), sub_user, "live")
|
||||
elif sub_type.lower() in ["up", "用户"]:
|
||||
result = await BilibiliSub.delete_bilibili_sub(int(id_), sub_user, "up")
|
||||
else:
|
||||
result = await BilibiliSub.delete_bilibili_sub(int(id_), sub_user)
|
||||
if result:
|
||||
await del_sub.send(f"删除订阅id:{id_} 成功...")
|
||||
logger.info(
|
||||
f"(USER {event.user_id}, GROUP "
|
||||
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'})"
|
||||
f" 删除订阅 {id_}"
|
||||
)
|
||||
else:
|
||||
await del_sub.send(f"删除订阅id:{msg} 失败...")
|
||||
await del_sub.send(f"删除订阅id:{id_} 失败...")
|
||||
|
||||
|
||||
@show_sub_info.handle()
|
||||
@@ -170,7 +194,7 @@ async def _(event: MessageEvent):
|
||||
id_ = f"{event.group_id}"
|
||||
else:
|
||||
id_ = f"{event.user_id}"
|
||||
data = await BilibiliSub.get_sub_data(id_)
|
||||
data = await BilibiliSub.filter(sub_users__contains=id_).all()
|
||||
live_rst = ""
|
||||
up_rst = ""
|
||||
season_rst = ""
|
||||
@@ -195,7 +219,13 @@ async def _(event: MessageEvent):
|
||||
live_rst = (
|
||||
"该群目前没有任何订阅..." if isinstance(event, GroupMessageEvent) else "您目前没有任何订阅..."
|
||||
)
|
||||
await show_sub_info.send(live_rst + up_rst + season_rst)
|
||||
await show_sub_info.send(
|
||||
image(
|
||||
await text2image(
|
||||
live_rst + up_rst + season_rst, padding=10, color="#f9f6f2"
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# 推送
|
||||
@@ -207,18 +237,18 @@ async def _():
|
||||
bot = get_bot()
|
||||
sub = None
|
||||
if bot:
|
||||
try:
|
||||
await sub_manager.reload_sub_data()
|
||||
sub = await sub_manager.random_sub_data()
|
||||
if sub:
|
||||
logger.debug(f"Bilibili订阅开始检测:{sub.sub_id}")
|
||||
rst = await get_sub_status(sub.sub_id, sub.sub_type)
|
||||
# try:
|
||||
await sub_manager.reload_sub_data()
|
||||
sub = await sub_manager.random_sub_data()
|
||||
if sub:
|
||||
logger.debug(f"Bilibili订阅开始检测:{sub.sub_id}")
|
||||
rst = await get_sub_status(sub.sub_id, sub.sub_type)
|
||||
await send_sub_msg(rst, sub, bot)
|
||||
if sub.sub_type == "live":
|
||||
rst = await get_sub_status(sub.sub_id, "up")
|
||||
await send_sub_msg(rst, sub, bot)
|
||||
if sub.sub_type == "live":
|
||||
rst = await get_sub_status(sub.sub_id, "up")
|
||||
await send_sub_msg(rst, sub, bot)
|
||||
except Exception as e:
|
||||
logger.error(f"B站订阅推送发生错误 sub_id:{sub.sub_id if sub else 0} {type(e)}:{e}")
|
||||
# except Exception as e:
|
||||
# logger.error(f"B站订阅推送发生错误 sub_id:{sub.sub_id if sub else 0} {type(e)}:{e}")
|
||||
|
||||
|
||||
async def send_sub_msg(rst: str, sub: BilibiliSub, bot: Bot):
|
||||
@@ -228,13 +258,30 @@ async def send_sub_msg(rst: str, sub: BilibiliSub, bot: Bot):
|
||||
:param sub: BilibiliSub
|
||||
:param bot: Bot
|
||||
"""
|
||||
temp_group = []
|
||||
if rst:
|
||||
for x in sub.sub_users.split(",")[:-1]:
|
||||
try:
|
||||
if ":" in x:
|
||||
await bot.send_group_msg(
|
||||
group_id=int(x.split(":")[1]), message=Message(rst)
|
||||
)
|
||||
if ":" in x and x.split(":")[1] not in temp_group:
|
||||
group_id = int(x.split(":")[1])
|
||||
temp_group.append(group_id)
|
||||
if (
|
||||
await bot.get_group_member_info(
|
||||
group_id=group_id, user_id=int(bot.self_id), no_cache=True
|
||||
)
|
||||
)["role"] in ["owner", "admin"]:
|
||||
if (
|
||||
sub.sub_type == "live"
|
||||
and Config.get_config("bilibili_sub", "LIVE_MSG_AT_ALL")
|
||||
) or (
|
||||
sub.sub_type == "up"
|
||||
and Config.get_config("bilibili_sub", "UP_MSG_AT_ALL")
|
||||
):
|
||||
rst = "[CQ:at,qq=all]\n" + rst
|
||||
if group_manager.get_plugin_status("bilibili_sub", group_id):
|
||||
await bot.send_group_msg(
|
||||
group_id=group_id, message=Message(rst)
|
||||
)
|
||||
else:
|
||||
await bot.send_private_msg(user_id=int(x), message=Message(rst))
|
||||
except Exception as e:
|
||||
|
||||
+144
-167
@@ -1,30 +1,33 @@
|
||||
from bilireq.exceptions import ResponseCodeError
|
||||
from utils.manager import resources_manager
|
||||
from asyncio.exceptions import TimeoutError
|
||||
from .model import BilibiliSub
|
||||
from bilireq.live import get_room_info_by_id
|
||||
from .utils import get_meta
|
||||
from utils.message_builder import image
|
||||
from bilireq.user import get_user_info
|
||||
from bilireq import dynamic
|
||||
from .utils import get_videos
|
||||
from typing import Optional
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from datetime import datetime
|
||||
from utils.browser import get_browser
|
||||
from services.db_context import db
|
||||
from services.log import logger
|
||||
from utils.http_utils import AsyncHttpx
|
||||
import random
|
||||
from asyncio.exceptions import TimeoutError
|
||||
from datetime import datetime
|
||||
from typing import Optional, Tuple
|
||||
|
||||
# from .utils import get_videos
|
||||
from bilireq import dynamic
|
||||
from bilireq.exceptions import ResponseCodeError
|
||||
from bilireq.live import get_room_info_by_id
|
||||
from bilireq.user import get_videos
|
||||
from nonebot.adapters.onebot.v11 import MessageSegment
|
||||
|
||||
from configs.path_config import IMAGE_PATH, TEMP_PATH
|
||||
from services.log import logger
|
||||
from utils.browser import get_browser
|
||||
from utils.http_utils import AsyncHttpx, AsyncPlaywright
|
||||
from utils.manager import resources_manager
|
||||
from utils.message_builder import image
|
||||
from utils.utils import get_bot
|
||||
|
||||
from .model import BilibiliSub
|
||||
from .utils import get_meta, get_user_card
|
||||
|
||||
SEARCH_URL = "https://api.bilibili.com/x/web-interface/search/all/v2"
|
||||
|
||||
DYNAMIC_PATH = IMAGE_PATH / "bilibili_sub" / "dynamic"
|
||||
DYNAMIC_PATH.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
|
||||
bilibili_search_url = "https://api.bilibili.com/x/web-interface/search/all/v2"
|
||||
|
||||
dynamic_path = IMAGE_PATH / "bilibili_sub" / "dynamic"
|
||||
dynamic_path.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
|
||||
resources_manager.add_temp_dir(dynamic_path)
|
||||
resources_manager.add_temp_dir(DYNAMIC_PATH)
|
||||
|
||||
|
||||
async def add_live_sub(live_id: int, sub_user: str) -> str:
|
||||
@@ -35,36 +38,35 @@ async def add_live_sub(live_id: int, sub_user: str) -> str:
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
async with db.transaction():
|
||||
try:
|
||||
"""bilibili_api.live库的LiveRoom类中get_room_info改为bilireq.live库的get_room_info_by_id方法"""
|
||||
live_info = await get_room_info_by_id(live_id)
|
||||
except ResponseCodeError:
|
||||
return f"未找到房间号Id:{live_id} 的信息,请检查Id是否正确"
|
||||
uid = live_info["uid"]
|
||||
room_id = live_info["room_id"]
|
||||
short_id = live_info["short_id"]
|
||||
title = live_info["title"]
|
||||
live_status = live_info["live_status"]
|
||||
if await BilibiliSub.add_bilibili_sub(
|
||||
room_id,
|
||||
"live",
|
||||
sub_user,
|
||||
uid=uid,
|
||||
live_short_id=short_id,
|
||||
live_status=live_status,
|
||||
):
|
||||
await _get_up_status(live_id)
|
||||
uname = (await BilibiliSub.get_sub(live_id)).uname
|
||||
return (
|
||||
"已成功订阅主播:\n"
|
||||
f"\ttitle:{title}\n"
|
||||
f"\tname: {uname}\n"
|
||||
f"\tlive_id:{live_id}\n"
|
||||
f"\tuid:{uid}"
|
||||
)
|
||||
else:
|
||||
return "添加订阅失败..."
|
||||
try:
|
||||
"""bilibili_api.live库的LiveRoom类中get_room_info改为bilireq.live库的get_room_info_by_id方法"""
|
||||
live_info = await get_room_info_by_id(live_id)
|
||||
except ResponseCodeError:
|
||||
return f"未找到房间号Id:{live_id} 的信息,请检查Id是否正确"
|
||||
uid = live_info["uid"]
|
||||
room_id = live_info["room_id"]
|
||||
short_id = live_info["short_id"]
|
||||
title = live_info["title"]
|
||||
live_status = live_info["live_status"]
|
||||
if await BilibiliSub.sub_handle(
|
||||
room_id,
|
||||
"live",
|
||||
sub_user,
|
||||
uid=uid,
|
||||
live_short_id=short_id,
|
||||
live_status=live_status,
|
||||
):
|
||||
await _get_up_status(room_id)
|
||||
uname = (await BilibiliSub.get_or_none(sub_id=room_id)).uname
|
||||
return (
|
||||
"已成功订阅主播:\n"
|
||||
f"\ttitle:{title}\n"
|
||||
f"\tname: {uname}\n"
|
||||
f"\tlive_id:{room_id}\n"
|
||||
f"\tuid:{uid}"
|
||||
)
|
||||
else:
|
||||
return "添加订阅失败..."
|
||||
except Exception as e:
|
||||
logger.error(f"订阅主播live_id:{live_id} 发生了错误 {type(e)}:{e}")
|
||||
return "添加订阅失败..."
|
||||
@@ -77,35 +79,34 @@ async def add_up_sub(uid: int, sub_user: str) -> str:
|
||||
:param sub_user: 订阅用户
|
||||
"""
|
||||
try:
|
||||
async with db.transaction():
|
||||
try:
|
||||
"""bilibili_api.user库中User类的get_user_info改为bilireq.user库的get_user_info方法"""
|
||||
user_info = await get_user_info(uid)
|
||||
except ResponseCodeError:
|
||||
return f"未找到UpId:{uid} 的信息,请检查Id是否正确"
|
||||
uname = user_info["name"]
|
||||
"""bilibili_api.user库中User类的get_dynamics改为bilireq.dynamic库的get_user_dynamics方法"""
|
||||
dynamic_info = await dynamic.get_user_dynamics(uid)
|
||||
dynamic_upload_time = 0
|
||||
if dynamic_info.get("cards"):
|
||||
dynamic_upload_time = dynamic_info["cards"][0]["desc"]["timestamp"]
|
||||
"""bilibili_api.user库中User类的get_videos改为bilireq.user库的get_videos方法"""
|
||||
video_info = await get_videos(uid)
|
||||
latest_video_created = 0
|
||||
if video_info["list"].get("vlist"):
|
||||
latest_video_created = video_info["list"]["vlist"][0]["created"]
|
||||
if await BilibiliSub.add_bilibili_sub(
|
||||
uid,
|
||||
"up",
|
||||
sub_user,
|
||||
uid=uid,
|
||||
uname=uname,
|
||||
dynamic_upload_time=dynamic_upload_time,
|
||||
latest_video_created=latest_video_created,
|
||||
):
|
||||
return "已成功订阅UP:\n" f"\tname: {uname}\n" f"\tuid:{uid}"
|
||||
else:
|
||||
return "添加订阅失败..."
|
||||
try:
|
||||
"""bilibili_api.user库中User类的get_user_info改为bilireq.user库的get_user_info方法"""
|
||||
user_info = await get_user_card(uid)
|
||||
except ResponseCodeError:
|
||||
return f"未找到UpId:{uid} 的信息,请检查Id是否正确"
|
||||
uname = user_info["name"]
|
||||
"""bilibili_api.user库中User类的get_dynamics改为bilireq.dynamic库的get_user_dynamics方法"""
|
||||
dynamic_info = await dynamic.get_user_dynamics(uid)
|
||||
dynamic_upload_time = 0
|
||||
if dynamic_info.get("cards"):
|
||||
dynamic_upload_time = dynamic_info["cards"][0]["desc"]["timestamp"]
|
||||
"""bilibili_api.user库中User类的get_videos改为bilireq.user库的get_videos方法"""
|
||||
video_info = await get_videos(uid)
|
||||
latest_video_created = 0
|
||||
if video_info["list"].get("vlist"):
|
||||
latest_video_created = video_info["list"]["vlist"][0]["created"]
|
||||
if await BilibiliSub.sub_handle(
|
||||
uid,
|
||||
"up",
|
||||
sub_user,
|
||||
uid=uid,
|
||||
uname=uname,
|
||||
dynamic_upload_time=dynamic_upload_time,
|
||||
latest_video_created=latest_video_created,
|
||||
):
|
||||
return "已成功订阅UP:\n" f"\tname: {uname}\n" f"\tuid:{uid}"
|
||||
else:
|
||||
return "添加订阅失败..."
|
||||
except Exception as e:
|
||||
logger.error(f"订阅Up uid:{uid} 发生了错误 {type(e)}:{e}")
|
||||
return "添加订阅失败..."
|
||||
@@ -118,30 +119,29 @@ async def add_season_sub(media_id: int, sub_user: str) -> str:
|
||||
:param sub_user: 订阅用户
|
||||
"""
|
||||
try:
|
||||
async with db.transaction():
|
||||
try:
|
||||
"""bilibili_api.bangumi库中get_meta改为bilireq.bangumi库的get_meta方法"""
|
||||
season_info = await get_meta(media_id)
|
||||
except ResponseCodeError:
|
||||
return f"未找到media_id:{media_id} 的信息,请检查Id是否正确"
|
||||
season_id = season_info["media"]["season_id"]
|
||||
season_current_episode = season_info["media"]["new_ep"]["index"]
|
||||
season_name = season_info["media"]["title"]
|
||||
if await BilibiliSub.add_bilibili_sub(
|
||||
media_id,
|
||||
"season",
|
||||
sub_user,
|
||||
season_name=season_name,
|
||||
season_id=season_id,
|
||||
season_current_episode=season_current_episode,
|
||||
):
|
||||
return (
|
||||
"已成功订阅番剧:\n"
|
||||
f"\ttitle: {season_name}\n"
|
||||
f"\tcurrent_episode: {season_current_episode}"
|
||||
)
|
||||
else:
|
||||
return "添加订阅失败..."
|
||||
try:
|
||||
"""bilibili_api.bangumi库中get_meta改为bilireq.bangumi库的get_meta方法"""
|
||||
season_info = await get_meta(media_id)
|
||||
except ResponseCodeError:
|
||||
return f"未找到media_id:{media_id} 的信息,请检查Id是否正确"
|
||||
season_id = season_info["media"]["season_id"]
|
||||
season_current_episode = season_info["media"]["new_ep"]["index"]
|
||||
season_name = season_info["media"]["title"]
|
||||
if await BilibiliSub.sub_handle(
|
||||
media_id,
|
||||
"season",
|
||||
sub_user,
|
||||
season_name=season_name,
|
||||
season_id=season_id,
|
||||
season_current_episode=season_current_episode,
|
||||
):
|
||||
return (
|
||||
"已成功订阅番剧:\n"
|
||||
f"\ttitle: {season_name}\n"
|
||||
f"\tcurrent_episode: {season_current_episode}"
|
||||
)
|
||||
else:
|
||||
return "添加订阅失败..."
|
||||
except Exception as e:
|
||||
logger.error(f"订阅番剧 media_id:{media_id} 发生了错误 {type(e)}:{e}")
|
||||
return "添加订阅失败..."
|
||||
@@ -168,9 +168,7 @@ async def get_media_id(keyword: str) -> dict:
|
||||
for _ in range(3):
|
||||
try:
|
||||
_season_data = {}
|
||||
response = await AsyncHttpx.get(
|
||||
bilibili_search_url, params=params, timeout=5
|
||||
)
|
||||
response = await AsyncHttpx.get(SEARCH_URL, params=params, timeout=5)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("data"):
|
||||
@@ -204,8 +202,10 @@ async def get_sub_status(id_: int, sub_type: str) -> Optional[str]:
|
||||
return await _get_up_status(id_)
|
||||
elif sub_type == "season":
|
||||
return await _get_season_status(id_)
|
||||
except ResponseCodeError:
|
||||
return f"Id:{id_} 获取信息失败...请检查订阅Id是否存在或稍后再试..."
|
||||
except ResponseCodeError as msg:
|
||||
logger.info(f"Id:{id_} 获取信息失败...{msg}")
|
||||
return None
|
||||
# return f"Id:{id_} 获取信息失败...请检查订阅Id是否存在或稍后再试..."
|
||||
# except Exception as e:
|
||||
# logger.error(f"获取订阅状态发生预料之外的错误 id_:{id_} {type(e)}:{e}")
|
||||
# return "发生了预料之外的错误..请稍后再试或联系管理员....."
|
||||
@@ -222,11 +222,12 @@ async def _get_live_status(id_: int) -> Optional[str]:
|
||||
room_id = live_info["room_id"]
|
||||
live_status = live_info["live_status"]
|
||||
cover = live_info["user_cover"]
|
||||
sub = await BilibiliSub.get_sub(id_)
|
||||
sub = await BilibiliSub.get_or_none(sub_id=id_)
|
||||
if sub.live_status != live_status:
|
||||
await BilibiliSub.update_sub_info(id_, live_status=live_status)
|
||||
if sub.live_status == 0 and live_status == 1:
|
||||
await BilibiliSub.sub_handle(id_, live_status=live_status)
|
||||
if sub.live_status in [0, 2] and live_status == 1:
|
||||
return (
|
||||
f""
|
||||
f"{image(cover)}\n"
|
||||
f"{sub.uname} 开播啦!\n"
|
||||
f"标题:{title}\n"
|
||||
@@ -238,37 +239,36 @@ async def _get_live_status(id_: int) -> Optional[str]:
|
||||
async def _get_up_status(id_: int) -> Optional[str]:
|
||||
"""
|
||||
获取用户投稿状态
|
||||
:param id_: 用户 id
|
||||
:param id_: 订阅 id
|
||||
:return:
|
||||
"""
|
||||
_user = await BilibiliSub.get_sub(id_)
|
||||
_user = await BilibiliSub.get_or_none(sub_id=id_)
|
||||
"""bilibili_api.user库中User类的get_user_info改为bilireq.user库的get_user_info方法"""
|
||||
user_info = await get_user_info(_user.uid)
|
||||
user_info = await get_user_card(_user.uid)
|
||||
uname = user_info["name"]
|
||||
"""bilibili_api.user库中User类的get_videos改为bilireq.user库的get_videos方法"""
|
||||
video_info = await get_videos(id_)
|
||||
video_info = await get_videos(_user.uid)
|
||||
latest_video_created = 0
|
||||
video = None
|
||||
dividing_line = "\n-------------\n"
|
||||
if _user.uname != uname:
|
||||
await BilibiliSub.update_sub_info(id_, uname=uname)
|
||||
dynamic_img, dynamic_upload_time = await get_user_dynamic(id_, _user)
|
||||
await BilibiliSub.sub_handle(id_, uname=uname)
|
||||
dynamic_img, dynamic_upload_time, link = await get_user_dynamic(_user.uid, _user)
|
||||
if video_info["list"].get("vlist"):
|
||||
video = video_info["list"]["vlist"][0]
|
||||
latest_video_created = video["created"]
|
||||
rst = ""
|
||||
if dynamic_img:
|
||||
await BilibiliSub.update_sub_info(id_, dynamic_upload_time=dynamic_upload_time)
|
||||
rst += f"{uname} 发布了动态!\n" f"{dynamic_img}\n"
|
||||
await BilibiliSub.sub_handle(id_, dynamic_upload_time=dynamic_upload_time)
|
||||
rst += f"{uname} 发布了动态!\n" f"{dynamic_img}\n{link}"
|
||||
if (
|
||||
latest_video_created
|
||||
and _user.latest_video_created
|
||||
and video
|
||||
and _user.latest_video_created < latest_video_created
|
||||
):
|
||||
rst = rst + "-------------\n" if rst else rst
|
||||
await BilibiliSub.update_sub_info(
|
||||
id_, latest_video_created=latest_video_created
|
||||
)
|
||||
rst = rst + dividing_line if rst else rst
|
||||
await BilibiliSub.sub_handle(id_, latest_video_created=latest_video_created)
|
||||
rst += (
|
||||
f'{image(video["pic"])}\n'
|
||||
f"{uname} 投稿了新视频啦\n"
|
||||
@@ -276,7 +276,7 @@ async def _get_up_status(id_: int) -> Optional[str]:
|
||||
f'Bvid:{video["bvid"]}\n'
|
||||
f'直链:https://www.bilibili.com/video/{video["bvid"]}'
|
||||
)
|
||||
rst = None if rst == "-------------\n" else rst
|
||||
rst = None if rst == dividing_line else rst
|
||||
return rst
|
||||
|
||||
|
||||
@@ -288,10 +288,10 @@ async def _get_season_status(id_) -> Optional[str]:
|
||||
"""bilibili_api.bangumi库中get_meta改为bilireq.bangumi库的get_meta方法"""
|
||||
season_info = await get_meta(id_)
|
||||
title = season_info["media"]["title"]
|
||||
_idx = (await BilibiliSub.get_sub(id_)).season_current_episode
|
||||
_idx = (await BilibiliSub.get_or_none(sub_id=id_)).season_current_episode
|
||||
new_ep = season_info["media"]["new_ep"]["index"]
|
||||
if new_ep != _idx:
|
||||
await BilibiliSub.update_sub_info(
|
||||
await BilibiliSub.sub_handle(
|
||||
id_, season_current_episode=new_ep, season_update_time=datetime.now()
|
||||
)
|
||||
return (
|
||||
@@ -304,7 +304,7 @@ async def _get_season_status(id_) -> Optional[str]:
|
||||
|
||||
async def get_user_dynamic(
|
||||
uid: int, local_user: BilibiliSub
|
||||
) -> "Optional[MessageSegment], int":
|
||||
) -> Tuple[Optional[MessageSegment], int, str]:
|
||||
"""
|
||||
获取用户动态
|
||||
:param uid: 用户uid
|
||||
@@ -313,45 +313,22 @@ async def get_user_dynamic(
|
||||
"""
|
||||
"""bilibili_api.user库中User类的get_dynamics改为bilireq.dynamic库的get_user_dynamics方法"""
|
||||
dynamic_info = await dynamic.get_user_dynamics(uid)
|
||||
browser = await get_browser()
|
||||
if dynamic_info.get("cards") and browser:
|
||||
if dynamic_info.get("cards"):
|
||||
dynamic_upload_time = dynamic_info["cards"][0]["desc"]["timestamp"]
|
||||
dynamic_id = dynamic_info["cards"][0]["desc"]["dynamic_id"]
|
||||
if local_user.dynamic_upload_time < dynamic_upload_time:
|
||||
page = await browser.new_page()
|
||||
try:
|
||||
await page.goto(
|
||||
f"https://space.bilibili.com/{local_user.uid}/dynamic",
|
||||
wait_until="networkidle",
|
||||
timeout=10000,
|
||||
)
|
||||
await page.set_viewport_size({"width": 2560, "height": 1080, "timeout": 10000*20}) # timeout: 200s
|
||||
# 删除置顶
|
||||
await page.evaluate(
|
||||
"""
|
||||
xs = document.getElementsByClassName('bili-dyn-item__tag');
|
||||
for (x of xs) {
|
||||
x.parentNode.parentNode.remove();
|
||||
}
|
||||
"""
|
||||
)
|
||||
card = page.locator(".bili-dyn-list__item").first
|
||||
await card.wait_for()
|
||||
# 截图并保存
|
||||
await card.screenshot(
|
||||
path=dynamic_path / f"{local_user.sub_id}_{dynamic_upload_time}.jpg",
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"B站订阅:获取用户动态 发送错误 {type(e)}:{e}")
|
||||
finally:
|
||||
await page.close()
|
||||
return (
|
||||
image(
|
||||
f"{local_user.sub_id}_{dynamic_upload_time}.jpg",
|
||||
"bilibili_sub/dynamic",
|
||||
),
|
||||
dynamic_upload_time,
|
||||
image = await AsyncPlaywright.screenshot(
|
||||
f"https://t.bilibili.com/{dynamic_id}",
|
||||
DYNAMIC_PATH / f"sub_{local_user.sub_id}.png",
|
||||
".bili-dyn-item__main",
|
||||
wait_until="networkidle",
|
||||
)
|
||||
return None, None
|
||||
return (
|
||||
image,
|
||||
dynamic_upload_time,
|
||||
f"https://t.bilibili.com/{dynamic_id}",
|
||||
)
|
||||
return None, 0, ""
|
||||
|
||||
|
||||
class SubManager:
|
||||
|
||||
+118
-184
@@ -1,43 +1,58 @@
|
||||
from services.log import logger
|
||||
from services.db_context import db
|
||||
from datetime import datetime
|
||||
from typing import Optional, List
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
from services.log import logger
|
||||
|
||||
|
||||
class BilibiliSub(db.Model):
|
||||
__tablename__ = "bilibili_sub"
|
||||
class BilibiliSub(Model):
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True)
|
||||
sub_id = db.Column(db.Integer(), nullable=False)
|
||||
sub_type = db.Column(db.String(), nullable=False)
|
||||
# 订阅用户
|
||||
sub_users = db.Column(db.String(), nullable=False)
|
||||
# 直播
|
||||
live_short_id = db.Column(db.Integer())
|
||||
live_status = db.Column(db.Integer)
|
||||
# 主播/UP
|
||||
uid = db.Column(db.BigInteger())
|
||||
uname = db.Column(db.String())
|
||||
latest_video_created = db.Column(db.BigInteger()) # 视频上传时间
|
||||
dynamic_upload_time = db.Column(db.BigInteger(), default=0) # 动态发布时间
|
||||
# 番剧
|
||||
season_name = db.Column(db.String())
|
||||
season_id = db.Column(db.Integer())
|
||||
season_current_episode = db.Column(db.String())
|
||||
season_update_time = db.Column(db.DateTime())
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
sub_id = fields.IntField()
|
||||
"""订阅id"""
|
||||
sub_type = fields.CharField(255)
|
||||
"""订阅类型"""
|
||||
sub_users = fields.TextField()
|
||||
"""订阅用户"""
|
||||
live_short_id = fields.IntField(null=True)
|
||||
"""直播短id"""
|
||||
live_status = fields.IntField(null=True)
|
||||
"""直播状态 0: 停播 1: 直播"""
|
||||
uid = fields.BigIntField(null=True)
|
||||
"""主播/UP UID"""
|
||||
uname = fields.CharField(255, null=True)
|
||||
"""主播/UP 名称"""
|
||||
latest_video_created = fields.BigIntField(null=True)
|
||||
"""最后视频上传时间"""
|
||||
dynamic_upload_time = fields.BigIntField(null=True, default=0)
|
||||
"""动态发布时间"""
|
||||
season_name = fields.CharField(255, null=True)
|
||||
"""番剧名称"""
|
||||
season_id = fields.IntField(null=True)
|
||||
"""番剧id"""
|
||||
season_current_episode = fields.CharField(255, null=True)
|
||||
"""番剧最新集数"""
|
||||
season_update_time = fields.DateField(null=True)
|
||||
"""番剧更新日期"""
|
||||
|
||||
_idx1 = db.Index("bilibili_sub_idx1", "sub_id", "sub_type", unique=True)
|
||||
class Meta:
|
||||
table = "bilibili_sub"
|
||||
table_description = "B站订阅数据表"
|
||||
unique_together = ("sub_id", "sub_type")
|
||||
|
||||
@classmethod
|
||||
async def add_bilibili_sub(
|
||||
async def sub_handle(
|
||||
cls,
|
||||
sub_id: int,
|
||||
sub_type: str,
|
||||
sub_user: str,
|
||||
sub_type: Optional[str] = None,
|
||||
sub_user: str = "",
|
||||
*,
|
||||
live_short_id: Optional[int] = None,
|
||||
live_status: Optional[int] = None,
|
||||
dynamic_upload_time: Optional[int] = None,
|
||||
dynamic_upload_time: int = 0,
|
||||
uid: Optional[int] = None,
|
||||
uname: Optional[str] = None,
|
||||
latest_video_created: Optional[int] = None,
|
||||
@@ -47,9 +62,9 @@ class BilibiliSub(db.Model):
|
||||
season_update_time: Optional[datetime] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
添加订阅
|
||||
参数:
|
||||
参数:
|
||||
:param sub_id: 订阅名称,房间号,番剧号等
|
||||
:param sub_type: 订阅类型
|
||||
:param sub_user: 订阅此条目的用户
|
||||
@@ -64,181 +79,100 @@ class BilibiliSub(db.Model):
|
||||
:param season_current_episode: 番剧最新集数
|
||||
:param season_update_time: 番剧更新时间
|
||||
"""
|
||||
try:
|
||||
async with db.transaction():
|
||||
query = (
|
||||
await cls.query.where(cls.sub_id == sub_id)
|
||||
.with_for_update()
|
||||
.gino.first()
|
||||
)
|
||||
sub_user = sub_user if sub_user[-1] == "," else f"{sub_user},"
|
||||
if query:
|
||||
if sub_user not in query.sub_users:
|
||||
sub_users = query.sub_users + sub_user
|
||||
await query.update(sub_users=sub_users).apply()
|
||||
else:
|
||||
sub = await cls.create(
|
||||
sub_id=sub_id, sub_type=sub_type, sub_users=sub_user
|
||||
)
|
||||
await sub.update(
|
||||
live_short_id=live_short_id
|
||||
if live_short_id
|
||||
else sub.live_short_id,
|
||||
live_status=live_status if live_status else sub.live_status,
|
||||
dynamic_upload_time=dynamic_upload_time
|
||||
if dynamic_upload_time
|
||||
else sub.dynamic_upload_time,
|
||||
uid=uid if uid else sub.uid,
|
||||
uname=uname if uname else sub.uname,
|
||||
latest_video_created=latest_video_created
|
||||
if latest_video_created
|
||||
else sub.latest_video_created,
|
||||
season_update_time=season_update_time
|
||||
if season_update_time
|
||||
else sub.season_update_time,
|
||||
season_current_episode=season_current_episode
|
||||
if season_current_episode
|
||||
else sub.season_current_episode,
|
||||
season_id=season_id if season_id else sub.season_id,
|
||||
season_name=season_name if season_name else sub.season_name,
|
||||
).apply()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.info(f"bilibili_sub 添加订阅错误 {type(e)}: {e}")
|
||||
return False
|
||||
# try:
|
||||
data = {
|
||||
"sub_type": sub_type,
|
||||
"sub_user": sub_user,
|
||||
"live_short_id": live_short_id,
|
||||
"live_status": live_status,
|
||||
"dynamic_upload_time": dynamic_upload_time,
|
||||
"uid": uid,
|
||||
"uname": uname,
|
||||
"latest_video_created": latest_video_created,
|
||||
"season_name": season_name,
|
||||
"season_id": season_id,
|
||||
"season_current_episode": season_current_episode,
|
||||
"season_update_time": season_update_time,
|
||||
}
|
||||
if sub_user:
|
||||
sub_user = sub_user if sub_user[-1] == "," else f"{sub_user},"
|
||||
sub = None
|
||||
if sub_type:
|
||||
sub = await cls.get_or_none(sub_id=sub_id, sub_type=sub_type)
|
||||
else:
|
||||
sub = await cls.get_or_none(sub_id=sub_id)
|
||||
if sub:
|
||||
sub_users = sub.sub_users + sub_user
|
||||
data["sub_type"] = sub_type or sub.sub_type
|
||||
data["sub_users"] = sub_users
|
||||
data["live_short_id"] = live_short_id or sub.live_short_id
|
||||
data["live_status"] = (
|
||||
live_status if live_status is not None else sub.live_status
|
||||
)
|
||||
data["dynamic_upload_time"] = dynamic_upload_time or sub.dynamic_upload_time
|
||||
data["uid"] = uid or sub.uid
|
||||
data["uname"] = uname or sub.uname
|
||||
data["latest_video_created"] = (
|
||||
latest_video_created or sub.latest_video_created
|
||||
)
|
||||
data["season_name"] = season_name or sub.season_name
|
||||
data["season_id"] = season_id or sub.season_id
|
||||
data["season_current_episode"] = (
|
||||
season_current_episode or sub.season_current_episode
|
||||
)
|
||||
data["season_update_time"] = season_update_time or sub.season_update_time
|
||||
else:
|
||||
await cls.create(sub_id=sub_id, sub_type=sub_type, sub_users=sub_user)
|
||||
await cls.update_or_create(sub_id=sub_id, defaults=data)
|
||||
return True
|
||||
# except Exception as e:
|
||||
# logger.info(f"bilibili_sub 添加订阅错误 {type(e)}: {e}")
|
||||
# return False
|
||||
|
||||
@classmethod
|
||||
async def delete_bilibili_sub(cls, sub_id: int, sub_user: str) -> bool:
|
||||
async def delete_bilibili_sub(
|
||||
cls, sub_id: int, sub_user: str, sub_type: Optional[str] = None
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
删除订阅
|
||||
参数:
|
||||
参数:
|
||||
:param sub_id: 订阅名称
|
||||
:param sub_user: 删除此条目的用户
|
||||
"""
|
||||
try:
|
||||
async with db.transaction():
|
||||
query = (
|
||||
await cls.query.where(
|
||||
(cls.sub_id == sub_id) & (cls.sub_users.contains(sub_user))
|
||||
)
|
||||
.with_for_update()
|
||||
.gino.first()
|
||||
)
|
||||
if not query:
|
||||
return False
|
||||
await query.update(
|
||||
sub_users=query.sub_users.replace(f"{sub_user},", "")
|
||||
).apply()
|
||||
if not query.sub_users.strip():
|
||||
await query.delete()
|
||||
return True
|
||||
if sub_type:
|
||||
sub = await cls.filter(
|
||||
sub_id=sub_id, sub_type=sub_type, sub_users__contains=sub_user
|
||||
).first()
|
||||
else:
|
||||
sub = await cls.filter(
|
||||
sub_id=sub_id, sub_users__contains=sub_user
|
||||
).first()
|
||||
if not sub:
|
||||
return False
|
||||
sub.sub_users = sub.sub_users.replace(f"{sub_user},", "")
|
||||
if sub.sub_users.strip():
|
||||
await sub.save(update_fields=["sub_users"])
|
||||
else:
|
||||
await sub.delete()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.info(f"bilibili_sub 删除订阅错误 {type(e)}: {e}")
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_sub(cls, sub_id: int) -> Optional["BilibiliSub"]:
|
||||
"""
|
||||
说明:
|
||||
获取订阅对象
|
||||
参数:
|
||||
:param sub_id: 订阅 id
|
||||
"""
|
||||
return await cls.query.where(cls.sub_id == sub_id).gino.first()
|
||||
|
||||
@classmethod
|
||||
async def get_sub_data(cls, id_: str) -> List["BilibiliSub"]:
|
||||
"""
|
||||
获取 id_ 订阅的所有内容
|
||||
:param id_: id
|
||||
"""
|
||||
query = cls.query.where(cls.sub_users.contains(id_))
|
||||
return await query.gino.all()
|
||||
|
||||
@classmethod
|
||||
async def update_sub_info(
|
||||
cls,
|
||||
sub_id: int,
|
||||
*,
|
||||
live_short_id: Optional[int] = None,
|
||||
live_status: Optional[int] = None,
|
||||
dynamic_upload_time: Optional[int] = None,
|
||||
uid: Optional[int] = None,
|
||||
uname: Optional[str] = None,
|
||||
latest_video_created: Optional[int] = None,
|
||||
season_name: Optional[str] = None,
|
||||
season_id: Optional[int] = None,
|
||||
season_current_episode: Optional[str] = None,
|
||||
season_update_time: Optional[datetime] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
更新订阅信息
|
||||
参数:
|
||||
:param sub_id: 订阅名称,房间号,番剧号等
|
||||
:param live_short_id: 直接短 id
|
||||
:param live_status: 主播开播状态
|
||||
:param dynamic_upload_time: 主播/UP最新动态时间
|
||||
:param uid: 主播/UP uid
|
||||
:param uname: 用户名称
|
||||
:param latest_video_created: 最新视频上传时间
|
||||
:param season_name: 番剧名称
|
||||
:param season_id: 番剧 season_id
|
||||
:param season_current_episode: 番剧最新集数
|
||||
:param season_update_time: 番剧更新时间
|
||||
"""
|
||||
try:
|
||||
async with db.transaction():
|
||||
sub = (
|
||||
await cls.query.where(cls.sub_id == sub_id)
|
||||
.with_for_update()
|
||||
.gino.first()
|
||||
)
|
||||
if sub:
|
||||
await sub.update(
|
||||
live_short_id=live_short_id
|
||||
if live_short_id is not None
|
||||
else sub.live_short_id,
|
||||
live_status=live_status
|
||||
if live_status is not None
|
||||
else sub.live_status,
|
||||
dynamic_upload_time=dynamic_upload_time
|
||||
if dynamic_upload_time is not None
|
||||
else sub.dynamic_upload_time,
|
||||
uid=uid if uid is not None else sub.uid,
|
||||
uname=uname if uname is not None else sub.uname,
|
||||
latest_video_created=latest_video_created
|
||||
if latest_video_created is not None
|
||||
else sub.latest_video_created,
|
||||
season_update_time=season_update_time
|
||||
if season_update_time is not None
|
||||
else sub.season_update_time,
|
||||
season_current_episode=season_current_episode
|
||||
if season_current_episode is not None
|
||||
else sub.season_current_episode,
|
||||
season_id=season_id if season_id is not None else sub.season_id,
|
||||
season_name=season_name
|
||||
if season_name is not None
|
||||
else sub.season_name,
|
||||
).apply()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.info(f"bilibili_sub 更新订阅错误 {type(e)}: {e}")
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
async def get_all_sub_data(
|
||||
cls,
|
||||
) -> "List[BilibiliSub], List[BilibiliSub], List[BilibiliSub]":
|
||||
) -> Tuple[List["BilibiliSub"], List["BilibiliSub"], List["BilibiliSub"]]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
分类获取所有数据
|
||||
"""
|
||||
live_data = []
|
||||
up_data = []
|
||||
season_data = []
|
||||
query = await cls.query.gino.all()
|
||||
query = await cls.all()
|
||||
for x in query:
|
||||
if x.sub_type == "live":
|
||||
live_data.append(x)
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
from utils.image_utils import BuildImage
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from utils.http_utils import AsyncHttpx
|
||||
from utils.http_utils import AsyncHttpx, get_user_agent
|
||||
# from bilibili_api import user
|
||||
from bilireq.user import get_user_info
|
||||
from httpx import AsyncClient
|
||||
from io import BytesIO
|
||||
|
||||
|
||||
BORDER_PATH = IMAGE_PATH / "border"
|
||||
BORDER_PATH.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
BASE_URL = "https://api.bilibili.com"
|
||||
|
||||
async def get_pic(url: str) -> bytes:
|
||||
"""
|
||||
@@ -78,7 +79,6 @@ async def get_meta(media_id: int, auth=None, reqtype="both", **kwargs):
|
||||
"""
|
||||
from bilireq.utils import get
|
||||
|
||||
BASE_URL = "https://api.bilibili.com"
|
||||
url = f"{BASE_URL}/pgc/review/user"
|
||||
params = {"media_id": media_id}
|
||||
raw_json = await get(url, raw=True, params=params, auth=auth, reqtype=reqtype, **kwargs)
|
||||
@@ -86,8 +86,7 @@ async def get_meta(media_id: int, auth=None, reqtype="both", **kwargs):
|
||||
|
||||
|
||||
async def get_videos(
|
||||
uid: int, tid: int = 0, pn: int = 1, keyword: str = "", order: str = "pubdate",
|
||||
*, auth=None, reqtype="both", **kwargs
|
||||
uid: int, tid: int = 0, pn: int = 1, keyword: str = "", order: str = "pubdate"
|
||||
):
|
||||
"""
|
||||
获取用户投该视频信息
|
||||
@@ -100,15 +99,45 @@ async def get_videos(
|
||||
:param keyword: 搜索关键词
|
||||
:param order: 排序方式,可以为 “pubdate(上传日期从新到旧), stow(收藏从多到少), click(播放量从多到少)”
|
||||
"""
|
||||
from bilireq.utils import get
|
||||
BASE_URL = "https://api.bilibili.com"
|
||||
from bilireq.utils import ResponseCodeError
|
||||
|
||||
url = f"{BASE_URL}/x/space/arc/search"
|
||||
params = {
|
||||
"mid": uid,
|
||||
"ps": 30,
|
||||
"tid": tid,
|
||||
"pn": pn,
|
||||
"keyword": keyword,
|
||||
"order": order
|
||||
}
|
||||
return await get(url, params=params, auth=auth, reqtype=reqtype, **kwargs)
|
||||
headers = get_user_agent()
|
||||
headers["Referer"] = f"https://space.bilibili.com/{uid}/video"
|
||||
async with AsyncClient() as client:
|
||||
r = await client.head(
|
||||
"https://space.bilibili.com",
|
||||
headers=headers,
|
||||
)
|
||||
params = {
|
||||
"mid": uid,
|
||||
"ps": 30,
|
||||
"tid": tid,
|
||||
"pn": pn,
|
||||
"keyword": keyword,
|
||||
"order": order,
|
||||
}
|
||||
raw_json = (
|
||||
await client.get(url, params=params, headers=headers, cookies=r.cookies)
|
||||
).json()
|
||||
if raw_json["code"] != 0:
|
||||
raise ResponseCodeError(
|
||||
code=raw_json["code"],
|
||||
msg=raw_json["message"],
|
||||
data=raw_json.get("data", None),
|
||||
)
|
||||
return raw_json["data"]
|
||||
|
||||
async def get_user_card(mid, photo: bool = False, auth=None, reqtype="both", **kwargs):
|
||||
from bilireq.utils import get
|
||||
|
||||
url = f"{BASE_URL}/x/web-interface/card"
|
||||
return (
|
||||
await get(
|
||||
url,
|
||||
params={"mid": mid, "photo": photo},
|
||||
auth=auth,
|
||||
reqtype=reqtype,
|
||||
**kwargs,
|
||||
)
|
||||
)["card"]
|
||||
|
||||
@@ -1,28 +1,30 @@
|
||||
from datetime import datetime
|
||||
from typing import Any, List, Tuple
|
||||
|
||||
from nonebot import on_command, on_message, on_regex
|
||||
from nonebot.adapters.onebot.v11 import (
|
||||
Bot,
|
||||
Event,
|
||||
MessageEvent,
|
||||
GroupMessageEvent,
|
||||
Message,
|
||||
Bot,
|
||||
MessageEvent,
|
||||
)
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_preprocessor
|
||||
from nonebot.params import CommandArg, RegexGroup
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
from configs.config import NICKNAME, Config
|
||||
from models.ban_user import BanUser
|
||||
from services.log import logger
|
||||
from utils.image_utils import BuildImage
|
||||
from utils.manager import group_manager
|
||||
from utils.utils import get_message_text, is_number
|
||||
from nonebot.params import RegexGroup, CommandArg
|
||||
from .utils import black_word_manager
|
||||
from nonebot import on_command, on_message, on_regex
|
||||
from configs.config import Config, NICKNAME
|
||||
from nonebot.permission import SUPERUSER
|
||||
from .data_source import show_black_text_image, set_user_punish
|
||||
from services.log import logger
|
||||
from models.ban_user import BanUser
|
||||
from datetime import datetime
|
||||
from utils.message_builder import image
|
||||
from .model import BlackWord
|
||||
from typing import Tuple, Any
|
||||
from utils.utils import get_message_text, is_number
|
||||
|
||||
from .data_source import set_user_punish, show_black_text_image
|
||||
from .model import BlackWord
|
||||
from .utils import black_word_manager
|
||||
|
||||
__zx_plugin_name__ = "敏感词检测"
|
||||
__plugin_usage__ = """
|
||||
@@ -38,7 +40,7 @@ usage:
|
||||
设置惩罚id需要通过 '记录名单u:xxxxxxxx' 获取
|
||||
指令:
|
||||
记录名单
|
||||
设置惩罚 [user_id] [id] [punish_level]
|
||||
设置惩罚 [user_id] [下标] [惩罚等级]
|
||||
示例:记录名单
|
||||
示例:记录名单u:12345678
|
||||
示例:设置惩罚 12345678 1 4
|
||||
@@ -54,7 +56,13 @@ __plugin_settings__ = {
|
||||
|
||||
|
||||
Config.add_plugin_config(
|
||||
"black_word", "CYCLE_DAYS", 30, name="敏感词检测与惩罚", help_="黑名单词汇记录周期", default_value=30
|
||||
"black_word",
|
||||
"CYCLE_DAYS",
|
||||
30,
|
||||
name="敏感词检测与惩罚",
|
||||
help_="黑名单词汇记录周期",
|
||||
default_value=30,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -63,10 +71,11 @@ Config.add_plugin_config(
|
||||
[5, 1, 1, 1, 1],
|
||||
help_="各个级别惩罚的容忍次数,依次为:1, 2, 3, 4, 5",
|
||||
default_value=[5, 1, 1, 1, 1],
|
||||
type=List[int],
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
"black_word", "AUTO_PUNISH", True, help_="是否启动自动惩罚机制", default_value=True
|
||||
"black_word", "AUTO_PUNISH", True, help_="是否启动自动惩罚机制", default_value=True, type=bool
|
||||
)
|
||||
|
||||
# Config.add_plugin_config(
|
||||
@@ -79,6 +88,7 @@ Config.add_plugin_config(
|
||||
360,
|
||||
help_="Union[int, List[int, int]]Ban时长(分钟),四级惩罚,可以为指定数字或指定列表区间(随机),例如 [30, 360]",
|
||||
default_value=360,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -87,6 +97,7 @@ Config.add_plugin_config(
|
||||
7,
|
||||
help_="Union[int, List[int, int]]Ban时长(天),三级惩罚,可以为指定数字或指定列表区间(随机),例如 [7, 30]",
|
||||
default_value=360,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -103,6 +114,7 @@ Config.add_plugin_config(
|
||||
True,
|
||||
help_="自动提级机制,当周期内处罚次数大于某一特定值就提升惩罚等级",
|
||||
default_value=True,
|
||||
type=bool,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -111,6 +123,7 @@ Config.add_plugin_config(
|
||||
3,
|
||||
help_="在CYCLE_DAYS周期内触发指定惩罚次数后提升惩罚等级",
|
||||
default_value=3,
|
||||
type=int,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -119,6 +132,7 @@ Config.add_plugin_config(
|
||||
False,
|
||||
help_="当未检测到已收录的敏感词时,开启ALAPI文本检测并将疑似文本发送给超级用户",
|
||||
default_value=False,
|
||||
type=bool,
|
||||
)
|
||||
|
||||
Config.add_plugin_config(
|
||||
@@ -127,6 +141,7 @@ Config.add_plugin_config(
|
||||
True,
|
||||
help_="当文本包含任意敏感词时,停止向下级插件传递,即不触发ai",
|
||||
default_value=True,
|
||||
type=bool,
|
||||
)
|
||||
|
||||
message_matcher = on_message(priority=1, block=False)
|
||||
@@ -150,24 +165,32 @@ async def _(
|
||||
matcher: Matcher,
|
||||
event: Event,
|
||||
):
|
||||
msg = get_message_text(event.json())
|
||||
if (
|
||||
isinstance(event, MessageEvent)
|
||||
and event.is_tome()
|
||||
and matcher.plugin_name == "black_word"
|
||||
and not await BanUser.is_ban(event.user_id)
|
||||
and str(event.user_id) not in bot.config.superusers
|
||||
and not get_message_text(event.json()).startswith("原神绑定")
|
||||
and not msg.startswith("原神绑定")
|
||||
):
|
||||
# 屏蔽群权限-1的群
|
||||
if isinstance(event, GroupMessageEvent) and group_manager.get_group_level(event.group_id) < 0:
|
||||
return
|
||||
user_id = event.user_id
|
||||
group_id = event.group_id if isinstance(event, GroupMessageEvent) else None
|
||||
msg = get_message_text(event.json())
|
||||
if await black_word_manager.check(user_id, group_id, msg) and Config.get_config(
|
||||
"black_word", "CONTAIN_BLACK_STOP_PROPAGATION"
|
||||
# if str(event.user_id) not in bot.config.superusers:
|
||||
# return logger.debug(f"超级用户跳过黑名单词汇检查 Message: {msg}", target=event.user_id)
|
||||
if (
|
||||
event.is_tome()
|
||||
and matcher.plugin_name == "black_word"
|
||||
and not await BanUser.is_ban(event.user_id)
|
||||
):
|
||||
matcher.stop_propagation()
|
||||
# 屏蔽群权限-1的群
|
||||
if (
|
||||
isinstance(event, GroupMessageEvent)
|
||||
and group_manager.get_group_level(event.group_id) < 0
|
||||
):
|
||||
return
|
||||
user_id = event.user_id
|
||||
group_id = event.group_id if isinstance(event, GroupMessageEvent) else None
|
||||
msg = get_message_text(event.json())
|
||||
if await black_word_manager.check(
|
||||
user_id, group_id, msg
|
||||
) and Config.get_config("black_word", "CONTAIN_BLACK_STOP_PROPAGATION"):
|
||||
matcher.stop_propagation()
|
||||
|
||||
|
||||
@show_black.handle()
|
||||
|
||||
+64
-70
@@ -1,47 +1,34 @@
|
||||
from services.db_context import db
|
||||
from typing import Optional, List
|
||||
from datetime import datetime, timedelta
|
||||
from typing import List, Optional
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from services.db_context import Model
|
||||
|
||||
|
||||
class BlackWord(db.Model):
|
||||
__tablename__ = "black_word"
|
||||
class BlackWord(Model):
|
||||
# __tablename__ = "black_word"
|
||||
|
||||
id = db.Column(db.Integer(), primary_key=True, autoincrement=True)
|
||||
user_qq = db.Column(db.BigInteger(), nullable=False, primary_key=True)
|
||||
group_id = db.Column(db.BigInteger())
|
||||
plant_text = db.Column(db.String())
|
||||
black_word = db.Column(db.String())
|
||||
punish = db.Column(db.String(), default="")
|
||||
punish_level = db.Column(db.Integer())
|
||||
create_time = db.Column(db.DateTime(timezone=True), nullable=False)
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
"""用户id"""
|
||||
group_id = fields.BigIntField(null=True)
|
||||
"""群聊id"""
|
||||
plant_text = fields.TextField()
|
||||
"""检测文本"""
|
||||
black_word = fields.TextField()
|
||||
"""黑名单词语"""
|
||||
punish = fields.TextField(default="")
|
||||
"""惩罚内容"""
|
||||
punish_level = fields.IntField()
|
||||
"""惩罚等级"""
|
||||
create_time = fields.DatetimeField(auto_now_add=True)
|
||||
"""创建时间"""
|
||||
|
||||
@classmethod
|
||||
async def add_user_black_word(
|
||||
cls,
|
||||
user_qq: int,
|
||||
group_id: Optional[int],
|
||||
black_word: str,
|
||||
plant_text: str,
|
||||
punish_level: int,
|
||||
):
|
||||
"""
|
||||
说明:
|
||||
添加用户发送的敏感词
|
||||
参数:
|
||||
:param user_qq: 用户id
|
||||
:param group_id: 群号
|
||||
:param black_word: 黑名单词汇
|
||||
:param plant_text: 消息文本
|
||||
:param punish_level: 惩罚等级
|
||||
"""
|
||||
await cls.create(
|
||||
user_qq=user_qq,
|
||||
group_id=group_id,
|
||||
plant_text=plant_text,
|
||||
black_word=black_word,
|
||||
punish_level=punish_level,
|
||||
create_time=datetime.now(),
|
||||
)
|
||||
class Meta:
|
||||
table = "black_word"
|
||||
table_description = "惩罚机制数据表"
|
||||
|
||||
@classmethod
|
||||
async def set_user_punish(
|
||||
@@ -52,9 +39,9 @@ class BlackWord(db.Model):
|
||||
id_: Optional[int] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
设置处罚
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: 用户id
|
||||
:param punish: 处罚
|
||||
:param black_word: 黑名单词汇
|
||||
@@ -63,17 +50,21 @@ class BlackWord(db.Model):
|
||||
user = None
|
||||
if (not black_word and not id_) or not punish:
|
||||
return False
|
||||
query = cls.query.where(cls.user_qq == user_qq).with_for_update()
|
||||
if black_word:
|
||||
user = await query.where(cls.black_word == black_word).order_by(cls.id.desc()).gino.first()
|
||||
user = (
|
||||
await cls.filter(user_qq=user_qq, black_word=black_word)
|
||||
.order_by("id")
|
||||
.first()
|
||||
)
|
||||
elif id_:
|
||||
user_list = await query.gino.all()
|
||||
user_list = await cls.filter(user_qq=user_qq).order_by("id").all()
|
||||
if len(user_list) == 0 or (id_ < 0 or id_ > len(user_list)):
|
||||
return False
|
||||
user = user_list[id_]
|
||||
if not user:
|
||||
return False
|
||||
await user.update(punish=cls.punish + punish + " ").apply()
|
||||
user.punish = f"{user.punish}{punish} "
|
||||
await user.save(update_fields=["punish"])
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
@@ -81,39 +72,40 @@ class BlackWord(db.Model):
|
||||
cls, user_qq: int, days: int = 7, punish_level: Optional[int] = None
|
||||
) -> int:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取用户规定周期内的犯事次数
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param days: 周期天数
|
||||
:param punish_level: 惩罚等级
|
||||
"""
|
||||
setattr(BlackWord, "count", db.func.count(cls.id).label("count"))
|
||||
query = cls.select("count").where(
|
||||
(cls.user_qq == user_qq)
|
||||
& (cls.punish_level != -1)
|
||||
& (cls.create_time > datetime.now() - timedelta(days=days))
|
||||
query = cls.filter(
|
||||
user_qq=user_qq,
|
||||
create_time__gte=datetime.now() - timedelta(days=days),
|
||||
punish_level__not_in=[-1],
|
||||
)
|
||||
if punish_level is not None:
|
||||
query = query.where(cls.punish_level == punish_level)
|
||||
return (await query.gino.first())[0]
|
||||
query = query.filter(punish_level=punish_level)
|
||||
return await query.count()
|
||||
|
||||
@classmethod
|
||||
async def get_user_punish_level(cls, user_qq: int, days: int = 7) -> Optional[int]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
获取用户最近一次的惩罚记录等级
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param days: 周期天数
|
||||
"""
|
||||
if (
|
||||
query := await cls.query.where(cls.user_qq == user_qq)
|
||||
.where(cls.create_time > datetime.now() - timedelta(days=days))
|
||||
.order_by(cls.id.desc())
|
||||
.gino.first()
|
||||
user := await cls.filter(
|
||||
user_qq=user_qq,
|
||||
create_time__gte=datetime.now() - timedelta(days=days),
|
||||
)
|
||||
.order_by("id")
|
||||
.first()
|
||||
):
|
||||
return query.punish_level
|
||||
return user.punish_level
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
@@ -125,24 +117,26 @@ class BlackWord(db.Model):
|
||||
date_type: str = "=",
|
||||
) -> List["BlackWord"]:
|
||||
"""
|
||||
说明:
|
||||
说明:
|
||||
通过指定条件查询数据
|
||||
参数:
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param group_id: 群号
|
||||
:param date: 日期
|
||||
:param date_type: 日期查询类型
|
||||
"""
|
||||
query = cls.query
|
||||
query = cls
|
||||
if user_qq:
|
||||
query = query.where(cls.user_qq == user_qq)
|
||||
query = query.filter(user_qq=user_qq)
|
||||
if group_id:
|
||||
query = query.where(cls.group_id == group_id)
|
||||
query = query.filter(group_id=group_id)
|
||||
if date:
|
||||
if date_type == "=":
|
||||
query = query.where(cls.create_time == date)
|
||||
query = query.filter(
|
||||
create_time__range=[date, date + timedelta(days=1)]
|
||||
)
|
||||
elif date_type == ">":
|
||||
query = query.where(cls.create_time > date)
|
||||
query = query.filter(create_time__gte=date)
|
||||
elif date_type == "<":
|
||||
query = query.where(cls.create_time < date)
|
||||
return await query.gino.all()
|
||||
query = query.filter(create_time__lte=date)
|
||||
return await query.order_by("id").all()
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user