/** * Mention handler for Discord bot interactions * * Provides utilities for: * - Detecting bot mentions in messages * - Stripping mentions from prompt text * - Building conversation context from message history */ import type { Collection, DMChannel, Message, NewsChannel, Snowflake, TextChannel, ThreadChannel } from "discord.js"; /** * Supported text-based channel types for message fetching */ export type TextBasedChannel = TextChannel | DMChannel | NewsChannel | ThreadChannel; /** * A processed message for context building */ export interface ContextMessage { /** Discord user ID of the message author */ authorId: string; /** Display name or username of the message author */ authorName: string; /** Whether the author is a bot */ isBot: boolean; /** Whether this is the bot's own message */ isSelf: boolean; /** Message content with any bot mentions stripped */ content: string; /** ISO timestamp of when the message was created */ timestamp: string; /** The original message ID */ messageId: string; } /** * Options for building conversation context */ export interface ContextBuildOptions { /** Maximum number of messages to include (default: 10) */ maxMessages?: number; /** Whether to include bot messages in context (default: true) */ includeBotMessages?: boolean; /** Whether to prioritize user messages over bot messages (default: true) */ prioritizeUserMessages?: boolean; } /** * Result of context building */ export interface ConversationContext { /** Processed messages in chronological order (oldest first) */ messages: ContextMessage[]; /** The triggering message with mention stripped */ prompt: string; /** Whether the bot was mentioned in the triggering message */ wasMentioned: boolean; } /** * Check if a message mentions a specific bot user * * Checks both direct user mentions (@BotName) and role mentions where * the bot is a member of the mentioned role. This handles the common case * where Discord auto-creates a managed role for bots and users accidentally * mention the role instead of the user. * * @param message - Discord message to check * @param botUserId - The bot's user ID * @returns true if the bot is mentioned (directly or via role), false otherwise * * @example * ```typescript * if (isBotMentioned(message, client.user.id)) { * // Handle the mention * } * ``` */ export declare function isBotMentioned(message: Message, botUserId: string): boolean; /** * Check if a message should be processed based on channel mode * * @param message - Discord message to check * @param botUserId - The bot's user ID * @param mode - Channel mode ('mention' or 'auto') * @returns true if the message should be processed, false otherwise * * @example * ```typescript * if (shouldProcessMessage(message, client.user.id, 'mention')) { * // Bot was mentioned, process the message * } * ``` */ export declare function shouldProcessMessage(message: Message, botUserId: string, mode: "mention" | "auto"): boolean; /** * Strip bot mention from message content * * Removes the bot mention (e.g., <@123456789>) from the message content * and trims any excess whitespace. * * @param content - Message content to process * @param botUserId - The bot's user ID * @returns Content with the bot mention removed * * @example * ```typescript * const prompt = stripBotMention("<@123> help me with this", "123"); * // Returns: "help me with this" * ``` */ export declare function stripBotMention(content: string, botUserId: string): string; /** * Strip bot role mentions from message content * * Removes role mentions (e.g., <@&123456789>) where the bot is a member * of that role. This handles the case where users mention the bot's * auto-created managed role instead of the bot user directly. * * @param content - Message content to process * @param message - The Discord message (to access role mentions) * @param botUserId - The bot's user ID * @returns Content with bot role mentions removed */ export declare function stripBotRoleMentions(content: string, message: Message, botUserId: string): string; /** * Strip all bot mentions from message content * * Removes all user mentions from the message content. Useful for * cleaning context messages. * * @param content - Message content to process * @param botUserId - The bot's user ID (optional, if provided only strips this bot) * @returns Content with mentions removed */ export declare function stripMentions(content: string, botUserId?: string): string; /** * Process a Discord message into a context message * * @param message - Discord message to process * @param botUserId - The bot's user ID * @returns Processed context message */ export declare function processMessage(message: Message, botUserId: string): ContextMessage; /** * Fetch recent message history from a channel * * @param channel - Text-based channel to fetch from * @param beforeMessageId - Fetch messages before this message ID * @param limit - Maximum number of messages to fetch * @returns Collection of messages */ export declare function fetchMessageHistory(channel: TextBasedChannel, beforeMessageId: string, limit: number): Promise>; /** * Build conversation context from message history * * Fetches recent messages from the channel and processes them into * a conversation context suitable for sending to Claude. * * @param triggerMessage - The message that triggered the bot * @param channel - The channel to fetch history from * @param botUserId - The bot's user ID * @param options - Context building options * @returns Conversation context with processed messages * * @example * ```typescript * const context = await buildConversationContext( * message, * message.channel, * client.user.id, * { maxMessages: 10, prioritizeUserMessages: true } * ); * * // Use context.prompt as the main prompt * // Use context.messages for conversation history * ``` */ export declare function buildConversationContext(triggerMessage: Message, channel: TextBasedChannel, botUserId: string, options?: ContextBuildOptions): Promise; /** * Format conversation context as a string for Claude * * Creates a formatted string representation of the conversation context * suitable for including in a prompt to Claude. * * @param context - The conversation context to format * @returns Formatted string representation * * @example * ```typescript * const formatted = formatContextForPrompt(context); * // Returns: * // [User123 at 2024-01-20T10:00:00Z]: How do I use this feature? * // [BotName at 2024-01-20T10:00:30Z]: Here's how you can... * // [User123 at 2024-01-20T10:01:00Z]: Thanks, but what about... * ``` */ export declare function formatContextForPrompt(context: ConversationContext): string; //# sourceMappingURL=mention-handler.d.ts.map