delete_message
Remove messages from Discord channels by specifying server, channel, and message IDs to manage content and maintain server organization.
Instructions
Delete a message from a channel
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 delete | |
| reason | No | Reason for deletion |
Implementation Reference
- src/tools/message-tools.ts:162-182 (handler)The main execution logic for the 'delete_message' tool. Fetches the Discord guild, channel, and message, validates the channel type, deletes the message, handles errors with withErrorHandling, and returns a formatted response.async ({ guildId, channelId, messageId, reason }) => { 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); await message.delete(); return { messageId, message: 'Message deleted successfully' }; }); 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:156-161 (schema)Zod input schema defining parameters for delete_message: required guildId, channelId, messageId; optional reason.{ 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 delete'), reason: z.string().optional().describe('Reason for deletion'), },
- src/tools/message-tools.ts:153-183 (registration)The server.tool() registration call for 'delete_message', including name, description, input schema, and handler within the registerMessageTools function.server.tool( 'delete_message', 'Delete a message from a channel', { 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 delete'), reason: z.string().optional().describe('Reason for deletion'), }, async ({ guildId, channelId, messageId, reason }) => { 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); await message.delete(); return { messageId, message: 'Message deleted successfully' }; }); if (!result.success) { return { content: [{ type: 'text', text: result.error }], isError: true }; } return { content: [{ type: 'text', text: JSON.stringify(result.data, null, 2) }] }; } );
- src/index.ts:59-59 (registration)Invocation of registerMessageTools(server) in createMcpServer(), which registers all message tools including delete_message.registerMessageTools(server);
- src/tools/message-tools.ts:9-17 (helper)Type guard helper function used in the delete_message handler (and others) to validate if a channel is messageable (Text, News, Thread channels).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; }