mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-02-25 20:10:28 +08:00
Merge pull request #150 from mnixry/patch-1
修复CommandSession的部分方法在多线程条件下出错
This commit is contained in:
commit
7eb49624c3
@ -3,6 +3,7 @@ import re
|
|||||||
import shlex
|
import shlex
|
||||||
import warnings
|
import warnings
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import partial
|
||||||
from typing import (
|
from typing import (
|
||||||
Tuple, Union, Callable, Iterable, Any, Optional, List, Dict,
|
Tuple, Union, Callable, Iterable, Any, Optional, List, Dict,
|
||||||
Awaitable
|
Awaitable
|
||||||
@ -271,7 +272,7 @@ class CommandSession(BaseSession):
|
|||||||
__slots__ = ('cmd',
|
__slots__ = ('cmd',
|
||||||
'current_key', 'current_arg_filters', '_current_send_kwargs',
|
'current_key', 'current_arg_filters', '_current_send_kwargs',
|
||||||
'current_arg', '_current_arg_text', '_current_arg_images',
|
'current_arg', '_current_arg_text', '_current_arg_images',
|
||||||
'_state', '_last_interaction', '_running')
|
'_state', '_last_interaction', '_running', '_run_future')
|
||||||
|
|
||||||
def __init__(self, bot: NoneBot, ctx: Context_T, cmd: Command, *,
|
def __init__(self, bot: NoneBot, ctx: Context_T, cmd: Command, *,
|
||||||
current_arg: str = '', args: Optional[CommandArgs_T] = None):
|
current_arg: str = '', args: Optional[CommandArgs_T] = None):
|
||||||
@ -292,6 +293,8 @@ class CommandSession(BaseSession):
|
|||||||
self._current_arg_images = None
|
self._current_arg_images = None
|
||||||
self.refresh(ctx, current_arg=current_arg) # fill the above
|
self.refresh(ctx, current_arg=current_arg) # fill the above
|
||||||
|
|
||||||
|
self._run_future = partial(asyncio.run_coroutine_threadsafe, loop=bot.loop)
|
||||||
|
|
||||||
self._state: State_T = {}
|
self._state: State_T = {}
|
||||||
if args:
|
if args:
|
||||||
self._state.update(args)
|
self._state.update(args)
|
||||||
@ -418,13 +421,13 @@ class CommandSession(BaseSession):
|
|||||||
def pause(self, message: Optional[Message_T] = None, **kwargs) -> None:
|
def pause(self, message: Optional[Message_T] = None, **kwargs) -> None:
|
||||||
"""Pause the session for further interaction."""
|
"""Pause the session for further interaction."""
|
||||||
if message:
|
if message:
|
||||||
asyncio.ensure_future(self.send(message, **kwargs))
|
self._run_future(self.send(message, **kwargs))
|
||||||
raise _PauseException
|
raise _PauseException
|
||||||
|
|
||||||
def finish(self, message: Optional[Message_T] = None, **kwargs) -> None:
|
def finish(self, message: Optional[Message_T] = None, **kwargs) -> None:
|
||||||
"""Finish the session."""
|
"""Finish the session."""
|
||||||
if message:
|
if message:
|
||||||
asyncio.ensure_future(self.send(message, **kwargs))
|
self._run_future(self.send(message, **kwargs))
|
||||||
raise _FinishException
|
raise _FinishException
|
||||||
|
|
||||||
def switch(self, new_ctx_message: Message_T) -> None:
|
def switch(self, new_ctx_message: Message_T) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user