Source code for tools.discord_edit_message

"""Edit a Discord message previously sent by the bot."""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from tool_context import ToolContext

logger = logging.getLogger(__name__)

TOOL_NAME = "discord_edit_message"
TOOL_DESCRIPTION = (
    "Edit a Discord message that was sent by the bot. "
    "Pass channel_id whenever possible—instant lookup; "
    "without it the bot may scan many channels (slow)."
)
TOOL_PARAMETERS = {
    "type": "object",
    "properties": {
        "message_id": {
            "type": "string",
            "description": "The ID of the message to edit.",
        },
        "new_content": {
            "type": "string",
            "description": "The new content for the message.",
        },
        "channel_id": {
            "type": "string",
            "description": (
                "Channel containing the message. Strongly recommended "
                "for fast lookup."
            ),
        },
    },
    "required": ["message_id", "new_content"],
}


[docs] async def run( message_id: str, new_content: str, channel_id: str | None = None, ctx: ToolContext | None = None, ) -> str: """Execute this tool and return the result. Args: message_id (str): Unique identifier for the message. new_content (str): The new content value. channel_id (str | None): Discord/Matrix channel identifier. ctx (ToolContext | None): Tool execution context providing access to bot internals. Returns: str: Result string. """ from tools._discord_helpers import ( require_discord_client, resolve_discord_message_bot_author, ) client = require_discord_client(ctx) if isinstance(client, str): return client try: msg_id = int(message_id) except ValueError: return ( f"Error: Invalid message ID: '{message_id}'. " f"Must be a number." ) bot_id = client.user.id resolved = await resolve_discord_message_bot_author( client, msg_id, message_id_display=message_id, bot_id=bot_id, channel_id=channel_id, ctx=ctx, own_message_verb="edit", ) if isinstance(resolved, str): return resolved await resolved.edit(content=new_content) ch = resolved.channel ch_name = getattr(ch, "name", None) or str(ch.id) return f"Edited message '{message_id}' in '{ch_name}'."