Skip to content

Commit c9ef4b7

Browse files
committed
✨ SuperuserShortcutExt
1 parent b86e5cf commit c9ef4b7

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

example/bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# driver.register_adapter(SatoriAdapter)
1414

1515
# nonebot.require("nonebot_plugin_alconna")
16-
nonebot.load_plugins("plugins")
17-
# nonebot.load_plugin("plugins.demo1")
16+
# nonebot.load_plugins("plugins")
17+
nonebot.load_plugin("plugins.demo1")
1818

1919

2020
async def _():

example/plugins/demo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from nonebot_plugin_waiter import waiter
2727

28+
from nonebot_plugin_alconna.builtins.extensions.shortcut import SuperUserShortcutExtension
2829
from nonebot_plugin_alconna import (
2930
At,
3031
Check,
@@ -64,7 +65,7 @@ async def output_converter(self, output_type, content: str):
6465
return UniMessage(content)
6566

6667

67-
add_global_extension(DemoExtension())
68+
add_global_extension(DemoExtension(), SuperUserShortcutExtension())
6869

6970

7071
def get_dist_map() -> dict:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from __future__ import annotations
2+
3+
from arclet.alconna import Alconna
4+
from nonebot.permission import SuperUser
5+
from nonebot.internal.adapter import Bot, Event
6+
7+
from nonebot_plugin_alconna import Extension, UniMessage
8+
9+
10+
class SuperUserShortcutExtension(Extension):
11+
"""
12+
用于设置仅超级用户可使用内置选项 `--shortcut` 的扩展。
13+
14+
Example:
15+
>>> from nonebot_plugin_alconna.builtins.extensions.shortcut import SuperUserShortcutExtension
16+
>>>
17+
>>> matcher = on_alconna("...", extensions=[SuperUserShortcutExtension()])
18+
"""
19+
20+
@property
21+
def priority(self) -> int:
22+
return 20
23+
24+
@property
25+
def id(self) -> str:
26+
return "builtins.extensions.shortcut:SuperUserShortcutExtension"
27+
28+
async def receive_wrapper(self, bot: Bot, event: Event, command: Alconna, receive: UniMessage) -> UniMessage:
29+
su = SuperUser()
30+
if await su(bot, event):
31+
command.namespace_config.disable_builtin_options.discard("shortcut")
32+
else:
33+
command.namespace_config.disable_builtin_options.add("shortcut")
34+
return receive
35+
36+
37+
__extension__ = SuperUserShortcutExtension

0 commit comments

Comments
 (0)