From 536c456e5e9cf8fc38971755ee9e079a51f1cf86 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Thu, 7 May 2026 11:14:57 +0800 Subject: [PATCH] fix(channels): restore bound logger in discord and websocket PR introduced module-level logger in static methods, which drops the channel context bound by BaseChannel.__init__. Revert to self._channel.logger / self.logger to preserve log labels. Also remove @staticmethod since these methods legitimately need instance access (F821 was the real issue, not the logger source). --- nanobot/channels/discord.py | 5 ++--- nanobot/channels/websocket.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/nanobot/channels/discord.py b/nanobot/channels/discord.py index 1d3ddb6f8..6e6a4d9d2 100644 --- a/nanobot/channels/discord.py +++ b/nanobot/channels/discord.py @@ -10,7 +10,6 @@ from dataclasses import dataclass from pathlib import Path from typing import TYPE_CHECKING, Any, Literal -from loguru import logger from pydantic import Field from nanobot.bus.events import OutboundMessage @@ -309,8 +308,8 @@ if DISCORD_AVAILABLE: fallback = "\n".join(f"[attachment: {name} - send failed]" for name in failed_media) return split_message(fallback, MAX_MESSAGE_LEN) - @staticmethod def _build_reply_context( + self, channel: Messageable, reply_to: str | None, ) -> tuple[discord.PartialMessage | None, discord.AllowedMentions]: @@ -321,7 +320,7 @@ if DISCORD_AVAILABLE: try: message_id = int(reply_to) except ValueError: - logger.warning("Invalid reply target: {}", reply_to) + self._channel.logger.warning("Invalid reply target: {}", reply_to) return None, mention_settings return channel.get_partial_message(message_id), mention_settings diff --git a/nanobot/channels/websocket.py b/nanobot/channels/websocket.py index 39d953894..459276501 100644 --- a/nanobot/channels/websocket.py +++ b/nanobot/channels/websocket.py @@ -1089,8 +1089,8 @@ class WebSocketChannel(BaseChannel): finally: self._cleanup_connection(connection) - @staticmethod def _save_envelope_media( + self, media: list[Any], ) -> tuple[list[str], str | None]: """Decode and persist ``media`` items from a ``message`` envelope. @@ -1125,7 +1125,7 @@ class WebSocketChannel(BaseChannel): try: Path(p).unlink(missing_ok=True) except OSError as exc: - logger.warning( + self.logger.warning( "failed to unlink partial media {}: {}", p, exc ) return [], reason @@ -1150,7 +1150,7 @@ class WebSocketChannel(BaseChannel): except FileSizeExceeded: return _abort("size") except Exception as exc: - logger.warning("media decode failed: {}", exc) + self.logger.warning("media decode failed: {}", exc) return _abort("decode") if saved is None: return _abort("decode")