Skip to content

Commit 38b0f49

Browse files
✨ 适配新版dc适配器 (#134)
* ⬆️ upgrade discord adapter dependency * 🐛 adapt discord slash extension to adapter 1.x * 🎨 format * 🚨 auto fix by pre-commit hooks * 🚨 添加 ignore * ⬆️ 更新 .pre-commit * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 65e6684 commit 38b0f49

7 files changed

Lines changed: 598 additions & 523 deletions

File tree

.pre-commit-config.yaml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
default_install_hook_types: [pre-commit, prepare-commit-msg]
22
ci:
3-
autofix_commit_msg: ":rotating_light: auto fix by pre-commit hooks"
4-
autofix_prs: true
5-
autoupdate_branch: master
6-
autoupdate_schedule: monthly
7-
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
3+
autofix_commit_msg: ':rotating_light: auto fix by pre-commit hooks'
4+
autofix_prs: true
5+
autoupdate_branch: master
6+
autoupdate_schedule: monthly
7+
autoupdate_commit_msg: ':arrow_up: auto update by pre-commit hooks'
88
repos:
9-
- repo: https://github.com/pycqa/isort
10-
rev: 5.13.2
11-
hooks:
12-
- id: isort
13-
stages: [pre-commit]
9+
- repo: https://github.com/pycqa/isort
10+
rev: 5.13.2
11+
hooks:
12+
- id: isort
13+
stages: [pre-commit]
1414

15-
- repo: https://github.com/psf/black
16-
rev: 25.1.0
17-
hooks:
18-
- id: black
19-
stages: [pre-commit]
15+
- repo: https://github.com/psf/black
16+
rev: 26.1.0
17+
hooks:
18+
- id: black
19+
stages: [pre-commit]
2020

21-
- repo: https://github.com/astral-sh/ruff-pre-commit
22-
rev: v0.12.7
23-
hooks:
24-
- id: ruff
25-
args: [--fix, --exit-non-zero-on-fix]
26-
stages: [pre-commit]
21+
- repo: https://github.com/astral-sh/ruff-pre-commit
22+
rev: v0.15.4
23+
hooks:
24+
- id: ruff
25+
args: [--fix, --exit-non-zero-on-fix]
26+
stages: [pre-commit]

pdm.lock

Lines changed: 482 additions & 412 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[project]
22
name = "nonebot-plugin-alconna"
33
description = "Alconna Adapter for Nonebot"
4-
authors = [
5-
{name = "RF-Tar-Railt", email = "rf_tar_railt@qq.com"},
6-
]
4+
authors = [{ name = "RF-Tar-Railt", email = "rf_tar_railt@qq.com" }]
75
dependencies = [
86
"tarina<0.8.0,>=0.7.3",
97
"nepattern<1.0,>=0.7.7",
@@ -16,12 +14,8 @@ dependencies = [
1614
dynamic = ["version"]
1715
requires-python = ">=3.10"
1816
readme = "README.md"
19-
license = {text = "MIT"}
20-
keywords = [
21-
"command",
22-
"alconna",
23-
"nonebot",
24-
]
17+
license = { text = "MIT" }
18+
keywords = ["command", "alconna", "nonebot"]
2519
[project.urls]
2620
homepage = "https://github.com/nonebot/plugin-alconna"
2721
repository = "https://github.com/nonebot/plugin-alconna"
@@ -58,95 +52,99 @@ target-version = "py39"
5852

5953
[tool.ruff.lint]
6054
select = [
61-
"F", # pyflakes
62-
"E", # pycodestyle errors
63-
"W", # pycodestyle warnings
55+
"F", # pyflakes
56+
"E", # pycodestyle errors
57+
"W", # pycodestyle warnings
6458
# "I", # isort
6559
"N", # PEP8-naming
6660
"UP", # pyupgrade
6761
"YTT", # flake8-2020
6862
"ASYNC", # flake8-async
6963
# "S", # flake8-bandit
70-
"BLE", # flake8-blind-except
64+
"BLE", # flake8-blind-except
7165
# "FBT", # flake8-boolean-trap
72-
"B", # flake8-bugbear
73-
"A", # flake8-builtins
74-
"COM", # flake8-commas
75-
"C4", # flake8-comprehensions
76-
"DTZ", # flake8-datetimez
77-
"T10", # flake8-debugger
66+
"B", # flake8-bugbear
67+
"A", # flake8-builtins
68+
"COM", # flake8-commas
69+
"C4", # flake8-comprehensions
70+
"DTZ", # flake8-datetimez
71+
"T10", # flake8-debugger
7872
# "FA", # flake8-future-annotations
79-
"ISC", # flake8-implicit-str-concat
80-
"ICN", # flake8-import-conventions
81-
"PIE", # flake8-pie
82-
"T20", # flake8-print
83-
"PYI", # flake8-pyi
84-
"Q", # flake8-quotes
85-
"RSE", # flake8-raise
86-
"RET", # flake8-return
87-
"SLOT", # flake8-slots
88-
"SIM", # flake8-simplify
89-
"TID", # flake8-tidy-imports
73+
"ISC", # flake8-implicit-str-concat
74+
"ICN", # flake8-import-conventions
75+
"PIE", # flake8-pie
76+
"T20", # flake8-print
77+
"PYI", # flake8-pyi
78+
"Q", # flake8-quotes
79+
"RSE", # flake8-raise
80+
"RET", # flake8-return
81+
"SLOT", # flake8-slots
82+
"SIM", # flake8-simplify
83+
"TID", # flake8-tidy-imports
9084
# "TC", # flake8-type-checking
9185
# "ARG", # flake8-unused-arguments
92-
"PTH", # flake8-use-pathlib
86+
"PTH", # flake8-use-pathlib
9387
# "ERA", # eradicate
94-
"PD", # pandas-vet
95-
"PGH", # pygrep-hooks
96-
"PL", # pylint
97-
"TRY", # tryceratops
98-
"FLY", # flynt
99-
"FAST", # FastAPI
100-
"PERF", # Perflint
101-
"FURB", # refurb
102-
"RUF", # Ruff-specific rules
88+
"PD", # pandas-vet
89+
"PGH", # pygrep-hooks
90+
"PL", # pylint
91+
"TRY", # tryceratops
92+
"FLY", # flynt
93+
"FAST", # FastAPI
94+
"PERF", # Perflint
95+
"FURB", # refurb
96+
"RUF", # Ruff-specific rules
10397
]
10498
ignore = [
10599
"C901",
106100
"T201",
107101
"E731",
108102
"PT023",
109-
"B010", # Do not call `setattr` with a constant attribute value.
103+
"B010", # Do not call `setattr` with a constant attribute value.
110104
"SIM105", # Use `contextlib.suppress`
111-
"B008", # Do not perform function call in argument defaults
112-
"RUF100", # Unused `noqa` directive
113-
"TC003", # Move xxx into a type-checking block
114-
"E402", # module level import not at top of file
115-
"E501", # 过长的行由 ruff format 处理, 剩余的都是字符串
116-
"UP035", # pyupgrade, 但 typing.Callable 的导入会报错
105+
"B008", # Do not perform function call in argument defaults
106+
"RUF100", # Unused `noqa` directive
107+
"TC003", # Move xxx into a type-checking block
108+
"E402", # module level import not at top of file
109+
"E501", # 过长的行由 ruff format 处理, 剩余的都是字符串
110+
"UP035", # pyupgrade, 但 typing.Callable 的导入会报错
117111
"TRY003",
118112
"TRY301", # 为啥非要把 raise 丢进另外一个 inner fn 里
119113
"BLE001", # except Exception as e
120114
"PGH003", # 要求 `# type: ignore` 提供理由,但 pyright 和 mypy 等都不是统一标准。
121-
"PLC0414", # 用 import-as 表示 re-export
122-
"N818", # 要求所有自定义错误以 Error 作后缀,但我们不这么做
123-
"RET502", # return = return None
115+
"PLC0414", # 用 import-as 表示 re-export
116+
"N818", # 要求所有自定义错误以 Error 作后缀,但我们不这么做
117+
"RET502", # return = return None
124118
"RET503", # 就要 implicit return none
125-
"PLC0415", # 你别管我为什么不在顶层 import
126-
"PLC0105", # 我们已经用 R 表示协变,Q 表示逆变了
127-
"PLR0913", # 参数就那么多的,你用 builder 什么的不是更逆天?
119+
"PLC0415", # 你别管我为什么不在顶层 import
120+
"PLC0105", # 我们已经用 R 表示协变,Q 表示逆变了
121+
"PLR0913", # 参数就那么多的,你用 builder 什么的不是更逆天?
128122
"SIM108", # 迫真 simplicy
129123
"RUF001", # String contains ambiguous `,` (FULLWIDTH COMMA). Did you mean `,` (COMMA)?
130124
"RUF002", # Docstring contains ambiguous `,` (FULLWIDTH COMMA). Did you mean `,` (COMMA)?
131125
"RUF009", # 我不想要额外的全局变量。
132126
# "UP038", # instance(..., X | Y) 还是太超前了
133-
"RUF003", # 中文注释里用全角符号怎么你了
134-
"SLOT000", # 动态类型需求,主要是 json5 backend
135-
"PLR0911", "PLR0912", "PLR0915", # 复杂度高点怎么你了
136-
"PYI019", # 用 `Self` 替代 `self: T` 的 T
137-
"PYI041", # int 和 float 在运行时的类型没有交集(互不成立 issubclass)
127+
"RUF003", # 中文注释里用全角符号怎么你了
128+
"SLOT000", # 动态类型需求,主要是 json5 backend
129+
"PLR0911",
130+
"PLR0912",
131+
"PLR0915", # 复杂度高点怎么你了
132+
"PYI019", # 用 `Self` 替代 `self: T` 的 T
133+
"PYI041", # int 和 float 在运行时的类型没有交集(互不成立 issubclass)
138134
"PLW2901", # shallow 怎么你了
139-
"S101", # assert 怎么你了,非要 RuntimeError 吗
135+
"S101", # assert 怎么你了,非要 RuntimeError 吗
140136
"PLR2004", # magic number 怎么你了
141-
"TRY004", # 我要抛啥错误我清楚
142-
"COM812", # 强制尾随逗号
143-
"TID252", # 相对导入
144-
"ISC001", # format warning
145-
"N801", # Class name should use CapWords convention
146-
"N802", # Function name should be lowercase
147-
"N804", # First argument of a class method should be named `cls`
148-
"N805", # First argument of a method should be named `self`
149-
"N806", # Variable in function should be lowercase
137+
"TRY004", # 我要抛啥错误我清楚
138+
"COM812", # 强制尾随逗号
139+
"TID252", # 相对导入
140+
"ISC001", # format warning
141+
"N801", # Class name should use CapWords convention
142+
"N802", # Function name should be lowercase
143+
"N804", # First argument of a class method should be named `cls`
144+
"N805", # First argument of a method should be named `self`
145+
"N806", # Variable in function should be lowercase
146+
"PLW0108",
147+
"ASYNC240",
150148
]
151149
flake8-quotes = { inline-quotes = "double", multiline-quotes = "double" }
152150

@@ -167,7 +165,11 @@ reportSelfClsParameterName = false
167165

168166
[tool.pdm.scripts]
169167
test = "pytest -v -W ignore ./tests/"
170-
format = { composite = ["isort ./src/ ./example/ ./tests/","black ./src/ ./example/ ./tests/","ruff check ./src/ ./example/ ./tests/"] }
168+
format = { composite = [
169+
"isort ./src/ ./example/ ./tests/",
170+
"black ./src/ ./example/ ./tests/",
171+
"ruff check ./src/ ./example/ ./tests/",
172+
] }
171173

172174
[tool.pytest.ini_options]
173175
asyncio_mode = "auto"
@@ -189,7 +191,7 @@ dev = [
189191
"nonebot2[httpx,websockets]>=2.4.3",
190192
"nonebot-adapter-console<0.10.0,>=0.9.0",
191193
"nonebot-adapter-ding>=2.0.0a16",
192-
"nonebot-adapter-discord>=0.1.8",
194+
"nonebot-adapter-discord>=1.1.3",
193195
"nonebot-adapter-dodo>=0.2.1",
194196
"nonebot-adapter-feishu>=2.6.2",
195197
"nonebot-adapter-kaiheila>=0.3.4",
@@ -215,6 +217,7 @@ dev = [
215217
"nonebot-adapter-efchat>=0.1.9.post2",
216218
"pytest-asyncio==0.26.0",
217219
"nonebot-adapter-bilibili-live>=0.2.4",
220+
"nonebot-adapter-yunhu>=0.1.6",
218221
]
219222
test = [
220223
"nonebug>=0.4.3",

src/nonebot_plugin_alconna/builtins/extensions/discord.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
SubCommandGroupOption,
2424
SubCommandOption,
2525
)
26-
from nonebot.adapters.discord.api.types import InteractionCallbackType, MessageFlag
26+
from nonebot.adapters.discord.api.types import UNSET, InteractionCallbackType, MessageFlag
2727
from nonebot.adapters.discord.commands.matcher import ApplicationCommandConfig, SlashCommandMatcher, on_slash_command
2828
from nonebot.adapters.discord.commands.storage import _application_command_storage
2929
from nonebot.adapters.discord.event import ApplicationCommandInteractionEvent
@@ -35,7 +35,8 @@
3535
from nonebot.typing import T_Handler, T_PermissionChecker, T_RuleChecker, T_State
3636
from tarina import LRU, lang
3737

38-
from nonebot_plugin_alconna import At, Extension, Image, UniMessage, log
38+
from nonebot_plugin_alconna import At, Extension, Image, UniMessage
39+
from nonebot_plugin_alconna.consts import log
3940
from nonebot_plugin_alconna.extension import cache_msg
4041
from nonebot_plugin_alconna.matcher import _M, AlconnaMatcher
4142

@@ -59,9 +60,7 @@ def _translate_args(args: Args) -> list[AnyCommandOption]:
5960
name=f"{arg.name}",
6061
description=f"{arg.notice or arg.name}",
6162
required=not arg.optional,
62-
choices=[
63-
OptionChoice(name=str(x), value=x) for x in arg.value.base # type: ignore # noqa: E501
64-
],
63+
choices=[OptionChoice(name=str(x), value=x) for x in arg.value.base if isinstance(x, int)],
6564
)
6665
)
6766
elif all(isinstance(x, float) for x in arg.value.base):
@@ -70,9 +69,7 @@ def _translate_args(args: Args) -> list[AnyCommandOption]:
7069
name=f"{arg.name}",
7170
description=f"{arg.notice or arg.name}",
7271
required=not arg.optional,
73-
choices=[
74-
OptionChoice(name=str(x), value=x) for x in arg.value.base # type: ignore # noqa: E501
75-
],
72+
choices=[OptionChoice(name=str(x), value=x) for x in arg.value.base if isinstance(x, float)],
7673
)
7774
)
7875
else:
@@ -176,7 +173,7 @@ def _translate_args(args: Args) -> list[AnyCommandOption]:
176173
return result
177174

