From b9a4d7dcda76ef12fd4a233318a26347ead4e082 Mon Sep 17 00:00:00 2001 From: MxEmerson <2382413024@qq.com> Date: Tue, 17 Sep 2024 23:54:46 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E5=8E=BB=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E5=BC=95=E7=94=A8=EF=BC=8C=E6=9B=B4=E6=AD=A3=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=B3=A8=E8=A7=A3=EF=BC=8C=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=85=BC=E5=AE=B9pydantic=20v1=20&=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/rpc/__init__.py | 11 ++++------- src/plugins/auto_accept/__init__.py | 12 ++++++------ src/plugins/block/__init__.py | 4 ++-- src/plugins/chat/__init__.py | 8 ++++---- src/plugins/drink/__init__.py | 8 ++++---- src/plugins/greeting/__init__.py | 4 ++-- src/plugins/repeater/__init__.py | 4 ++-- src/plugins/roulette/__init__.py | 8 ++++---- src/plugins/sing/__init__.py | 8 ++++---- src/plugins/sing/ncm_loader.py | 13 ++++++++++--- 10 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/common/utils/rpc/__init__.py b/src/common/utils/rpc/__init__.py index 6d968507..6b97b9a7 100644 --- a/src/common/utils/rpc/__init__.py +++ b/src/common/utils/rpc/__init__.py @@ -1,19 +1,16 @@ import json import grpc -from typing import List, Dict, Tuple, Optional, Type, TypeVar, Any +from typing import List, Dict, Tuple, Optional, Any from src.common.utils.rpc import pymongo_rpc_pb2, pymongo_rpc_pb2_grpc from src.common.config import plugin_config -T = TypeVar('T') - - class CollectionProxy: - def __init__(self, rpc_client: Type['MongoClient'], collection_name: str): + def __init__(self, rpc_client: 'MongoClient', collection_name: str): self.rpc_client = rpc_client self.collection_name = collection_name - def __getitem__(self: T, collection_name: str) -> T: + def __getitem__(self, collection_name: str) -> 'CollectionProxy': return CollectionProxy(self.rpc_client, collection_name) def find(self, filter: Dict = {}) -> List[Dict[str, Any]]: @@ -39,7 +36,7 @@ def create_index(self, keys: List[Tuple], name: Optional[str] = None, default_la class MongoClient: - def __init__(self, mongo_host: str, mongo_port: str, **kwargs): + def __init__(self, mongo_host: str, mongo_port: int, **kwargs): self.channel = grpc.insecure_channel(f'{mongo_host}:{mongo_port}') self.stub = pymongo_rpc_pb2_grpc.MongoDBServiceStub(self.channel) self.metadata = [('authorization', plugin_config.rpc_token)] diff --git a/src/plugins/auto_accept/__init__.py b/src/plugins/auto_accept/__init__.py index c56f664c..43aad939 100644 --- a/src/plugins/auto_accept/__init__.py +++ b/src/plugins/auto_accept/__init__.py @@ -1,6 +1,5 @@ -import nonebot -from nonebot import on_command, on_request, on_notice, get_driver -from nonebot.adapters.onebot.v11 import MessageSegment, Message, FriendRequestEvent, GroupRequestEvent, Bot +from nonebot import on_request +from nonebot.adapters.onebot.v11 import FriendRequestEvent, GroupRequestEvent, Bot from nonebot.typing import T_State from src.common.config import BotConfig, GroupConfig, UserConfig @@ -12,7 +11,7 @@ @request_cmd.handle() -async def handle_request(bot: Bot, event: GroupRequestEvent, state: T_State): +async def handle_group_request(bot: Bot, event: GroupRequestEvent, state: T_State): if event.sub_type == 'invite': if GroupConfig(event.group_id).is_banned() or UserConfig(event.user_id).is_banned(): await event.reject(bot) @@ -24,11 +23,12 @@ async def handle_request(bot: Bot, event: GroupRequestEvent, state: T_State): @request_cmd.handle() -async def handle_request(bot: Bot, event: FriendRequestEvent, state: T_State): +async def handle_friend_request(bot: Bot, event: FriendRequestEvent, state: T_State): if UserConfig(event.user_id).is_banned(): await event.reject(bot) return bot_config = BotConfig(event.self_id) - if bot_config.is_admin_of_bot(event.user_id): # or bot_config.auto_accept(): # 自动加好友太容易被封号了,先关了 + # or bot_config.auto_accept(): # 自动加好友太容易被封号了,先关了 + if bot_config.is_admin_of_bot(event.user_id): await event.approve(bot) diff --git a/src/plugins/block/__init__.py b/src/plugins/block/__init__.py index 2872a6ba..c54657aa 100644 --- a/src/plugins/block/__init__.py +++ b/src/plugins/block/__init__.py @@ -3,7 +3,6 @@ import time from typing import List, Union -import nonebot from nonebot import get_bots from nonebot.adapters import Bot from nonebot.adapters.onebot.v11 import GroupIncreaseNoticeEvent, GroupMessageEvent, PokeNotifyEvent, permission @@ -36,7 +35,8 @@ def refresh_accounts(self) -> None: onebot_accounts: list[int] = [ int(self_id) for self_id, bot in get_bots().items() if self_id.isnumeric() and bot.type == 'OneBot V11' ] - self.accounts = list(set(go_cqhttp_plugin_accounts + onebot_accounts)) + self.accounts = list( + set(go_cqhttp_plugin_accounts + onebot_accounts)) async def is_other_bot(self, bot: Bot, event: GroupMessageEvent, state: T_State) -> bool: self.refresh_accounts() diff --git a/src/plugins/chat/__init__.py b/src/plugins/chat/__init__.py index 2803a2fb..f3ed353e 100644 --- a/src/plugins/chat/__init__.py +++ b/src/plugins/chat/__init__.py @@ -1,9 +1,9 @@ from asyncer import asyncify -from nonebot.adapters.onebot.v11 import MessageSegment, Message, permission, GroupMessageEvent +from nonebot.adapters.onebot.v11 import MessageSegment, permission, GroupMessageEvent from nonebot.adapters import Bot, Event from nonebot.rule import Rule from nonebot.typing import T_State -from nonebot import on_message, get_driver, logger +from nonebot import on_message, logger from src.common.config import BotConfig, GroupConfig, plugin_config @@ -30,14 +30,14 @@ @BotConfig.handle_sober_up -def on_sober_up(bot_id, group_id, drunkenness) -> bool: +def on_sober_up(bot_id, group_id, drunkenness) -> None: session = f'{bot_id}_{group_id}' logger.info( f'bot [{bot_id}] sober up in group [{group_id}], clear session [{session}]') chat.del_session(session) -def is_drunk(bot: Bot, event: Event, state: T_State) -> bool: +def is_drunk(bot: Bot, event: Event, state: T_State) -> int: config = BotConfig(event.self_id, event.group_id) return config.drunkenness() diff --git a/src/plugins/drink/__init__.py b/src/plugins/drink/__init__.py index 102c94c3..c9cd1423 100644 --- a/src/plugins/drink/__init__.py +++ b/src/plugins/drink/__init__.py @@ -1,14 +1,14 @@ import random import asyncio -from nonebot import on_message, require, get_bot, logger, get_driver +from nonebot import on_message, require, logger from nonebot.exception import ActionFailed from nonebot.typing import T_State -from nonebot.rule import keyword, to_me, Rule -from nonebot.adapters import Bot, Event +from nonebot.rule import Rule +from nonebot.adapters import Bot from nonebot.adapters.onebot.v11 import GroupMessageEvent, PrivateMessageEvent from nonebot.adapters.onebot.v11 import permission -from src.common.config import BotConfig, GroupConfig +from src.common.config import BotConfig async def is_drink_msg(bot: Bot, event: GroupMessageEvent, state: T_State) -> bool: diff --git a/src/plugins/greeting/__init__.py b/src/plugins/greeting/__init__.py index 8f90338e..0263fc6b 100644 --- a/src/plugins/greeting/__init__.py +++ b/src/plugins/greeting/__init__.py @@ -2,9 +2,9 @@ import asyncio from pathlib import Path -from nonebot import on_command, on_message, on_notice, get_driver, get_bot +from nonebot import on_message, on_notice, get_bot from nonebot.adapters.onebot.v11 import MessageSegment, Message, permission, GroupMessageEvent -from nonebot.rule import keyword, startswith, to_me, Rule +from nonebot.rule import to_me, Rule from nonebot.typing import T_State from nonebot.adapters import Bot, Event from src.common.config import BotConfig, GroupConfig, UserConfig diff --git a/src/plugins/repeater/__init__.py b/src/plugins/repeater/__init__.py index 56b03bc2..36979d93 100644 --- a/src/plugins/repeater/__init__.py +++ b/src/plugins/repeater/__init__.py @@ -4,12 +4,12 @@ import time import threading -from nonebot import on_message, on_notice, require, get_bot, logger, get_driver +from nonebot import on_message, on_notice, require, get_bot, logger from nonebot.exception import ActionFailed from nonebot.typing import T_State from nonebot.rule import keyword, to_me, Rule from nonebot.adapters import Bot, Event -from nonebot.adapters.onebot.v11 import GroupMessageEvent, PrivateMessageEvent, GroupRecallNoticeEvent +from nonebot.adapters.onebot.v11 import GroupMessageEvent, GroupRecallNoticeEvent from nonebot.adapters.onebot.v11 import permission, Message, MessageSegment from nonebot.permission import Permission from nonebot.permission import SUPERUSER diff --git a/src/plugins/roulette/__init__.py b/src/plugins/roulette/__init__.py index 310d1345..b877c9d2 100644 --- a/src/plugins/roulette/__init__.py +++ b/src/plugins/roulette/__init__.py @@ -1,12 +1,12 @@ import asyncio from collections import defaultdict from typing import Awaitable, Optional -from nonebot import on_message, on_request, get_bot, logger, get_driver +from nonebot import on_message, on_request, get_bot, logger from nonebot.typing import T_State -from nonebot.rule import keyword, to_me, Rule +from nonebot.rule import Rule from nonebot.adapters import Bot, Event from nonebot.adapters.onebot.v11 import GroupMessageEvent, GroupRequestEvent -from nonebot.adapters.onebot.v11 import MessageSegment, Message, permission, GroupMessageEvent +from nonebot.adapters.onebot.v11 import MessageSegment, permission, GroupMessageEvent from nonebot.permission import Permission from src.common.config import BotConfig, GroupConfig @@ -164,7 +164,7 @@ async def shot(self_id: int, user_id: int, group_id: int) -> Optional[Awaitable[ if self_role == 'owner': # 牛牛是群主不能退群,不然群就解散了 return None - async def group_leave(): + async def group_leave() -> None: await get_bot(str(self_id)).call_api('set_group_leave', **{ 'group_id': group_id }) diff --git a/src/plugins/sing/__init__.py b/src/plugins/sing/__init__.py index 72c294fb..a9fc3352 100644 --- a/src/plugins/sing/__init__.py +++ b/src/plugins/sing/__init__.py @@ -5,7 +5,7 @@ import time import os -from nonebot import get_driver, on_message, require, logger +from nonebot import on_message, require, logger from nonebot.typing import T_State from nonebot.rule import Rule from nonebot.adapters import Bot, Event @@ -34,10 +34,10 @@ async def is_to_sing(bot: Bot, event: Event, state: T_State) -> bool: text = event.get_plaintext() if not text: return False - + if not SING_CMD in text and not any([cmd in text for cmd in SING_CONTINUE_CMDS]): return False - + if text.endswith(SING_CMD): return False @@ -79,7 +79,7 @@ async def is_to_sing(bot: Bot, event: Event, state: T_State) -> bool: progress = GroupConfig(group_id=event.group_id).sing_progress() if not progress: return False - + song_id = progress['song_id'] chunk_index = progress['chunk_index'] key_val = progress['key'] diff --git a/src/plugins/sing/ncm_loader.py b/src/plugins/sing/ncm_loader.py index b27d8aa0..50aff0f2 100644 --- a/src/plugins/sing/ncm_loader.py +++ b/src/plugins/sing/ncm_loader.py @@ -13,7 +13,14 @@ class Config(BaseModel, extra=Extra.ignore): ncm_ctcode: int = 86 -config = Config.parse_obj(get_driver().config) +try: + # pydantic v2 + from nonebot import get_plugin_config + config = get_plugin_config(Config) +except ImportError: + # pydantic v1 + config = Config.parse_obj(get_driver().config) + if config.ncm_phone and config.ncm_password: ncm.login.LoginViaCellphone( @@ -72,7 +79,7 @@ def get_song_id(song_name: str): if res["result"]["songCount"] == 0: return None - + for song in res["result"]["songs"]: privilege = song["privilege"] if "chargeInfoList" not in privilege: @@ -86,5 +93,5 @@ def get_song_id(song_name: str): continue return song["id"] - + return None