edit_message
Modify existing bot messages in Discord servers by providing new content, channel ID, and message ID to update communications.
Instructions
Edit a message sent by the bot
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| guildId | Yes | The ID of the server (guild) | |
| channelId | Yes | The ID of the channel | |
| messageId | Yes | The ID of the message to edit | |
| content | Yes | The new message content |
Implementation Reference
- src/tools/message-tools.ts:116-150 (registration)Registers the 'edit_message' MCP tool with input schema (guildId, channelId, messageId, content) and handler function that fetches the Discord channel and message, edits the message content, and returns the updated message details.server.tool( 'edit_message', 'Edit a message sent by the bot', { guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the channel'), messageId: z.string().describe('The ID of the message to edit'), content: z.string().describe('The new message content'), }, async ({ guildId, channelId, messageId, content }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!isMessageableChannel(channel)) { throw new Error('Channel does not support messages'); } const message = await channel.messages.fetch(messageId); const edited = await message.edit(content); return { messageId: edited.id, content: edited.content, editedAt: edited.editedAt?.toISOString(), }; }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } );
- src/tools/message-tools.ts:125-149 (handler)The async handler function for the edit_message tool, which uses Discord.js to edit a specified message and handles errors via withErrorHandling.async ({ guildId, channelId, messageId, content }) => { const result = await withErrorHandling(async () => { const client = await getDiscordClient(); const guild = await client.guilds.fetch(guildId); const channel = await guild.channels.fetch(channelId); if (!isMessageableChannel(channel)) { throw new Error('Channel does not support messages'); } const message = await channel.messages.fetch(messageId); const edited = await message.edit(content); return { messageId: edited.id, content: edited.content, editedAt: edited.editedAt?.toISOString(), }; }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; }
- src/tools/message-tools.ts:120-124 (schema)Zod input schema defining parameters for the edit_message tool.guildId: z.string().describe('The ID of the server (guild)'), channelId: z.string().describe('The ID of the channel'), messageId: z.string().describe('The ID of the message to edit'), content: z.string().describe('The new message content'), },
- src/index.ts:58-58 (registration)Calls registerMessageTools to register all message tools including edit_message on the MCP server instance.registerMessageTools(server);
- src/tools/message-tools.ts:9-17 (helper)Helper function used by edit_message handler to validate if the channel supports sending/editing messages.function isMessageableChannel(channel: unknown): channel is MessageableChannel { if (!channel || typeof channel !== 'object') return false; const ch = channel as { type?: number }; return ch.type === ChannelType.GuildText || ch.type === ChannelType.GuildAnnouncement || ch.type === ChannelType.PublicThread || ch.type === ChannelType.PrivateThread || ch.type === ChannelType.AnnouncementThread; }