Skip to content

Commit 70e69bc

Browse files
committed
🐛 fix multi command-event send when multi-assign define
1 parent 64d1f9a commit 70e69bc

3 files changed

Lines changed: 45 additions & 39 deletions

File tree

‎arclet/entari/command/__init__.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def __init__(
7979
converter=lambda x: MessageChain(x),
8080
)
8181
self.subscribers = WeakValueDictionary[str, Subscriber]()
82+
self._cache = {}
8283

8384
le.on(CommandExecute, self.execute)
8485

@@ -143,7 +144,7 @@ def wrapper(func: Callable[..., TM]) -> Subscriber[TM]:
143144
plg._extra.setdefault("commands", []).append(([], _command.command))
144145
else:
145146
target = self.scope.register(func, CommandDispatch, providers=providers)
146-
target.propagate(AlconnaSuppiler(_command))
147+
target.propagate(AlconnaSuppiler(_command, self._cache))
147148
target.propagate(_after_execute, priority=0)
148149
self.trie[key] = target.id
149150
self.subscribers[target.id] = target
@@ -174,7 +175,7 @@ def _remove(_):
174175
plg._extra.setdefault("commands", []).append((_command.prefixes, _command.command))
175176
else:
176177
target = self.scope.register(func, providers=providers)
177-
target.propagate(AlconnaSuppiler(_command))
178+
target.propagate(AlconnaSuppiler(_command, self._cache))
178179
target.propagate(_after_execute, priority=0)
179180
self.subscribers[target.id] = target
180181
for _key in keys:

‎arclet/entari/command/plugin.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
class AlconnaPluginDispatcher(PluginDispatcher):
2222
def __init__(self, plugin: Plugin, command: Alconna, need_reply_me: bool = False, need_notice_me: bool = False, use_config_prefix: bool = True, skip_for_unmatch: bool = True): # noqa: E501
2323
plugin._extra.setdefault("commands", []).append((command.prefixes, command.command))
24-
self.supplier = AlconnaSuppiler(command, skip_for_unmatch)
24+
cache = plugin._extra.setdefault("command_cache", {})
25+
self.supplier = AlconnaSuppiler(command, cache, skip_for_unmatch)
2526
super().__init__(plugin, MessageCreatedEvent, command.path)
2627
self.propagators.append(
2728
MessageJudges(need_reply_me, need_notice_me, use_config_prefix),

‎arclet/entari/command/provider.py‎

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import inspect
23
from typing import Any, Literal, Union, get_args
34

@@ -70,42 +71,42 @@ def compose(self):
7071
class AlconnaSuppiler(Propagator):
7172
cmd: Alconna
7273

73-
def __init__(self, cmd: Alconna, skip_for_unmatch: bool = True):
74+
def __init__(self, cmd: Alconna, cache: dict[int, asyncio.Future], skip_for_unmatch: bool = True):
7475
self.cmd = cmd
76+
self.cache = cache
7577
self.skip_for_unmatch = skip_for_unmatch
7678

77-
async def before_supply(self, message: MessageChain, session: Session | None = None, reply: Reply | None = None):
79+
async def supply(self, message: MessageChain, session: Session | None = None, reply: Reply | None = None):
80+
if self.cmd._hash in self.cache:
81+
future = self.cache[self.cmd._hash]
82+
if future.done():
83+
res = future.result()
84+
else:
85+
res = await future
86+
if res is None:
87+
return STOP
88+
return {"alc_result": res}
89+
90+
fut = asyncio.Future()
91+
self.cache[self.cmd._hash] = fut
7892
if session:
7993
recv = await post(ev := CommandReceive(session, self.cmd, message, reply))
8094
message = recv.value if recv else ev.content
81-
return {"_message": message}
82-
83-
async def supply(self, ctx: Contexts):
84-
try:
85-
_message: MessageChain = ctx.pop("_message")
86-
except KeyError:
87-
raise ProviderUnsatisfied("_message") from None
8895
with output_manager.capture(self.cmd.name) as cap:
8996
output_manager.set_action(lambda x: x, self.cmd.name)
9097
try:
91-
_res = self.cmd.parse(_message)
98+
_res = self.cmd.parse(message)
9299
except Exception as e:
93-
_res = Arparma(self.cmd._hash, _message, False, error_info=e)
100+
_res = Arparma(self.cmd._hash, message, False, error_info=e)
94101
may_help_text: str | None = cap.get("output", None)
95102
if not _res.head_matched:
103+
fut.set_result(None)
96104
return STOP
97105
if not may_help_text and not _res.matched and self.skip_for_unmatch:
106+
fut.set_result(None)
98107
return STOP
99108
if not may_help_text and _res.error_info:
100109
may_help_text = repr(_res.error_info)
101-
return {"_result": CommandResult(self.cmd, _res, may_help_text)}
102-
103-
async def after_supply(self, ctx: Contexts, session: Session | None = None):
104-
try:
105-
alc_result: CommandResult = ctx.pop("_result")
106-
except KeyError:
107-
raise ProviderUnsatisfied("_result") from None
108-
_res = alc_result.result
109110
if session:
110111
pres = await post(ev := CommandParse(session, self.cmd, _res))
111112
_res = ev.result
@@ -114,27 +115,30 @@ async def after_supply(self, ctx: Contexts, session: Session | None = None):
114115
_res = pres.value
115116
elif pres.value is False:
116117
return STOP
117-
if _res.matched:
118-
return {"alc_result": CommandResult(self.cmd, _res, alc_result.output)}
119-
if alc_result.output:
120-
if session:
121-
_t = str(_res.error_info) if isinstance(_res.error_info, SpecialOptionTriggered) else "error"
122-
ores = await post(ev := CommandOutput(session, self.cmd, _t, alc_result.output))
123-
msg = MessageChain(ev.content)
124-
if ores:
125-
if ores.value is False:
126-
return STOP
127-
elif isinstance(ores.value, str | MessageChain):
128-
msg = MessageChain(ores.value)
118+
if _res.matched or not may_help_text:
119+
res = CommandResult(self.cmd, _res, may_help_text)
120+
elif session:
121+
_t = str(_res.error_info) if isinstance(_res.error_info, SpecialOptionTriggered) else "error"
122+
ores = await post(ev := CommandOutput(session, self.cmd, _t, may_help_text))
123+
msg = MessageChain(ev.content)
124+
if ores:
125+
if ores.value is False:
126+
msg = None
127+
elif isinstance(ores.value, str | MessageChain):
128+
msg = MessageChain(ores.value)
129+
if msg:
129130
await session.send(msg)
130-
return STOP
131-
return {"alc_result": alc_result}
132-
return STOP
131+
may_help_text = None
132+
res = CommandResult(self.cmd, _res, may_help_text)
133+
else:
134+
res = CommandResult(self.cmd, _res, may_help_text)
135+
fut.set_result(res)
136+
if not _res.matched and not may_help_text:
137+
return STOP
138+
return {"alc_result": res}
133139

134140
def compose(self):
135-
yield self.before_supply, True, 65
136141
yield self.supply, True, 70
137-
yield self.after_supply, True, 75
138142

139143

140144
class AlconnaProvider(Provider[Any]):

0 commit comments

Comments
 (0)