Initial commit: Discord-Claude Gateway with event-driven agent runtime
This commit is contained in:
@@ -121,16 +121,32 @@ export class DiscordBot {
|
||||
|
||||
private setupMessageHandler(): void {
|
||||
this.client.on("messageCreate", (message: Message) => {
|
||||
if (shouldIgnoreMessage(message)) return;
|
||||
console.log(`[DEBUG] Message received: "${message.content}" from ${message.author.tag} (bot: ${message.author.bot})`);
|
||||
|
||||
if (shouldIgnoreMessage(message)) {
|
||||
console.log("[DEBUG] Ignoring bot message");
|
||||
return;
|
||||
}
|
||||
|
||||
const botUser = this.client.user;
|
||||
if (!botUser) return;
|
||||
if (!botUser) {
|
||||
console.log("[DEBUG] No bot user available");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!message.mentions.has(botUser)) return;
|
||||
if (!message.mentions.has(botUser)) {
|
||||
console.log("[DEBUG] Message does not mention the bot");
|
||||
return;
|
||||
}
|
||||
|
||||
const text = extractPromptFromMention(message.content, botUser.id);
|
||||
if (!text) return;
|
||||
console.log(`[DEBUG] Extracted prompt: "${text}"`);
|
||||
if (!text) {
|
||||
console.log("[DEBUG] Empty prompt after extraction, ignoring");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[DEBUG] Forwarding prompt to handler: "${text}" from channel ${message.channelId}`);
|
||||
this.promptHandler?.({
|
||||
text,
|
||||
channelId: message.channelId,
|
||||
|
||||
@@ -92,8 +92,10 @@ export class GatewayCore {
|
||||
|
||||
// 8. Register EventQueue processing handler
|
||||
this.eventQueue.onEvent(async (event: Event) => {
|
||||
console.log(`[DEBUG] Processing event: type=${event.type}, id=${event.id}`);
|
||||
try {
|
||||
const result = await this.agentRuntime.processEvent(event);
|
||||
console.log(`[DEBUG] Event result: responseText=${result.responseText?.length ?? 0} chars, error=${result.error ?? "none"}`);
|
||||
|
||||
if (result.responseText && result.targetChannelId) {
|
||||
const chunks = splitMessage(result.responseText);
|
||||
@@ -107,7 +109,6 @@ export class GatewayCore {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error processing event:", error);
|
||||
// Attempt to notify the channel if it's a message event
|
||||
if (event.type === "message") {
|
||||
const payload = event.payload as MessagePayload;
|
||||
const errorMsg = formatErrorForUser(error);
|
||||
@@ -116,7 +117,6 @@ export class GatewayCore {
|
||||
.catch(() => {});
|
||||
}
|
||||
} finally {
|
||||
// Decrement active query count for message events
|
||||
if (event.type === "message") {
|
||||
this.activeQueryCount--;
|
||||
}
|
||||
@@ -125,6 +125,8 @@ export class GatewayCore {
|
||||
|
||||
// 9. Wire DiscordBot.onPrompt() to create message events and enqueue them
|
||||
this.discordBot.onPrompt((prompt: Prompt) => {
|
||||
console.log(`[DEBUG] onPrompt called: "${prompt.text}" from channel ${prompt.channelId}`);
|
||||
|
||||
if (this.isShuttingDown) {
|
||||
this.discordBot
|
||||
.sendMessage(prompt.channelId, "Gateway is shutting down. Please try again later.")
|
||||
|
||||
Reference in New Issue
Block a user