2026-06-09 17:34:18 +07:00
|
|
|
import { type CustomLogger, createChildLogger } from "@bete/shared/logger";
|
2026-06-04 18:52:56 +07:00
|
|
|
import Redis from "ioredis";
|
2026-06-09 13:22:09 +07:00
|
|
|
import { type DiscordGatewayEvent, EventChannels } from "./eventTypes.js";
|
2026-06-01 21:44:29 +07:00
|
|
|
|
|
|
|
|
export class RedisEventPublisher {
|
|
|
|
|
private redis: Redis;
|
|
|
|
|
private logger: CustomLogger;
|
|
|
|
|
|
|
|
|
|
constructor(redisUrl: string, logger: CustomLogger) {
|
|
|
|
|
this.redis = new Redis(redisUrl);
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
|
|
|
|
this.redis.on("error", (err) => {
|
|
|
|
|
this.logger.error({ error: err }, "Redis connection error");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.redis.on("connect", () => {
|
|
|
|
|
this.logger.info("Redis connected");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async publish(channel: string, event: DiscordGatewayEvent): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
await this.redis.publish(channel, JSON.stringify(event));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error(
|
|
|
|
|
{ error, channel, eventType: event.type },
|
|
|
|
|
"Failed to publish event",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
|
|
|
|
await this.redis.quit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class EventBroadcaster {
|
|
|
|
|
private publisher: RedisEventPublisher;
|
2026-06-09 17:34:18 +07:00
|
|
|
private logger = createChildLogger("event-broadcaster");
|
2026-06-01 21:44:29 +07:00
|
|
|
|
2026-06-08 19:14:34 +07:00
|
|
|
constructor(publisher: RedisEventPublisher) {
|
2026-06-01 21:44:29 +07:00
|
|
|
this.publisher = publisher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async messageCreated(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing message_created");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.MESSAGE_CREATED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "message_created",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async messageUpdated(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing message_updated");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.MESSAGE_UPDATED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "message_updated",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async messageDeleted(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing message_deleted");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.MESSAGE_DELETED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "message_deleted",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async messageAnalyzed(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing message_analyzed");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.MESSAGE_ANALYZED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "message_analyzed",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async attachmentCreated(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing attachment_created");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.ATTACHMENT_CREATED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "attachment_created",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async attachmentUploaded(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing attachment_uploaded");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.ATTACHMENT_UPLOADED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "attachment_uploaded",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async voiceRecordingStarted(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing voice_recording_started");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.VOICE_STARTED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "voice_recording_started",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async voiceRecordingStopped(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing voice_recording_stopped");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.VOICE_STOPPED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "voice_recording_stopped",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async voiceRecordingUploaded(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing voice_recording_uploaded");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.VOICE_UPLOADED, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "voice_recording_uploaded",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 19:14:34 +07:00
|
|
|
/**
|
|
|
|
|
* Broadcasts PCM audio data for real-time voice streaming
|
|
|
|
|
* @param pcmBuffer - Raw PCM audio buffer
|
|
|
|
|
* @param userId - Discord user ID
|
|
|
|
|
* @param metadata - Optional metadata about the audio chunk
|
|
|
|
|
*/
|
|
|
|
|
async voicePcmData(
|
|
|
|
|
pcmBuffer: Buffer,
|
|
|
|
|
userId: string,
|
2026-06-09 10:16:04 +07:00
|
|
|
metadata?: Record<string, unknown>,
|
2026-06-08 19:14:34 +07:00
|
|
|
): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug(
|
|
|
|
|
{ userId, pcmSize: pcmBuffer.length },
|
|
|
|
|
"Publishing voice_pcm_data",
|
|
|
|
|
);
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.VOICE_PCM, {
|
2026-06-08 19:14:34 +07:00
|
|
|
type: "voice_pcm_data",
|
|
|
|
|
data: {
|
|
|
|
|
userId,
|
|
|
|
|
pcm: pcmBuffer.toString("base64"),
|
|
|
|
|
metadata,
|
|
|
|
|
},
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Broadcasts voice user activity state changes
|
|
|
|
|
* @param userId - Discord user ID
|
|
|
|
|
* @param data - User state data including username, avatar, and speaking status
|
|
|
|
|
*/
|
|
|
|
|
async voiceActiveUser(
|
|
|
|
|
userId: string,
|
|
|
|
|
data: { username: string; avatar: string; speaking: boolean },
|
|
|
|
|
): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug(
|
|
|
|
|
{ userId, speaking: data.speaking },
|
|
|
|
|
"Publishing voice_active_user",
|
|
|
|
|
);
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.VOICE_ACTIVE_USER, {
|
2026-06-08 19:14:34 +07:00
|
|
|
type: "voice_active_user",
|
|
|
|
|
data: {
|
|
|
|
|
userId,
|
|
|
|
|
...data,
|
|
|
|
|
},
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 21:44:29 +07:00
|
|
|
async analysisQueueStatus(data: unknown): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug({ data }, "Publishing analysis_queue_status");
|
2026-06-09 13:22:09 +07:00
|
|
|
await this.publisher.publish(EventChannels.ANALYSIS_QUEUE_STATUS, {
|
2026-06-01 21:44:29 +07:00
|
|
|
type: "analysis_queue_status",
|
|
|
|
|
data,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
source: "discord-gateway",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async close(): Promise<void> {
|
2026-06-09 17:34:18 +07:00
|
|
|
this.logger.debug("Closing event broadcaster");
|
2026-06-01 21:44:29 +07:00
|
|
|
await this.publisher.close();
|
|
|
|
|
}
|
|
|
|
|
}
|