178175

179-
def _translate_options(opt: Union[Option, Subcommand]) -> Union[SubCommandGroupOption, SubCommandOption]:
176+
def _translate_options(opt: Union[Option, Subcommand]) -> AnyCommandOption:
180177
if isinstance(opt, Option):
181178
return SubCommandOption(
182179
name=f"{opt.name}",
@@ -190,7 +187,9 @@ def _translate_options(opt: Union[Option, Subcommand]) -> Union[SubCommandGroupO
190187
)
191188
if not opt.args.empty:
192189
return SubCommandOption(
193-
name=f"{opt.name}", description=f"{opt.help_text}", options=_translate_args(opt.args) # type: ignore
190+
name=f"{opt.name}",
191+
description=f"{opt.help_text}",
192+
options=_translate_args(opt.args), # type: ignore
194193
)
195194
return SubCommandGroupOption(
196195
name=f"{opt.name}",
@@ -239,6 +238,10 @@ def translate(
239238
"options": options,
240239
**locals(),
241240
}
241+
if default_permission is None:
242+
buffer.pop("default_permission")
243+
if nsfw is None:
244+
buffer.pop("nsfw")
242245
buffer["_depth"] += 1
243246
buffer.pop("alc")
244247
return on_slash_command(**buffer)
@@ -316,11 +319,11 @@ def post_init(self, alc: Alconna) -> None:
316319
name_localizations=self.name_localizations,
317320
description=self.description or alc.meta.description,
318321
description_localizations=self.description_localizations,
319-
options=options, # type: ignore
322+
options=options,
320323
default_member_permissions=self.default_member_permissions,
321324
dm_permission=self.dm_permission,
322-
default_permission=self.default_permission,
323-
nsfw=self.nsfw,
325+
default_permission=self.default_permission if self.default_permission is not None else UNSET,
326+
nsfw=self.nsfw if self.nsfw is not None else UNSET,
324327
)
325328
_application_command_storage[self.internal_id or config.name] = config
326329
self.application_command = config
@@ -355,12 +358,12 @@ def _handle_options(options: list[ApplicationCommandInteractionDataOption]):
355358
ApplicationCommandOptionType.SUB_COMMAND_GROUP,
356359
):
357360
yield f"{opt.name}"
358-
if opt.options:
361+
if isinstance(opt.options, list):
359362
yield from _handle_options(opt.options)
360363
else:
361364
yield f"{opt.value}"
362365

