/** * DiscordConnector - Connects an agent to Discord * * Each agent has its own DiscordConnector instance with its own bot identity. * The connector uses discord.js v14 to connect to the Discord gateway and * handles connection lifecycle events. */ import { EventEmitter } from "node:events"; import type { IChatSessionManager } from "@herdctl/chat"; import { Client } from "discord.js"; import { type ICommandManager } from "./commands/index.js"; import type { DiscordConnectorEventMap, DiscordConnectorEventName, DiscordConnectorOptions, DiscordConnectorState, DiscordFileUploadParams, IDiscordConnector } from "./types.js"; /** * DiscordConnector class - Connects a single agent to Discord * * Each agent has its own connector instance with: * - Its own discord.js Client * - Its own bot token and identity * - Connection lifecycle management * - Event emission for monitoring * * @example * ```typescript * const connector = new DiscordConnector({ * agentConfig, * discordConfig: agentConfig.chat.discord, * botToken: process.env.MY_BOT_TOKEN, * fleetManager, * }); * * await connector.connect(); * console.log(`Connected as ${connector.getState().botUser?.username}`); * * // Later... * await connector.disconnect(); * ``` */ export declare class DiscordConnector extends EventEmitter implements IDiscordConnector { private readonly _agentConfig; private readonly _discordConfig; private readonly _botToken; private readonly _logger; private readonly _sessionManager; private readonly _commandActions?; private readonly _commandRegistration?; private readonly _errorHandler; private _client; private _commandManager; private _status; private _connectedAt; private _disconnectedAt; private _reconnectAttempts; private _lastError; private _botUser; private _rateLimitCount; private _lastRateLimitAt; private _rateLimitResetTime; private _rateLimitResetTimer; private _messagesReceived; private _messagesSent; private _messagesIgnored; constructor(options: DiscordConnectorOptions); /** * Get the session manager instance */ get sessionManager(): IChatSessionManager; /** * Get the command manager instance (available after connect) */ get commandManager(): ICommandManager | null; /** * Name of the agent this connector is for */ get agentName(): string; /** * Get the discord.js Client instance (for testing) */ get client(): Client | null; /** * Connect to Discord gateway * * Creates a new discord.js Client and connects to the gateway. * Registers event handlers for connection lifecycle events. * * @throws AlreadyConnectedError if already connected * @throws DiscordConnectionError on connection failure */ connect(): Promise; /** * Disconnect from Discord gateway * * Performs graceful shutdown by destroying the client. * Does not throw on failure - logs errors and completes. */ disconnect(): Promise; /** * Check if currently connected to Discord * * @returns true if connected and ready, false otherwise */ isConnected(): boolean; /** * Get current connector state * * @returns Current state including connection status and metadata */ getState(): DiscordConnectorState; uploadFile(params: DiscordFileUploadParams): Promise<{ fileId: string; }>; /** * Set up event handlers for the discord.js client */ private _setupEventHandlers; /** * Initialize and register slash commands */ private _initializeCommands; /** * Handle an incoming interaction (slash command) */ private _handleInteraction; /** * Handle an incoming message * * Determines if the message should be processed based on channel configuration * and mention mode settings. If the message should be processed, builds * conversation context and emits a 'message' event. * * For DMs: * - DMs default to auto mode (no mention required) * - Allowlist/blocklist filtering is applied * * For guild channels: * - Mode is determined by channel configuration * - Only configured channels are processed */ private _handleMessage; /** * Categorize a MIME content type into an attachment processing category */ private static _categorizeContentType; /** * Set bot presence based on configuration */ private _setPresence; /** * Handle rate limit events from discord.js REST client * * Discord.js automatically queues and retries requests when rate limited. * This method tracks rate limit occurrences and emits events for monitoring. */ private _handleRateLimit; /** * Type-safe event emitter methods */ emit(event: K, payload: DiscordConnectorEventMap[K]): boolean; on(event: K, listener: (payload: DiscordConnectorEventMap[K]) => void): this; once(event: K, listener: (payload: DiscordConnectorEventMap[K]) => void): this; off(event: K, listener: (payload: DiscordConnectorEventMap[K]) => void): this; } //# sourceMappingURL=discord-connector.d.ts.map