Skip to content

Commit 87fea65

Browse files
committed
♻️ worse @supported_action
1 parent bdd2e20 commit 87fea65

File tree

6 files changed

+103
-65
lines changed

6 files changed

+103
-65
lines changed

nonebot_plugin_all4one/middlewares/__init__.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Generic,
1616
Literal,
1717
TypeVar,
18+
Callable,
1819
ClassVar,
1920
Optional,
2021
)
@@ -43,19 +44,17 @@
4344
"OneBot V11": "onebot.v11",
4445
}
4546

47+
supported_actions: Dict[str, Set[str]] = {}
4648

47-
class supported_action:
48-
def __init__(self, fn):
49-
self.fn = fn
5049

51-
def __set_name__(self, owner: Type["Middleware"], name: str):
52-
owner.supported_actions.add(name)
50+
def supported_action(middleware: str):
51+
def _(action):
52+
if middleware not in supported_actions:
53+
supported_actions[middleware] = set()
54+
supported_actions[middleware].add(action.__name__)
55+
return action
5356

54-
def __call__(self, *args, **kwargs):
55-
return self.fn(*args, **kwargs)
56-
57-
def __get__(self, obj, objtype=None):
58-
return partial(self.__call__, obj)
57+
return _
5958

6059

6160
_T = TypeVar("_T", bound=OneBotEvent)
@@ -88,22 +87,7 @@ async def get(self):
8887
return event
8988

9089

91-
class _MiddlewareMeta(type):
92-
def __new__(cls, name, bases, attrs):
93-
supported_actions = set()
94-
for base in bases:
95-
supported_actions.update(base.supported_actions)
96-
attrs["supported_actions"] = supported_actions
97-
return type.__new__(cls, name, bases, attrs)
98-
99-
100-
class MiddlewareMeta(_MiddlewareMeta, ABCMeta):
101-
pass
102-
103-
104-
class Middleware(metaclass=MiddlewareMeta):
105-
supported_actions: ClassVar[Set[str]]
106-
90+
class Middleware:
10791
def __init__(self, bot: Bot):
10892
self.bot = bot
10993
self.tasks: List[asyncio.Task] = []
@@ -115,7 +99,10 @@ async def get_supported_actions(self, **kwargs: Any) -> List[str]:
11599
参数:
116100
kwargs: 扩展字段
117101
"""
118-
return list(self.supported_actions)
102+
_ = set()
103+
_.update(supported_actions.get("", set()))
104+
_.update(supported_actions.get(self.get_name(), set()))
105+
return list(_)
119106

120107
async def _call_api(self, api: str, **kwargs: Any) -> Any:
121108
if api not in await self.get_supported_actions():
@@ -175,7 +162,7 @@ def prefix_self_id(self, event: Event) -> Event:
175162
@abstractmethod
176163
def get_name(cls) -> str:
177164
"""对应协议适配器的名称"""
178-
raise NotImplementedError
165+
return ""
179166

180167
@abstractmethod
181168
def get_platform(self) -> str:
@@ -464,7 +451,7 @@ async def leave_channel(
464451
"""
465452
raise NotImplementedError
466453