363-
if data.options:
366+
if isinstance(data.options, list):
364367
cmd += " "
365368
cmd += " ".join(_handle_options(data.options))
366369

src/nonebot_plugin_alconna/uniseg/target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(
7777
self.private = private
7878
self.source = source
7979
self.self_id = self_id
80-
self.extra = extra if extra else {}
80+
self.extra = extra or {}
8181
self.selector = None
8282
if scope:
8383
self.selector = partial(SCOPES[scope], self)

tests/fake.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def fake_discord_interaction_event(**field) -> "ApplicationCommandInteractionEve
117117
field["application_id"] = 123456789
118118
field["token"] = "sometoken" # noqa: S105
119119
field["version"] = 1
120+
field.setdefault("attachment_size_limit", 0)
121+
field.setdefault("authorizing_integration_owners", {})
120122

121123
class FakeEvent(_fake):
122124
pass

tests/test_uniseg.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,11 @@ def test_persistence():
102102
{"type": "image", "url": "https://example.com/1.jpg", "sticker": False},
103103
{"type": "text", "text": "hello"},
104104
]
105-
assert (
106-
msg.dump(json=True)
107-
== """\
105+
assert msg.dump(json=True) == """\
108106
[{"type": "at", "flag": "user", "target": "123"}, \
109107
{"type": "at", "flag": "channel", "target": "456"}, \
110108
{"type": "image", "url": "https://example.com/1.jpg", "sticker": false}, \
111109
{"type": "text", "text": "hello"}]"""
112-
)
113110

114111
msg1 = [
115112
{"type": "at", "flag": "user", "target": "456"},

0 commit comments

Comments
 (0)