Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions arclet/entari/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
from .event import BaseEvent as BaseEvent
from .event import attr as attr
from .event import register_internal_event as register_internal_event
from .event.api import SendRequest as SendRequest
from .event.api import SendResponse as SendResponse
from .event.base import MessageCreatedEvent as MessageCreatedEvent
from .event.base import MessageEvent as MessageEvent
from .event.base import Reply as Reply
Expand All @@ -63,8 +65,6 @@
from .event.lifespan import Cleanup as Cleanup
from .event.lifespan import Ready as Ready
from .event.lifespan import Startup as Startup
from .event.send import SendRequest as SendRequest
from .event.send import SendResponse as SendResponse
from .filter import filter_ as filter_
from .localdata import local_data as local_data
from .message import MessageChain as MessageChain
Expand Down
5 changes: 3 additions & 2 deletions arclet/entari/command/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
from typing import Any
from typing_extensions import TypeVar, deprecated

Expand Down Expand Up @@ -28,7 +29,7 @@


async def _after_execute(ctx: Contexts, session: Session | None = None):
result = ctx[RESULT]
result: str | MessageChain | _ExitException | None = ctx[RESULT]
event = ctx[EVENT]
if result is not None:
if isinstance(result, _ExitException):
Expand All @@ -55,7 +56,7 @@ def assign(self, path: str, value: Any = _seminal, or_not: bool = False, priorit
class AlconnaPluginDispatcher(PluginDispatcher[T]):
def __init__(self, plugin: Plugin, command: Alconna, need_reply_me: bool = False, need_notice_me: bool = False, use_config_prefix: bool = True, block: bool = True, skip_for_unmatch: bool = True): # noqa: E501
plugin._extra.setdefault("commands", []).append((command.prefixes, command.command))
self.cache = LRU(10)
self.cache: "LRU[str, asyncio.Future]" = LRU(10) # noqa: UP037
self.supplier = AlconnaSuppiler(command, self.cache, block, skip_for_unmatch)
super().__init__(plugin, MessageCreatedEvent, command.path)
plugin.collect(
Expand Down
2 changes: 1 addition & 1 deletion arclet/entari/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
ITEM_SESSION,
ITEM_USER,
)
from .event.api import SendResponse
from .event.base import MessageCreatedEvent, event_parse
from .event.config import ConfigReload
from .event.lifespan import AccountUpdate
from .event.send import SendResponse
from .localdata import local_data
from .logger import apply_log_save, enable_rich_except, log
from .message import MessageChain
Expand Down
107 changes: 107 additions & 0 deletions arclet/entari/event/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any

from arclet.letoderea import Contexts, Result, define, provide
from satori import ChannelType
from satori.client import Account
from satori.exception import ActionFailed
from satori.model import Channel, MessageObject

from ..const import ITEM_ACCOUNT, ITEM_CHANNEL, ITEM_MESSAGE_CONTENT, ITEM_SESSION
from ..message import MessageChain

if TYPE_CHECKING:
from ..session import Session


@dataclass
class SendRequest:
account: Account
channel: str
message: MessageChain
session: "Session | None" = None

def check_result(self, value) -> Result[bool | MessageChain] | None:
if isinstance(value, bool | MessageChain):
return Result(value)


before_send_pub = define(SendRequest, name="entari.event/before_send")


@before_send_pub.gather
async def send_req_gather(req: SendRequest, context: Contexts):
context[ITEM_ACCOUNT] = req.account
context[ITEM_MESSAGE_CONTENT] = req.message
if req.session:
context[ITEM_SESSION] = req.session
context[ITEM_CHANNEL] = req.session.channel
else:
try:
context[ITEM_CHANNEL] = await req.account.channel_get(req.channel)
except ActionFailed:
context[ITEM_CHANNEL] = Channel(
req.channel, ChannelType.DIRECT if req.channel.startswith("private:") else ChannelType.TEXT
)


@dataclass
class SendResponse:
account: Account
channel: str
message: MessageChain
result: list[MessageObject]
session: "Session | None" = None


send_pub = define(SendResponse, name="entari.event/after_send")
send_pub.providers.append(provide(list[MessageObject], call="$resp_result"))


@send_pub.gather
async def send_resp_gather(resp: SendResponse, context: Contexts):
context[ITEM_ACCOUNT] = resp.account
context[ITEM_MESSAGE_CONTENT] = resp.message
context["$resp_result"] = resp.result
if resp.session:
context[ITEM_SESSION] = resp.session
context[ITEM_CHANNEL] = resp.session.channel
else:
try:
context[ITEM_CHANNEL] = await resp.account.channel_get(resp.channel)
except ActionFailed:
context[ITEM_CHANNEL] = Channel(
resp.channel, ChannelType.DIRECT if resp.channel.startswith("private:") else ChannelType.TEXT
)


@dataclass
class APIRequest:
account: Account
name: str
params: dict[str, Any]


before_api_pub = define(APIRequest, name="entari.event/before_api_call")


@before_api_pub.gather
async def call_req_gather(req: APIRequest, context: Contexts):
context[ITEM_ACCOUNT] = req.account


@dataclass
class APIResponse:
account: Account
name: str
params: dict[str, Any]
success: bool
result: Any


after_api_pub = define(APIResponse, name="entari.event/after_api_call")


@after_api_pub.gather
async def call_resp_gather(resp: APIResponse, context: Contexts):
context[ITEM_ACCOUNT] = resp.account
4 changes: 2 additions & 2 deletions arclet/entari/event/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _is_notice_me(message: MessageChain, account: Account):


def _remove_notice_me(message: MessageChain, account: Account):
message = message.copy()
message = message.fork()
message.pop(0)
if _is_notice_me(message, account):
message.pop(0)
Expand Down Expand Up @@ -316,7 +316,7 @@ def __init__(self, account: Account, origin: OriginEvent):
super().__init__(account, origin)
self.content = MessageChain(self.message.message)
if self.content.has(Quote):
self.quote = self.content.get(Quote, 1)[0]
self.quote = self.content.get_first(Quote)
self.content = self.content.exclude(Quote)

async def gather(self, context: Contexts):
Expand Down
81 changes: 8 additions & 73 deletions arclet/entari/event/send.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,10 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING
from warnings import warn

from arclet.letoderea import Contexts, Result, define, provide
from satori import ChannelType
from satori.client import Account
from satori.exception import ActionFailed
from satori.model import Channel, MessageObject
warn(
"arclet.entari.event.send is deprecated, please use arclet.entari.event.api instead",
DeprecationWarning,
stacklevel=2,
)

from ..const import ITEM_ACCOUNT, ITEM_CHANNEL, ITEM_MESSAGE_CONTENT, ITEM_SESSION
from ..message import MessageChain

if TYPE_CHECKING:
from ..session import Session


@dataclass
class SendRequest:
account: Account
channel: str
message: MessageChain
session: "Session | None" = None

def check_result(self, value) -> Result[bool | MessageChain] | None:
if isinstance(value, bool | MessageChain):
return Result(value)


before_send_pub = define(SendRequest, name="entari.event/before_send")


@before_send_pub.gather
async def req_gather(req: SendRequest, context: Contexts):
context[ITEM_ACCOUNT] = req.account
context[ITEM_MESSAGE_CONTENT] = req.message
if req.session:
context[ITEM_SESSION] = req.session
context[ITEM_CHANNEL] = req.session.channel
else:
try:
context[ITEM_CHANNEL] = await req.account.channel_get(req.channel)
except ActionFailed:
context[ITEM_CHANNEL] = Channel(
req.channel, ChannelType.DIRECT if req.channel.startswith("private:") else ChannelType.TEXT
)


@dataclass
class SendResponse:
account: Account
channel: str
message: MessageChain
result: list[MessageObject]
session: "Session | None" = None


send_pub = define(SendResponse, name="entari.event/after_send")
send_pub.providers.append(provide(list[MessageObject], call="$resp_result"))


@send_pub.gather
async def resp_gather(resp: SendResponse, context: Contexts):
context[ITEM_ACCOUNT] = resp.account
context[ITEM_MESSAGE_CONTENT] = resp.message
context["$resp_result"] = resp.result
if resp.session:
context[ITEM_SESSION] = resp.session
context[ITEM_CHANNEL] = resp.session.channel
else:
try:
context[ITEM_CHANNEL] = await resp.account.channel_get(resp.channel)
except ActionFailed:
context[ITEM_CHANNEL] = Channel(
resp.channel, ChannelType.DIRECT if resp.channel.startswith("private:") else ChannelType.TEXT
)
from .api import SendRequest as SendRequest # noqa: F401
from .api import SendResponse as SendResponse # noqa: F401
Loading