Merge PR #962: fix(qq): make start() long-running per base channel contract
This commit is contained in:
@@ -55,7 +55,6 @@ class QQChannel(BaseChannel):
|
|||||||
self.config: QQConfig = config
|
self.config: QQConfig = config
|
||||||
self._client: "botpy.Client | None" = None
|
self._client: "botpy.Client | None" = None
|
||||||
self._processed_ids: deque = deque(maxlen=1000)
|
self._processed_ids: deque = deque(maxlen=1000)
|
||||||
self._bot_task: asyncio.Task | None = None
|
|
||||||
|
|
||||||
async def start(self) -> None:
|
async def start(self) -> None:
|
||||||
"""Start the QQ bot."""
|
"""Start the QQ bot."""
|
||||||
@@ -71,8 +70,8 @@ class QQChannel(BaseChannel):
|
|||||||
BotClass = _make_bot_class(self)
|
BotClass = _make_bot_class(self)
|
||||||
self._client = BotClass()
|
self._client = BotClass()
|
||||||
|
|
||||||
self._bot_task = asyncio.create_task(self._run_bot())
|
|
||||||
logger.info("QQ bot started (C2C private message)")
|
logger.info("QQ bot started (C2C private message)")
|
||||||
|
await self._run_bot()
|
||||||
|
|
||||||
async def _run_bot(self) -> None:
|
async def _run_bot(self) -> None:
|
||||||
"""Run the bot connection with auto-reconnect."""
|
"""Run the bot connection with auto-reconnect."""
|
||||||
@@ -88,11 +87,10 @@ class QQChannel(BaseChannel):
|
|||||||
async def stop(self) -> None:
|
async def stop(self) -> None:
|
||||||
"""Stop the QQ bot."""
|
"""Stop the QQ bot."""
|
||||||
self._running = False
|
self._running = False
|
||||||
if self._bot_task:
|
if self._client:
|
||||||
self._bot_task.cancel()
|
|
||||||
try:
|
try:
|
||||||
await self._bot_task
|
await self._client.close()
|
||||||
except asyncio.CancelledError:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
logger.info("QQ bot stopped")
|
logger.info("QQ bot stopped")
|
||||||
|
|
||||||
@@ -130,5 +128,5 @@ class QQChannel(BaseChannel):
|
|||||||
content=content,
|
content=content,
|
||||||
metadata={"message_id": data.id},
|
metadata={"message_id": data.id},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
logger.error("Error handling QQ message: {}", e)
|
logger.exception("Error handling QQ message")
|
||||||
|
|||||||
Reference in New Issue
Block a user