Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

lint

Alishahryar11cde6d425f1c
diff --git a/messaging/discord.py b/messaging/discord.py
index 90d46f3f..b866109b 100644
--- a/messaging/discord.py
+++ b/messaging/discord.py
@@ -6,7 +6,7 @@ Implements MessagingPlatform for Discord using discord.py.
import asyncio
import os
-from typing import Callable, Awaitable, Optional, Any, Set
+from typing import Callable, Awaitable, Optional, Any, Set, cast
from loguru import logger
@@ -14,17 +14,27 @@ from .base import MessagingPlatform
from .models import IncomingMessage
from .discord_markdown import format_status_discord
+_discord_module: Any = None
try:
- import discord
+ import discord as _discord_import
+ _discord_module = _discord_import
DISCORD_AVAILABLE = True
except ImportError:
- discord = None # type: ignore
DISCORD_AVAILABLE = False
DISCORD_MESSAGE_LIMIT = 2000
+def _get_discord() -> Any:
+ """Return the discord module. Raises if not available."""
+ if not DISCORD_AVAILABLE or _discord_module is None:
+ raise ImportError(
+ "discord.py is required. Install with: pip install discord.py"
+ )
+ return _discord_module
+
+
def _parse_allowed_channels(raw: Optional[str]) -> Set[str]:
"""Parse comma-separated channel IDs into a set of strings."""
if not raw or not raw.strip():
@@ -32,15 +42,16 @@ def _parse_allowed_channels(raw: Optional[str]) -> Set[str]:
return {s.strip() for s in raw.split(",") if s.strip()}
-if DISCORD_AVAILABLE and discord is not None:
+if DISCORD_AVAILABLE and _discord_module is not None:
+ _discord = _discord_module
- class _DiscordClient(discord.Client):
+ class _DiscordClient(_discord.Client):
"""Internal Discord client that forwards events to DiscordPlatform."""
def __init__(
self,
platform: "DiscordPlatform",
- intents: discord.Intents,
+ intents: _discord.Intents,
) -> None:
super().__init__(intents=intents)
self._platform = platform
@@ -84,10 +95,12 @@ class DiscordPlatform(MessagingPlatform):
if not self.bot_token:
logger.warning("DISCORD_BOT_TOKEN not set")
+ discord = _get_discord()
intents = discord.Intents.default()
intents.message_content = True
- self._client = _DiscordClient(self, intents) # type: ignore[misc]
+ assert _DiscordClient is not None
+ self._client = _DiscordClient(self, intents)
self._message_handler: Optional[
Callable[[IncomingMessage], Awaitable[None]]
] = None
@@ -217,15 +230,17 @@ class DiscordPlatform(MessagingPlatform):
raise RuntimeError(f"Channel {chat_id} not found")
text = self._truncate(text)
+ channel = cast(Any, channel)
+ discord = _get_discord()
if reply_to:
ref = discord.MessageReference(
message_id=int(reply_to),
channel_id=int(chat_id),
)
- msg = await channel.send(content=text, reference=ref) # type: ignore[union-attr]
+ msg = await channel.send(content=text, reference=ref)
else:
- msg = await channel.send(content=text) # type: ignore[union-attr]
+ msg = await channel.send(content=text)
return str(msg.id)
@@ -241,8 +256,10 @@ class DiscordPlatform(MessagingPlatform):
if not channel or not hasattr(channel, "fetch_message"):
raise RuntimeError(f"Channel {chat_id} not found")
+ discord = _get_discord()
+ channel = cast(Any, channel)
try:
- msg = await channel.fetch_message(int(message_id)) # type: ignore[union-attr]
+ msg = await channel.fetch_message(int(message_id))
except discord.NotFound:
return
@@ -259,8 +276,10 @@ class DiscordPlatform(MessagingPlatform):
if not channel or not hasattr(channel, "fetch_message"):
return
+ discord = _get_discord()
+ channel = cast(Any, channel)
try:
- msg = await channel.fetch_message(int(message_id)) # type: ignore[union-attr]
+ msg = await channel.fetch_message(int(message_id))
await msg.delete()
except discord.NotFound, discord.Forbidden:
pass
diff --git a/tests/test_discord_platform.py b/tests/test_discord_platform.py
index cf46812f..eb6e6750 100644
--- a/tests/test_discord_platform.py
+++ b/tests/test_discord_platform.py
@@ -49,9 +49,8 @@ class TestDiscordPlatform:
def test_empty_allowed_channels_rejects_all_messages(self):
"""When allowed_channel_ids is empty, no channels are allowed (secure default)."""
- platform = DiscordPlatform(
- bot_token="token", allowed_channel_ids=""
- )
+ with patch.dict("os.environ", {"ALLOWED_DISCORD_CHANNELS": ""}, clear=False):
+ platform = DiscordPlatform(bot_token="token", allowed_channel_ids="")
assert platform.allowed_channel_ids == set()
# Empty set means: not self.allowed_channel_ids is True -> reject
@@ -74,10 +73,11 @@ class TestDiscordPlatform:
mock_msg.id = 999
mock_channel = AsyncMock()
mock_channel.send = AsyncMock(return_value=mock_msg)
- platform._client.get_channel = MagicMock(return_value=mock_channel) # type: ignore[method-assign]
platform._connected = True
-
- msg_id = await platform.send_message("123", "Hello")
+ with patch.object(
+ platform._client, "get_channel", MagicMock(return_value=mock_channel)
+ ):
+ msg_id = await platform.send_message("123", "Hello")
assert msg_id == "999"
@pytest.mark.asyncio
@@ -86,8 +86,9 @@ class TestDiscordPlatform:
mock_msg = AsyncMock()
mock_channel = AsyncMock()
mock_channel.fetch_message = AsyncMock(return_value=mock_msg)
- platform._client.get_channel = MagicMock(return_value=mock_channel) # type: ignore[method-assign]
platform._connected = True
-
- await platform.edit_message("123", "456", "Updated text")
+ with patch.object(
+ platform._client, "get_channel", MagicMock(return_value=mock_channel)
+ ):
+ await platform.edit_message("123", "456", "Updated text")
mock_msg.edit.assert_called_once_with(content="Updated text")