Skip to content

Commit ae2f734

Browse files
committed
⏪ "♻️ worse @supported_action"
This reverts commit 87fea65.
1 parent 87fea65 commit ae2f734

File tree

6 files changed

+65
-103
lines changed

6 files changed

+65
-103
lines changed

nonebot_plugin_all4one/middlewares/__init__.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Generic,
1616
Literal,
1717
TypeVar,
18-
Callable,
1918
ClassVar,
2019
Optional,
2120
)
@@ -44,17 +43,19 @@
4443
"OneBot V11": "onebot.v11",
4544
}
4645

47-
supported_actions: Dict[str, Set[str]] = {}
4846

47+
class supported_action:
48+
def __init__(self, fn):
49+
self.fn = fn
4950

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
51+
def __set_name__(self, owner: Type["Middleware"], name: str):
52+
owner.supported_actions.add(name)
5653

57-
return _
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)
5859

5960

6061
_T = TypeVar("_T", bound=OneBotEvent)
@@ -87,7 +88,22 @@ async def get(self):
8788
return event
8889

8990

90-
class Middleware:
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+
91107
def __init__(self, bot: Bot):
92108
self.bot = bot
93109
self.tasks: List[asyncio.Task] = []
@@ -99,10 +115,7 @@ async def get_supported_actions(self, **kwargs: Any) -> List[str]:
99115
参数:
100116
kwargs: 扩展字段
101117
"""
102-
_ = set()
103-
_.update(supported_actions.get("", set()))
104-
_.update(supported_actions.get(self.get_name(), set()))
105-
return list(_)
118+
return list(self.supported_actions)
106119

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

167180
@abstractmethod
168181
def get_platform(self) -> str:
@@ -451,7 +464,7 @@ async def leave_channel(
451464
"""
452465
raise NotImplementedError
453466

454-
@supported_action("")
467+
@supported_action
455468
async def upload_file(
456469
self,
457470
*,
@@ -545,7 +558,7 @@ async def upload_file_fragmented(
545558
"""
546559
raise NotImplementedError
547560

548-
@supported_action("")
561+
@supported_action
549562
async def get_file(
550563
self,
551564
*,

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("Console")
58+
@supported_action
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("OneBot V11")
218+
@supported_action
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("OneBot V11")
244+
@supported_action
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("OneBot V11")
248+
@supported_action
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("OneBot V11")
259+
@supported_action
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("OneBot V11")
275+
@supported_action
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("OneBot V11")
295+
@supported_action
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("OneBot V11")
309+
@supported_action
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("OneBot V11")
327+
@supported_action
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("OneBot V11")
357+
@supported_action
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("OneBot V11")
363+
@supported_action
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("OneBot V12")
42+
@supported_action
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(await super().get_supported_actions())
98+
_.update(self.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 await super().get_supported_actions():
103+
if api in self.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("QQ Guild")
194+
@supported_action
195195
async def send_message(
196196
self,
197197
*,
@@ -268,7 +268,7 @@ async def send_message(
268268
"time": time,
269269
}
270270

271-
@supported_action("QQ Guild")
271+
@supported_action
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("QQ Guild")
282+
@supported_action
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("QQ Guild")
306+
@supported_action
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("QQ Guild")
332+
@supported_action
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("QQ Guild")
351+
@supported_action
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("QQ Guild")
362+
@supported_action
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("QQ Guild")
378+
@supported_action
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("QQ Guild")
384+
@supported_action
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("QQ Guild")
396+
@supported_action
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("QQ Guild")
449+
@supported_action
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)