AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): Add channel ID support to SendDiscordMessageBlock for consistency with other Discord blocks (#11055)
Copilot9bc9b53b99e9
diff --git a/autogpt_platform/backend/backend/blocks/discord/bot_blocks.py b/autogpt_platform/backend/backend/blocks/discord/bot_blocks.pyindex 22469de2d4..a0fde74e69 100644--- a/autogpt_platform/backend/backend/blocks/discord/bot_blocks.py+++ b/autogpt_platform/backend/backend/blocks/discord/bot_blocks.py@@ -171,11 +171,11 @@ class SendDiscordMessageBlock(Block):description="The content of the message to send")channel_name: str = SchemaField(- description="The name of the channel the message will be sent to"+ description="Channel ID or channel name to send the message to")server_name: str = SchemaField(- description="The name of the server where the channel is located",- advanced=True, # Optional field for server name+ description="Server name (only needed if using channel name)",+ advanced=True,default="",)@@ -231,25 +231,49 @@ class SendDiscordMessageBlock(Block):@client.eventasync def on_ready():print(f"Logged in as {client.user}")- for guild in client.guilds:- if server_name and guild.name != server_name:- continue- for channel in guild.text_channels:- if channel.name == channel_name:- # Split message into chunks if it exceeds 2000 characters- chunks = self.chunk_message(message_content)- last_message = None- for chunk in chunks:- last_message = await channel.send(chunk)- result["status"] = "Message sent"- result["message_id"] = (- str(last_message.id) if last_message else ""- )- result["channel_id"] = str(channel.id)- await client.close()- return-- result["status"] = "Channel not found"+ channel = None++ # Try to parse as channel ID first+ try:+ channel_id = int(channel_name)+ channel = client.get_channel(channel_id)+ except ValueError:+ # Not a valid ID, will try name lookup+ pass++ # If not found by ID (or not an ID), try name lookup+ if not channel:+ for guild in client.guilds:+ if server_name and guild.name != server_name:+ continue+ for ch in guild.text_channels:+ if ch.name == channel_name:+ channel = ch+ break+ if channel:+ break++ if not channel:+ result["status"] = f"Channel not found: {channel_name}"+ await client.close()+ return++ # Type check - ensure it's a text channel that can send messages+ if not hasattr(channel, "send"):+ result["status"] = (+ f"Channel {channel_name} cannot receive messages (not a text channel)"+ )+ await client.close()+ return++ # Split message into chunks if it exceeds 2000 characters+ chunks = self.chunk_message(message_content)+ last_message = None+ for chunk in chunks:+ last_message = await channel.send(chunk) # type: ignore+ result["status"] = "Message sent"+ result["message_id"] = str(last_message.id) if last_message else ""+ result["channel_id"] = str(channel.id)await client.close()await client.start(token)diff --git a/docs/content/platform/blocks/discord.md b/docs/content/platform/blocks/discord.mdindex 36479e75c7..fa08093463 100644--- a/docs/content/platform/blocks/discord.md+++ b/docs/content/platform/blocks/discord.md@@ -43,7 +43,7 @@ The block uses a Discord bot to log into a server, locate the specified channel,|-------|-------------|| Discord Bot Token | A secret token used to authenticate the bot with Discord || Message Content | The text content of the message to be sent |-| Channel Name | The name of the Discord channel where the message should be sent |+| Channel Name | Channel ID or channel name to send the message to |### Outputs| Output | Description |