467-
@supported_action
454+
@supported_action("")
468455
async def upload_file(
469456
self,
470457
*,
@@ -558,7 +545,7 @@ async def upload_file_fragmented(
558545
"""
559546
raise NotImplementedError
560547

561-
@supported_action
548+
@supported_action("")
562549
async def get_file(
563550
self,
564551
*,

nonebot_plugin_all4one/middlewares/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def from_onebot_message(self, message: OneBotMessage) -> Message:
5555
def to_onebot_message(self, message: Message) -> OneBotMessage:
5656
return OneBotMessage(OneBotMessageSegment.text(str(message)))
5757

58-
@supported_action
58+
@supported_action("Console")
5959
async def send_message(
6060
self,
6161
*,

nonebot_plugin_all4one/middlewares/onebot/v11.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ async def from_onebot_message(self, message: OneBotMessage) -> Message:
215215
message_list.append(MessageSegment.reply(segment.data["message_id"]))
216216
return Message(message_list)
217217

218-
@supported_action
218+
@supported_action("OneBot V11")
219219
async def send_message(
220220
self,
221221
*,
@@ -241,11 +241,11 @@ async def send_message(
241241
"time": int(datetime.now().timestamp()),
242242
}
243243

244-
@supported_action
244+
@supported_action("OneBot V11")
245245
async def delete_message(self, *, message_id: str, **kwargs: Any) -> None:
246246
await self.bot.delete_msg(message_id=int(message_id))
247247

248-
@supported_action
248+
@supported_action("OneBot V11")
249249
async def get_self_info(
250250
self, **kwargs: Any
251251
) -> Dict[Union[Literal["user_id", "nickname"], str], str]:
@@ -256,7 +256,7 @@ async def get_self_info(
256256
"user_displayname": "",
257257
}
258258

259-
@supported_action
259+
@supported_action("OneBot V11")
260260
async def get_user_info(
261261
self, *, user_id: str, no_cache: bool = False, **kwargs: Any
262262
) -> Dict[Union[Literal["user_id", "nickname"], str], str]:
@@ -272,7 +272,7 @@ async def get_user_info(
272272
resp.update({f"qq.{k}": v for k, v in result.items() if k not in resp})
273273
return resp
274274

275-
@supported_action
275+
@supported_action("OneBot V11")
276276
async def get_friend_list(
277277
self,
278278
**kwargs: Any,
@@ -292,7 +292,7 @@ async def get_friend_list(
292292
resp.append(friend_dict)
293293
return resp
294294

295-
@supported_action
295+
@supported_action("OneBot V11")
296296
async def get_group_info(
297297
self, *, group_id: str, no_cache: bool = False, **kwargs: Any
298298
) -> Dict[Union[Literal["group_id", "group_name"], str], str]:
@@ -306,7 +306,7 @@ async def get_group_info(
306306
resp.update({f"qq.{k}": v for k, v in result.items() if k not in resp})
307307
return resp
308308

309-
@supported_action
309+
@supported_action("OneBot V11")
310310
async def get_group_list(
311311
self,
312312
**kwargs: Any,
@@ -324,7 +324,7 @@ async def get_group_list(
324324
resp.append(group_dict)
325325
return resp
326326

327-
@supported_action
327+
@supported_action("OneBot V11")
328328
async def get_group_member_info(
329329
self, *, group_id: str, user_id: str, no_cache: bool = False, **kwargs: Any
330330
) -> Dict[Union[Literal["user_id", "nickname"], str], str]:
@@ -354,13 +354,13 @@ async def get_group_member_list(
354354
resp.append(tmp)
355355
return resp
356356

357-
@supported_action
357+
@supported_action("OneBot V11")
358358
async def set_group_name(
359359
self, *, group_id: str, group_name: str, **kwargs: Any
360360
) -> None:
361361
await self.bot.set_group_name(group_id=int(group_id), group_name=group_name)
362362

363-
@supported_action
363+
@supported_action("OneBot V11")
364364
async def leave_group(
365365
self, *, group_id: str, is_dismiss: bool = False, **kwargs: Any
366366
) -> None:

nonebot_plugin_all4one/middlewares/onebot/v12.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def to_onebot_event(self, event: Event) -> List[Event]:
3939
event.message = Message(message_list)
4040
return [event]
4141

42-
@supported_action
42+
@supported_action("OneBot V12")
4343
async def send_message(
4444
self,
4545
*,
@@ -95,11 +95,11 @@ async def send_message(
9595

9696
async def get_supported_actions(self, **kwargs: Any) -> List[str]:
9797
_ = set()
98-
_.update(self.supported_actions)
98+
_.update(await super().get_supported_actions())
9999
_.update(await self.bot.get_supported_actions(**kwargs))
100100
return list(_)
101101

102102
async def _call_api(self, api: str, **kwargs: Any) -> Any:
103-
if api in self.supported_actions:
103+
if api in await super().get_supported_actions():
104104
return await getattr(self, api)(**kwargs)
105105
return await self.bot.call_api(api, **kwargs)

nonebot_plugin_all4one/middlewares/qqguild.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ async def to_onebot_message(self, event: MessageEvent) -> OneBotMessage:
191191
message_list.append(OneBotMessageSegment.mention_all())
192192
return OneBotMessage(message_list)
193193

194-
@supported_action
194+
@supported_action("QQ Guild")
195195
async def send_message(
196196
self,
197197
*,
@@ -268,7 +268,7 @@ async def send_message(
268268
"time": time,
269269
}
270270

271-
@supported_action
271+
@supported_action("QQ Guild")
272272
async def get_guild_info(
273273
self, *, guild_id: str, **kwargs: Any
274274
) -> Dict[Union[Literal["guild_id", "guild_name"], str], str]:
@@ -279,7 +279,7 @@ async def get_guild_info(
279279
guild_dict["guild_name"] = guild_dict["name"]
280280
return guild_dict
281281

282-
@supported_action
282+
@supported_action("QQ Guild")
283283
async def get_guild_list(
284284
self, **kwargs: Any
285285
) -> List[Dict[Union[Literal["guild_id", "guild_name"], str], str]]:
@@ -303,7 +303,7 @@ async def get_guild_list(
303303
guilds_list.append(guild_dict)
304304
return guilds_list
305305

306-
@supported_action
306+
@supported_action("QQ Guild")
307307
async def get_guild_member_info(
308308
self, *, guild_id: str, user_id: str, **kwargs: Any
309309
) -> Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
@@ -329,7 +329,7 @@ async def _get_all_members(self, guild_id: str) -> List[Member]:
329329
after = str(result[-1].user.id) # type: ignore
330330
return members
331331

332-
@supported_action
332+
@supported_action("QQ Guild")
333333
async def get_guild_member_list(
334334
self, *, guild_id: str, **kwargs: Any
335335
) -> List[
@@ -348,7 +348,7 @@ async def get_guild_member_list(
348348
)
349349
return members_list
350350

351-
@supported_action
351+
@supported_action("QQ Guild")
352352
async def get_channel_info(
353353
self, *, guild_id: str, channel_id: str, **kwargs: Any
354354
) -> Dict[Union[Literal["channel_id", "channel_name"], str], str]:
@@ -359,7 +359,7 @@ async def get_channel_info(
359359
"channel_name": result.name, # type: ignore
360360
}
361361

362-
@supported_action
362+
@supported_action("QQ Guild")
363363
async def get_channel_list(
364364
self, *, guild_id: str, **kwargs: Any
365365
) -> List[Dict[Union[Literal["channel_id", "channel_name"], str], str]]:
@@ -375,13 +375,13 @@ async def get_channel_list(
375375
)
376376
return channels_list
377377

378-
@supported_action
378+
@supported_action("QQ Guild")
379379
async def set_channel_name(
380380
self, *, guild_id: str, channel_id: str, channel_name: str, **kwargs: Any
381381
) -> None:
382382
await self.bot.patch_channel(channel_id=int(channel_id), name=channel_name)
383383

384-
@supported_action
384+
@supported_action("QQ Guild")
385385
async def get_channel_member_info(
386386
self, *, guild_id: str, channel_id: str, user_id: str, **kwargs: Any
387387
) -> Dict[Union[Literal["user_id", "user_name", "user_displayname"], str], str]:
@@ -393,7 +393,7 @@ async def get_channel_member_info(
393393
"user_displayname": result.nick, # type: ignore
394394
}
395395

396-
@supported_action
396+
@supported_action("QQ Guild")
397397
async def get_channel_member_list(
398398
self, *, guild_id: str, channel_id: str, **kwargs: Any
399399
) -> List[
@@ -446,7 +446,7 @@ def can_view(member: Member) -> bool:
446446
)
447447
return members_list
448448

449-
@supported_action
449+
@supported_action("QQ Guild")
450450
async def delete_message(self, *, message_id: str, **kwargs: Any) -> None:
451451
message, _, channel = self._from_ob_message_id(message_id)
452452
if channel is None or message is None:

0 commit comments

Comments
 (0)