Skip to content

Commit

Permalink
✨ pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
j1g5awi committed Mar 22, 2024
1 parent 8a8e7b4 commit b98df69
Show file tree
Hide file tree
Showing 26 changed files with 1,015 additions and 1,209 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
cancel-in-progress: true
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false
env:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ bot.py

.vscode

# direnv
.direnv

# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python

Expand Down
21 changes: 3 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
[![License](https://img.shields.io/github/license/nonepkg/nonebot-plugin-all4one?style=flat-square)](LICENSE)
![Python Version](https://img.shields.io/badge/python-3.8+-blue.svg?style=flat-square)
[![NoneBot Version](https://img.shields.io/badge/nonebot-2.0.0rc2+-red.svg?style=flat-square)](https://v2.nonebot.dev/)
[![OneBot V12](https://img.shields.io/badge/OneBot-12-black?style=flat-square)](https://12.onebot.dev/)
[![OneBot 4A](https://img.shields.io/badge/OneBot-4A-black?style=flat-square)](https://onebot4all.vercel.app/)
[![PyPI Version](https://img.shields.io/pypi/v/nonebot-plugin-all4one.svg?style=flat-square)](https://pypi.python.org/pypi/nonebot-plugin-all4one)
[![codecov](https://codecov.io/gh/nonepkg/plugin-all4one/branch/master/graph/badge.svg?token=BOK429DAHO)](https://codecov.io/gh/nonepkg/plugin-all4one)

</div>

## 安装

### 从 PyPI 安装(推荐)

- 使用 nb-cli
- 使用 nb-cli

```sh
nb plugin install nonebot-plugin-all4one
Expand All @@ -30,18 +28,6 @@ nb plugin install nonebot-plugin-all4one
pdm add nonebot-plugin-all4one
```

- 使用 pip

```sh
pip install nonebot-plugin-all4one
```

### 从 GitHub 安装(不推荐)

```sh
pdm add git+https://github.com/nonepkg/nonebot-plugin-all4one
```

## 使用

```dotenv
Expand All @@ -64,8 +50,7 @@ blocked_plugins = ["echo"] # 在 block_event=False 时生效,可自定义处

- [x] [OneBot V11](https://github.com/nonebot/adapter-onebot)
- [x] [Telegram](http://github.com/nonebot/adapter-telegram)
- [x] [QQ Guild](https://github.com/nonebot/adapter-qq) [@he0119](https://github.com/he0119)
- [x] [Villa](https://github.com/CMHopeSunshine/nonebot-adapter-villa) [@he0119](https://github.com/he0119)
- [ ] [QQ](https://github.com/nonebot/adapter-qq) [@he0119](https://github.com/he0119) 寻求新维护者

## 相关链接

Expand Down
4 changes: 0 additions & 4 deletions docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ TELEGRAM_BOTS=[]
TELEGRAM_PROXY
TELEGRAM_WEBHOOK_URL

# Villa
# https://github.com/CMHopeSunshine/nonebot-adapter-villa
VILLA_BOTS=[]

# Sentry
SENTRY_ENVIRONMENT=prod
SENTRY_DSN
6 changes: 0 additions & 6 deletions docker/bot.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import nonebot
from nonebot.adapters.villa import Adapter as VILLAAdapter
from nonebot.adapters.qqguild import Adapter as QQGUILDAdapter
from nonebot.adapters.telegram import Adapter as TELEGRAMAdapter
from nonebot.adapters.onebot.v11 import Adapter as ONEBOT_V11Adapter
from nonebot.adapters.onebot.v12 import Adapter as ONEBOT_V12Adapter

nonebot.init()

driver = nonebot.get_driver()
driver.register_adapter(VILLAAdapter)
driver.register_adapter(QQGUILDAdapter)
driver.register_adapter(TELEGRAMAdapter)
driver.register_adapter(ONEBOT_V11Adapter)
driver.register_adapter(ONEBOT_V12Adapter)

nonebot.load_plugin("nonebot_plugin_sentry")
nonebot.load_plugin("nonebot_plugin_all4one")
Expand Down
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
pkgs.stdenv.cc.cc
];
NIX_LD = pkgs.lib.fileContents "${pkgs.stdenv.cc}/nix-support/dynamic-linker";
shellHook = ''
export LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH
''
;
};
};
};
Expand Down
7 changes: 4 additions & 3 deletions nonebot_plugin_all4one/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
type="application",
homepage="https://github.com/nonepkg/nonebot-plugin-all4one",
config=Config,
supported_adapters={"~console", "~onebot.v11", "~telegram", "~qqguild"},
supported_adapters={"~onebot.v11", "~telegram"},
)

driver = get_driver()
a4o_config = Config(**driver.config.dict())
a4o_config = Config(**driver.config.model_dump())
obimpl = OneBotImplementation(driver)

on(priority=1, block=False)
Expand Down Expand Up @@ -54,6 +54,7 @@ async def _(bot: Bot, matcher: Matcher):
if (
bot.type in obimpl._middlewares
and matcher.plugin_name
and matcher.plugin_name in a4o_config.blocked_plugins # type: ignore
and a4o_config.blocked_plugins
and matcher.plugin_name in a4o_config.blocked_plugins
):
raise IgnoredException("All4One has blocked it")
4 changes: 2 additions & 2 deletions nonebot_plugin_all4one/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Set, Optional
from typing import Optional

from pydantic import BaseModel


class Config(BaseModel):
block_event: bool = True
blocked_plugins: Optional[Set[str]] = None
blocked_plugins: Optional[set[str]] = None

class Config:
extra = "ignore"
6 changes: 3 additions & 3 deletions nonebot_plugin_all4one/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hashlib import sha256
from base64 import b64decode
from uuid import UUID, uuid4
from typing import Dict, Union, Optional
from typing import Union, Optional

from anyio import open_file
from httpx import AsyncClient
Expand All @@ -24,7 +24,7 @@ class File(Model):
src: Mapped[Optional[str]]
src_id: Mapped[Optional[str]]
url: Mapped[Optional[str]]
headers: Mapped[Optional[Dict[str, str]]] = mapped_column(JSON)
headers: Mapped[Optional[dict[str, str]]] = mapped_column(JSON)
path: Mapped[Optional[str]]
sha256: Mapped[Optional[str]]

Expand Down Expand Up @@ -69,7 +69,7 @@ async def upload_file(
src: Optional[str] = None,
src_id: Optional[str] = None,
url: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
headers: Optional[dict[str, str]] = None,
path: Optional[str] = None,
data: Optional[Union[str, bytes]] = None,
sha256: Optional[str] = None,
Expand Down
60 changes: 31 additions & 29 deletions nonebot_plugin_all4one/middlewares/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Union, Literal, Optional
from typing import Any, Union, Literal, Optional

from anyio import open_file
from nonebot.adapters import Bot, Event, Message
Expand Down Expand Up @@ -27,7 +27,7 @@ def __init__(self, bot: Bot):
self.bot = bot
self._supported_actions = self._get_supported_actions()

def _get_supported_actions(self) -> List[str]:
def _get_supported_actions(self) -> list[str]:
"""获取支持的动作列表"""
supported_actions = set()
for class_ in self.__class__.__mro__:
Expand All @@ -37,7 +37,7 @@ def _get_supported_actions(self) -> List[str]:
return list(supported_actions)

@supported_action
async def get_supported_actions(self, **kwargs: Any) -> List[str]:
async def get_supported_actions(self, **kwargs: Any) -> list[str]:
"""获取支持的动作列表
参数:
Expand Down Expand Up @@ -90,7 +90,7 @@ def get_platform(self) -> str:
raise NotImplementedError

@abstractmethod
async def to_onebot_event(self, event: Event) -> List[OneBotEvent]:
async def to_onebot_event(self, event: Event) -> list[OneBotEvent]:
raise NotImplementedError

async def send_message(
Expand All @@ -103,7 +103,7 @@ async def send_message(
channel_id: Optional[str] = None,
message: Message,
**kwargs: Any,
) -> Dict[Union[Literal["message_id", "time"], str], Any]:
) -> dict[Union[Literal["message_id", "time"], str], Any]:
"""发送消息
参数:
Expand All @@ -127,11 +127,13 @@ async def delete_message(self, *, message_id: str, **kwargs: Any) -> None:

async def get_self_info(
self, **kwargs: Any
) -> Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
) -> dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
"""获取机器人自身信息"""
raise NotImplementedError

async def get_user_info(self, *, user_id: str, **kwargs: Any) -> Dict[
async def get_user_info(
self, *, user_id: str, **kwargs: Any
) -> dict[
Union[Literal["user_id", "user_name", "user_displayname", "user_remark"], str],
str,
]:
Expand All @@ -146,8 +148,8 @@ async def get_user_info(self, *, user_id: str, **kwargs: Any) -> Dict[
async def get_friend_list(
self,
**kwargs: Any,
) -> List[
Dict[
) -> list[
dict[
Union[
Literal["user_id", "user_name", "user_displayname", "user_remark"], str
],
Expand All @@ -163,7 +165,7 @@ async def get_friend_list(

async def get_group_info(
self, *, group_id: str, **kwargs: Any
) -> Dict[Union[Literal["group_id", "group_name"], str], str]:
) -> dict[Union[Literal["group_id", "group_name"], str], str]:
"""获取群信息
参数:
Expand All @@ -175,7 +177,7 @@ async def get_group_info(
async def get_group_list(
self,
**kwargs: Any,
) -> List[Dict[Union[Literal["group_id", "group_name"], str], str]]:
) -> list[dict[Union[Literal["group_id", "group_name"], str], str]]:
"""获取群列表
参数:
Expand All @@ -185,7 +187,7 @@ async def get_group_list(

async def get_group_member_info(
self, *, group_id: str, user_id: str, **kwargs: Any
) -> Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
) -> dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
"""获取群成员信息
参数:
Expand All @@ -197,8 +199,8 @@ async def get_group_member_info(

async def get_group_member_list(
self, *, group_id: str, **kwargs: Any
) -> List[
Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]
) -> list[
dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]
]:
"""获取群成员列表
Expand Down Expand Up @@ -231,7 +233,7 @@ async def leave_group(self, *, group_id: str, **kwargs: Any) -> None:

async def get_guild_info(
self, *, guild_id: str, **kwargs: Any
) -> Dict[Union[Literal["guild_id", "guild_name"], str], str]:
) -> dict[Union[Literal["guild_id", "guild_name"], str], str]:
"""获取 Guild 信息
参数:
Expand All @@ -243,7 +245,7 @@ async def get_guild_info(
async def get_guild_list(
self,
**kwargs: Any,
) -> List[Dict[Union[Literal["guild_id", "guild_name"], str], str]]:
) -> list[dict[Union[Literal["guild_id", "guild_name"], str], str]]:
"""获取群组列表
参数:
Expand All @@ -265,7 +267,7 @@ async def set_guild_name(

async def get_guild_member_info(
self, *, guild_id: str, user_id: str, **kwargs: Any
) -> Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
) -> dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
"""获取群组成员信息
参数:
Expand All @@ -277,8 +279,8 @@ async def get_guild_member_info(

async def get_guild_member_list(
self, *, guild_id: str, **kwargs: Any
) -> List[
Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]
) -> list[
dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]
]:
"""获取群组成员列表
Expand All @@ -299,7 +301,7 @@ async def leave_guild(self, *, guild_id: str, **kwargs: Any) -> None:

async def get_channel_info(
self, *, guild_id: str, channel_id: str, **kwargs: Any
) -> Dict[Union[Literal["channel_id", "channel_name"], str], str]:
) -> dict[Union[Literal["channel_id", "channel_name"], str], str]:
"""获取频道信息
参数:
Expand All @@ -311,7 +313,7 @@ async def get_channel_info(

async def get_channel_list(
self, *, guild_id: str, joined_only: bool = False, **kwargs: Any
) -> List[Dict[Union[Literal["channel_id", "channel_name"], str], str]]:
) -> list[dict[Union[Literal["channel_id", "channel_name"], str], str]]:
"""获取频道列表
参数:
Expand All @@ -336,7 +338,7 @@ async def set_channel_name(

async def get_channel_member_info(
self, *, guild_id: str, channel_id: str, user_id: str, **kwargs: Any
) -> Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
) -> dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
"""获取频道成员信息
参数:
guild_id: 群组 ID
Expand All @@ -348,8 +350,8 @@ async def get_channel_member_info(

async def get_channel_member_list(
self, *, guild_id: str, channel_id: str, **kwargs: Any
) -> List[
Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]
) -> list[
dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]
]:
"""获取频道成员列表
参数:
Expand Down Expand Up @@ -377,12 +379,12 @@ async def upload_file(
type: Union[Literal["url", "path", "data"], str],
name: str,
url: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
headers: Optional[dict[str, str]] = None,
path: Optional[str] = None,
data: Optional[bytes] = None,
sha256: Optional[str] = None,
**kwargs: Any,
) -> Dict[Union[Literal["file_id"], str], str]:
) -> dict[Union[Literal["file_id"], str], str]:
"""上传文件
参数:
Expand Down Expand Up @@ -448,7 +450,7 @@ async def upload_file_fragmented(
size: Optional[int] = None,
data: Optional[bytes] = None,
**kwargs: Any,
) -> Optional[Dict[Union[Literal["file_id"], str], str]]:
) -> Optional[dict[Union[Literal["file_id"], str], str]]:
"""分片上传文件
参数:
Expand All @@ -471,7 +473,7 @@ async def get_file(
type: Union[Literal["url", "path", "data"], str],
file_id: str,
**kwargs: Any,
) -> Dict[
) -> dict[
Union[Literal["name", "url", "headers", "path", "data", "sha256"], str], Any
]:
"""获取文件
Expand Down Expand Up @@ -506,7 +508,7 @@ async def get_file_fragmented(
offset: Optional[int] = None,
size: Optional[int] = None,
**kwargs: Any,
) -> Dict[Union[Literal["name", "total_size", "sha256", "data"], str], str]:
) -> dict[Union[Literal["name", "total_size", "sha256", "data"], str], str]:
"""分片获取文件
参数:
Expand Down
Loading

0 comments on commit b98df69

Please sign in to comment.