Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
edddce9
💥 将 UNSET 改为使用 metaclass 实现
shoucandanghehe Feb 4, 2026
336f054
🔧 为 client.pyi 添加一些 ignore
shoucandanghehe Feb 5, 2026
2c45926
✨ 添加 omit_unset 方法
shoucandanghehe Feb 5, 2026
8ab0e1b
♻️ 重构 handle
shoucandanghehe Feb 5, 2026
fb1fa90
:rotating_light: auto fix by pre-commit hooks
pre-commit-ci[bot] Feb 5, 2026
36850d9
🐛 修复可空字段显式 null 传递
shoucandanghehe Feb 5, 2026
13d091e
🐛 修复 bool 查询参数编码
shoucandanghehe Feb 5, 2026
5f2542c
🐛 对齐可空语义与位置更新
shoucandanghehe Feb 6, 2026
97c3e01
🐛 对齐 API 行为与文档约束
shoucandanghehe Feb 6, 2026
048d00b
🐛 修正响应体解析与返回类型
shoucandanghehe Feb 6, 2026
61c4bb1
🐛 完善附件与论坛标签参数
shoucandanghehe Feb 6, 2026
1e118bd
🐛 修复默认值与必填参数约束
shoucandanghehe Feb 6, 2026
e8bf4eb
🐛 补全参数范围与互斥校验
shoucandanghehe Feb 6, 2026
6dc9895
🐛 修复鉴权与路由参数不一致
shoucandanghehe Feb 6, 2026
a00ca68
🐛 修复线程参数与归档时间处理
shoucandanghehe Feb 6, 2026
dd64d08
✨ 新增 client.pyi 生成脚本
shoucandanghehe Feb 6, 2026
3144266
🔧 配置 client.pyi 自动生成并对齐格式
shoucandanghehe Feb 6, 2026
71708c7
:rotating_light: auto fix by pre-commit hooks
pre-commit-ci[bot] Feb 6, 2026
9993f0c
🐛 修复 generate-client-pyi hook 的 uv 依赖
shoucandanghehe Feb 6, 2026
a2e623a
♻️ 引入 Range 注解参数校验
shoucandanghehe Feb 7, 2026
14cdc83
🐛 修复 Ruff 警告
shoucandanghehe Feb 7, 2026
94c3da5
✨ 为参数约束补充 overload 软提示
shoucandanghehe Feb 7, 2026
364f5b6
♻️ 抽离参数校验模块并补充规则测试
shoucandanghehe Feb 7, 2026
1011d94
🐛 修复 client.pyi 生成器对 Annotated 与 overload 的处理
shoucandanghehe Feb 7, 2026
52944b5
♻️ 为已移除 guild 端点增加软弃用告警
shoucandanghehe Feb 7, 2026
91b7e0f
✅ 为已移除 guild 端点补充弃用告警测试
shoucandanghehe Feb 7, 2026
5b73f36
🐛 修复 modify_DM 图标字节编码
shoucandanghehe Feb 7, 2026
d54bd43
🔥 移除一些 TODO
shoucandanghehe Feb 7, 2026
ab34a6b
♻️ 移除 _request 的 bot 参数并同步调用
shoucandanghehe Feb 8, 2026
aa1add4
♻️ _call_api 支持无 bot 形参处理器
shoucandanghehe Feb 8, 2026
43d70ba
♻️ 移除未使用的 bot 形参
shoucandanghehe Feb 8, 2026
489fd6e
🐛 修复消息回复检查对 UNSET 的判定
shoucandanghehe Feb 8, 2026
0957053
♻️ 统一发送模型的可选字段为 Missing
shoucandanghehe Feb 8, 2026
691d18b
♻️ 统一序列化链路使用 omit_unset
shoucandanghehe Feb 8, 2026
7e08824
✅ 补充 UNSET 序列化回归测试
shoucandanghehe Feb 8, 2026
0eab329
♻️ 合并序列化入口到 model_dump
shoucandanghehe Feb 9, 2026
c8b9474
♻️ 统一 API 序列化参数为 omit_unset_values
shoucandanghehe Feb 9, 2026
6dcecbc
♻️ 同步命令存储序列化参数与类型
shoucandanghehe Feb 9, 2026
4445578
✨ 为 client.pyi 生成增加哈希校验与元信息
shoucandanghehe Feb 9, 2026
0b2e5c2
♻️ 将测试动态导入改为静态导入
shoucandanghehe Feb 9, 2026
8816594
🐛 修正 ApplicationCommandCreate 模型定义
shoucandanghehe Feb 9, 2026
c0a9443
🐛 修正 ExecuteWebhookParams 模型定义
shoucandanghehe Feb 9, 2026
cec6e4a
:memo: standardize docstrings for Application Commands section
shoucandanghehe Feb 11, 2026
3289bb1
:memo: standardize docstrings for Interactions section
shoucandanghehe Feb 11, 2026
bb609c3
:memo: standardize docstrings for Application section
shoucandanghehe Feb 11, 2026
c649fdc
:memo: standardize docstrings for Audit Log and Auto Moderation sections
shoucandanghehe Feb 11, 2026
dea6352
:memo: standardize docstrings for Channel section
shoucandanghehe Feb 11, 2026
113c370
:memo: standardize docstrings for Message section
shoucandanghehe Feb 11, 2026
08c3b2b
:memo: standardize docstrings for Channel Operations section
shoucandanghehe Feb 11, 2026
8316b70
:memo: standardize docstrings for Emoji section
shoucandanghehe Feb 11, 2026
4bde8ab
:memo: standardize docstrings for Entitlement section
shoucandanghehe Feb 11, 2026
1e22f67
:memo: standardize docstrings for Guild Core section
shoucandanghehe Feb 11, 2026
028f2fa
:memo: standardize docstrings for Guild Members section
shoucandanghehe Feb 11, 2026
596d1b6
:memo: standardize docstrings for Guild Bans, Roles, Prune section
shoucandanghehe Feb 11, 2026
6ff7857
:memo: standardize docstrings for Guild Integrations/Widget/Welcome s…
shoucandanghehe Feb 11, 2026
47605a8
:memo: standardize docstrings for Voice section
shoucandanghehe Feb 11, 2026
f44c706
:memo: standardize docstrings for Guild Scheduled Event section
shoucandanghehe Feb 11, 2026
6ca9437
:memo: standardize docstrings for Guild Template section
shoucandanghehe Feb 11, 2026
7b9b9e0
:memo: standardize docstrings for Invite, Poll, SKU sections
shoucandanghehe Feb 11, 2026
9eae11c
:memo: standardize docstrings for Stage Instance and Sticker sections
shoucandanghehe Feb 11, 2026
06dff0e
:memo: standardize docstrings for Subscription and User sections
shoucandanghehe Feb 11, 2026
e8fc16d
:memo: standardize docstrings for Webhook, Gateway, OAuth2 sections
shoucandanghehe Feb 11, 2026
d697398
:memo: standardize section comments format
shoucandanghehe Feb 11, 2026
904def7
:memo: fix docstring format for deprecated method
shoucandanghehe Feb 11, 2026
ac47ecf
:memo: fix Discord docs links for moved or removed endpoints
shoucandanghehe Feb 11, 2026
6db444b
:rotating_light: auto fix by pre-commit hooks
pre-commit-ci[bot] Feb 11, 2026
4f6d96e
:bug: fix multipart payload dropping empty attachments list
shoucandanghehe Feb 11, 2026
496a8c7
🔥 移除无用赋值
shoucandanghehe Feb 12, 2026
7a695e0
:recycle: preserve UNSET falsy behavior in metaclass implementation
shoucandanghehe Feb 12, 2026
44fa22f
:zap: cache inspect.signature results for API handlers
shoucandanghehe Feb 12, 2026
a522e97
:rotating_light: auto fix by pre-commit hooks
pre-commit-ci[bot] Feb 12, 2026
bb4efb4
:recycle: 移除一些不正确的继承关系
shoucandanghehe Feb 12, 2026
57d398c
✨ 显式导出 UNSET
shoucandanghehe Feb 12, 2026
cada676
:recycle: 重构 UNSET 类及相关类型别名
shoucandanghehe Feb 12, 2026
10651ff
:bug: 修复类型守卫不会正确收窄类型
shoucandanghehe Feb 12, 2026
fe5a29d
:bug: 修复 _sub_command_rule 中 Missing 类型的类型检查问题
shoucandanghehe Feb 12, 2026
06687ce
:bug: 修复 on_*_command 函数参数的 Missing 类型问题
shoucandanghehe Feb 12, 2026
cae9c1b
🔧 Use `language: python` for pre-commit hook portability (#61)
Copilot Feb 12, 2026
efdac55
:bug: 修复 InteractionCreateEvent 中用户上下文检查逻辑
shoucandanghehe Feb 12, 2026
66005d6
:bug: 修复命令参数解析中的 Missing 类型收窄
shoucandanghehe Feb 12, 2026
692f33f
:bug: 修复 message 中 Missing 字段类型问题
shoucandanghehe Feb 12, 2026
1323b01
:recycle: 移除 _call_api 对 legacy 参数兼容
shoucandanghehe Feb 12, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ ci:
autoupdate_schedule: monthly
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
- repo: local
hooks:
- id: generate-client-pyi
name: generate client.pyi
entry: scripts/generate_client_pyi.py
language: python
pass_filenames: false
files: ^(nonebot/adapters/discord/api/handle\.py|scripts/generate_client_pyi\.py)$
Comment thread
shoucandanghehe marked this conversation as resolved.
Resolved

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.10
hooks:
Expand Down
2 changes: 2 additions & 0 deletions nonebot/adapters/discord/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .adapter import Adapter
from .api.types import UNSET
from .bot import Bot
from .commands import (
ApplicationCommandMatcher,
Expand Down Expand Up @@ -132,6 +133,7 @@
from .utils import log as log

__all__ = [
"UNSET",
"Adapter",
"ApplicationCommandAutoCompleteInteractionEvent",
"ApplicationCommandInteractionEvent",
Expand Down
32 changes: 26 additions & 6 deletions nonebot/adapters/discord/adapter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import asyncio
from collections.abc import Mapping
import contextlib
from functools import lru_cache
import inspect
import json
import sys
from typing import Any, Optional
from typing import Any, Callable, Optional
from typing_extensions import override

from nonebot.adapters import Adapter as BaseAdapter
Expand All @@ -13,7 +16,7 @@
from nonebot.plugin import get_plugin_config
from nonebot.utils import escape_tag

from .api.handle import API_HANDLERS
from .api.handle import HandleMixin
from .api.model import GatewayBot, User
from .bot import Bot
from .commands import sync_application_command
Expand All @@ -37,7 +40,13 @@
RECONNECT_INTERVAL = 3.0


class Adapter(BaseAdapter):
@lru_cache(maxsize=256)
def _get_handler_params(handler: Callable[..., Any]) -> Mapping[str, inspect.Parameter]:
"""Cache handler signature parameters to avoid repeated introspection."""
return inspect.signature(handler).parameters


class Adapter(BaseAdapter, HandleMixin):
@override
def __init__(self, driver: Driver, **kwargs: Any) -> None:
super().__init__(driver, **kwargs)
Expand Down Expand Up @@ -332,7 +341,14 @@ async def _authenticate(

try:
await ws.send(
json.dumps(model_dump(payload, by_alias=True, exclude_none=True))
json.dumps(
model_dump(
payload,
by_alias=True,
exclude_none=True,
omit_unset_values=True,
)
)
)
except Exception as e:
log(
Expand Down Expand Up @@ -459,6 +475,10 @@ def payload_to_event(cls, payload: Dispatch) -> Event:
@override
async def _call_api(self, bot: Bot, api: str, **data: Any) -> Any:
log("DEBUG", f"Calling API <y>{api}</y>")
if (api_handler := API_HANDLERS.get(api)) is None:
api_handler = getattr(self, f"_api_{api}", None)
if api_handler is None:
raise ApiNotAvailable
return await api_handler(self, bot, **data)
handler_params = _get_handler_params(api_handler)
if "bot" in handler_params:
return await api_handler(bot, **data)
return await api_handler(**data)
11 changes: 10 additions & 1 deletion nonebot/adapters/discord/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .client import ApiClient as ApiClient
from .handle import API_HANDLERS as API_HANDLERS
from .model import (
SKU,
ActionRow,
Expand All @@ -17,6 +16,7 @@
AnyCommandOption,
Application,
ApplicationCommand,
ApplicationCommandBulkOverwriteParams,
ApplicationCommandCreate,
ApplicationCommandData,
ApplicationCommandInteractionDataOption,
Expand Down Expand Up @@ -87,6 +87,7 @@
File,
FollowedChannel,
ForumTag,
ForumTagRequest,
Gateway,
GatewayBot,
Guild,
Expand Down Expand Up @@ -123,6 +124,7 @@
GuildTemplateGuildChannel,
GuildTemplateGuildRole,
GuildUpdate,
GuildVanityURL,
GuildWidget,
GuildWidgetChannel,
GuildWidgetSettings,
Expand Down Expand Up @@ -202,6 +204,7 @@
ResolvedData,
Resume,
Role,
RoleColors,
RoleOption,
RoleSubscriptionData,
RoleTags,
Expand All @@ -219,6 +222,7 @@
Sticker,
StickerItem,
StickerPack,
StickerPacksResponse,
StringOption,
SubCommandGroupOption,
SubCommandOption,
Expand Down Expand Up @@ -359,6 +363,7 @@
"AnyCommandOption",
"Application",
"ApplicationCommand",
"ApplicationCommandBulkOverwriteParams",
"ApplicationCommandCreate",
"ApplicationCommandData",
"ApplicationCommandInteractionDataOption",
Expand Down Expand Up @@ -449,6 +454,7 @@
"FollowedChannel",
"ForumLayoutTypes",
"ForumTag",
"ForumTagRequest",
"Gateway",
"GatewayBot",
"Guild",
Expand Down Expand Up @@ -494,6 +500,7 @@
"GuildTemplateGuildChannel",
"GuildTemplateGuildRole",
"GuildUpdate",
"GuildVanityURL",
"GuildWidget",
"GuildWidgetChannel",
"GuildWidgetSettings",
Expand Down Expand Up @@ -596,6 +603,7 @@
"ResolvedData",
"Resume",
"Role",
"RoleColors",
"RoleFlag",
"RoleOption",
"RoleSubscriptionData",
Expand All @@ -619,6 +627,7 @@
"StickerFormatType",
"StickerItem",
"StickerPack",
"StickerPacksResponse",
"StickerType",
"StringOption",
"SubCommandGroupOption",
Expand Down
Loading