Source code for tools.discord_react

"""Add a reaction emoji to a Discord message."""

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_react"
TOOL_DESCRIPTION = (
    "Add a reaction emoji to a Discord message the bot can see. "
    "Always pass channel_id when you know it—lookup is instant; "
    "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 react to.",
        },
        "emoji": {
            "type": "string",
            "description": (
                "The emoji to react with. Unicode emoji or "
                "custom format <:name:id>."
            ),
        },
        "channel_id": {
            "type": "string",
            "description": (
                "Channel containing the message. Strongly recommended: "
                "omitting it forces a slow search across accessible channels."
            ),
        },
    },
    "required": ["message_id", "emoji"],
}


[docs] async def run( message_id: str, emoji: 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. emoji (str): The emoji value. channel_id (str | None): Discord/Matrix channel identifier. ctx (ToolContext | None): Tool execution context providing access to bot internals. Returns: str: Result string. """ import discord from tools._discord_helpers import ( require_discord_client, resolve_discord_message, ) 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}'." resolved = await resolve_discord_message( client, msg_id, message_id_display=message_id, channel_id=channel_id, ctx=ctx, ) if isinstance(resolved, str): return resolved try: await resolved.add_reaction(emoji) except discord.errors.Forbidden: return "Error: No permission to add reactions." except discord.errors.HTTPException as exc: return f"Error adding reaction: {exc}" ch = resolved.channel ch_name = getattr(ch, "name", None) or str(ch.id) return ( f"Added reaction '{emoji}' to message " f"'{message_id}' in '{ch_name}'." )