1- import sys
21import asyncio
3- from collections .abc import Awaitable
2+ import sys
3+ from collections .abc import Awaitable , Callable
4+ from typing import Any
45from typing_extensions import override
5- from typing import Any , Callable , Optional
66
7- from textual .color import Color
87from nonebot .drivers import Driver
9- from nonechat import Frontend , ConsoleSetting
8+ from nonechat import ConsoleSetting , Frontend
9+ from textual .color import Color
1010
1111from nonebot import get_plugin_config
1212from nonebot .adapters import Adapter as BaseAdapter
1313
14+ from .backend import AdapterConsoleBackend
1415from .bot import Bot
15- from .utils import log
16- from .event import Event
1716from .config import Config
17+ from .event import Event
1818from .exception import ApiNotAvailable
19- from .backend import AdapterConsoleBackend
19+ from .utils import log
2020
2121
2222class Adapter (BaseAdapter ):
@@ -26,7 +26,7 @@ class Adapter(BaseAdapter):
2626 def __init__ (self , driver : Driver , ** kwargs : Any ) -> None :
2727 super ().__init__ (driver , ** kwargs )
2828 self .console_config = get_plugin_config (Config )
29- self ._task : Optional [ asyncio .Task ] = None
29+ self ._task : asyncio .Task | None = None
3030
3131 self ._stdout = sys .stdout
3232 self .clients : list [Callable [[Bot , str , dict [str , Any ]], Awaitable [Any ]]] = []
@@ -74,28 +74,34 @@ def post_event(self, event: Event) -> None:
7474
7575 @override
7676 async def _call_api (self , bot : Bot , api : str , ** data : Any ):
77- if api == "send_msg" :
78- return await self ._frontend .send_message (** data , bot = bot .info )
79- if api == "bell" :
80- return await self ._frontend .toggle_bell ()
81- if api == "get_user" :
82- return await self ._frontend .backend .get_user (data ["user_id" ])
83- if api == "get_channel" :
84- return await self ._frontend .backend .get_channel (data ["channel_id" ])
85- if api == "get_users" :
86- return await self ._frontend .backend .list_users ()
87- if api == "list_channels" :
88- return await self ._frontend .backend .list_channels (data .get ("list_users" , False ))
89- if api == "create_dm" :
90- user = await self ._frontend .backend .get_user (data ["user_id" ])
91- return await self ._frontend .backend .create_dm (user )
92- if api == "get_msg" :
93- channel = await self ._frontend .backend .get_channel (data ["channel_id" ])
94- return await self ._frontend .backend .get_chat (data ["message_id" ], channel )
95- if api == "recall_msg" :
96- channel = await self ._frontend .backend .get_channel (data ["channel_id" ])
97- return await self ._frontend .recall_message (data ["message_id" ], channel )
98- if api == "edit_msg" :
99- channel = await self ._frontend .backend .get_channel (data ["channel_id" ])
100- return await self ._frontend .edit_message (data ["message_id" ], data ["content" ], channel )
101- raise ApiNotAvailable (f"API { api } is not available in Console adapter" )
77+ match api :
78+ case "send_msg" :
79+ return await self ._frontend .send_message (** data , bot = bot .info )
80+ case "bell" :
81+ return await self ._frontend .toggle_bell ()
82+ case "current_user" :
83+ return self ._frontend .backend .current_user
84+ case "current_channel" :
85+ return self ._frontend .backend .current_channel
86+ case "get_user" :
87+ return await self ._frontend .backend .get_user (data ["user_id" ])
88+ case "get_channel" :
89+ return await self ._frontend .backend .get_channel (data ["channel_id" ])
90+ case "list_users" :
91+ return await self ._frontend .backend .list_users ()
92+ case "list_channels" :
93+ return await self ._frontend .backend .list_channels (data .get ("list_users" , False ))
94+ case "create_dm" :
95+ user = await self ._frontend .backend .get_user (data ["user_id" ])
96+ return await self ._frontend .backend .create_dm (user )
97+ case "get_msg" :
98+ channel = await self ._frontend .backend .get_channel (data ["channel_id" ])
99+ return await self ._frontend .backend .get_chat (data ["message_id" ], channel )
100+ case "recall_msg" :
101+ channel = await self ._frontend .backend .get_channel (data ["channel_id" ])
102+ return await self ._frontend .recall_message (data ["message_id" ], channel )
103+ case "edit_msg" :
104+ channel = await self ._frontend .backend .get_channel (data ["channel_id" ])
105+ return await self ._frontend .edit_message (data ["message_id" ], data ["content" ], channel )
106+ case _:
107+ raise ApiNotAvailable (f"API { api } is not available in Console adapter" )
0 commit comments