feat: update application initialization for drizzle
This commit is contained in:
+7
-10
@@ -4,7 +4,7 @@ import "@snazzah/davey";
|
||||
import "dotenv/config";
|
||||
import { Client } from "discord.js-selfbot-v13";
|
||||
import { config } from "./config";
|
||||
import { getDatabase } from "./database/adapter";
|
||||
import { initializeDatabase, closeDatabase } from "./database/drizzle";
|
||||
import { createChildLogger } from "./logger";
|
||||
import { startPendingAIAnalysisWorker } from "./moderation/aiAnalyzer";
|
||||
import { syncBacklogMessages } from "./moderation/backlogSync";
|
||||
@@ -26,7 +26,6 @@ const client = new Client();
|
||||
const voiceController = new VoiceController(client);
|
||||
|
||||
let isShuttingDown = false;
|
||||
let db: Awaited<ReturnType<typeof getDatabase>> | null = null;
|
||||
|
||||
async function gracefulShutdown(signal: string) {
|
||||
if (isShuttingDown) {
|
||||
@@ -39,10 +38,8 @@ async function gracefulShutdown(signal: string) {
|
||||
|
||||
try {
|
||||
logger.info("Closing database...");
|
||||
if (db) {
|
||||
await db.close();
|
||||
logger.info("Database closed");
|
||||
}
|
||||
await closeDatabase();
|
||||
logger.info("Database closed");
|
||||
|
||||
logger.info("Stopping voice connection...");
|
||||
await voiceController.disconnect();
|
||||
@@ -67,8 +64,8 @@ async function gracefulShutdown(signal: string) {
|
||||
|
||||
async function initializeApp() {
|
||||
try {
|
||||
logger.info("Initializing database adapter");
|
||||
db = await getDatabase();
|
||||
logger.info("Initializing database");
|
||||
await initializeDatabase();
|
||||
logger.info({ type: config.DATABASE_TYPE }, "Database initialized");
|
||||
} catch (err) {
|
||||
logger.error({ error: err }, "Failed to initialize database");
|
||||
@@ -77,9 +74,9 @@ async function initializeApp() {
|
||||
|
||||
client.on("ready", async () => {
|
||||
logger.info({ user: client.user?.tag }, "Bot logged in");
|
||||
registerMessageCapture(client, db!);
|
||||
registerMessageCapture(client);
|
||||
startPendingAIAnalysisWorker();
|
||||
syncBacklogMessages(client, db!).catch((error) => {
|
||||
syncBacklogMessages(client).catch((error) => {
|
||||
logger.warn({ error }, "Backlog sync failed");
|
||||
});
|
||||
await startWebserver(config.WEBSERVER_PORT, client, voiceController);
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import type { Client, Message } from "discord.js-selfbot-v13";
|
||||
import { config } from "../config";
|
||||
import { createChildLogger } from "../logger";
|
||||
import type { SqliteDatabase } from "../muxer-queue";
|
||||
import { captureMessage } from "./messageCapture";
|
||||
|
||||
const logger = createChildLogger("backlog-sync");
|
||||
|
||||
async function syncChannelMessages(
|
||||
db: SqliteDatabase,
|
||||
channel: any,
|
||||
cutoffTime: number,
|
||||
): Promise<number> {
|
||||
@@ -31,7 +29,7 @@ async function syncChannelMessages(
|
||||
continue;
|
||||
}
|
||||
|
||||
await captureMessage(db, message, "text");
|
||||
await captureMessage(message, "text");
|
||||
synced++;
|
||||
}
|
||||
|
||||
@@ -44,7 +42,6 @@ async function syncChannelMessages(
|
||||
|
||||
export async function syncBacklogMessages(
|
||||
client: Client,
|
||||
db: SqliteDatabase,
|
||||
): Promise<void> {
|
||||
if (!config.MONITOR_GUILD_ID) {
|
||||
logger.warn("MONITOR_GUILD_ID not configured, skipping backlog sync");
|
||||
@@ -68,7 +65,6 @@ export async function syncBacklogMessages(
|
||||
|
||||
export async function syncSelectedChannelBacklog(
|
||||
client: Client,
|
||||
db: SqliteDatabase,
|
||||
guildId: string,
|
||||
channelId: string,
|
||||
): Promise<number> {
|
||||
@@ -91,7 +87,7 @@ export async function syncSelectedChannelBacklog(
|
||||
);
|
||||
|
||||
try {
|
||||
const count = await syncChannelMessages(db, channel as any, cutoffTime);
|
||||
const count = await syncChannelMessages(channel as any, cutoffTime);
|
||||
logger.info(
|
||||
{ channelId, count },
|
||||
"Backlog sync completed for selected channel",
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { Client, Message } from "discord.js-selfbot-v13";
|
||||
import { config } from "../config";
|
||||
import { createChildLogger } from "../logger";
|
||||
import type { SqliteDatabase } from "../muxer-queue";
|
||||
import { getDatabase } from "../database/drizzle";
|
||||
import { messagesTable } from "../database/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { queueMessageAnalysis } from "./aiAnalyzer";
|
||||
import {
|
||||
getDisplayContent,
|
||||
@@ -14,7 +16,6 @@ import type { AttachmentRecord, MessageRecord } from "./types";
|
||||
const logger = createChildLogger("message-capture");
|
||||
|
||||
export async function captureMessage(
|
||||
db: SqliteDatabase,
|
||||
message: Message,
|
||||
type: "text" | "edited" | "deleted",
|
||||
): Promise<void> {
|
||||
@@ -95,14 +96,13 @@ export async function captureMessage(
|
||||
|
||||
export function registerMessageCapture(
|
||||
client: Client,
|
||||
db: SqliteDatabase,
|
||||
): void {
|
||||
client.on("messageCreate", async (message) => {
|
||||
if (!message.guildId || message.guildId !== config.MONITOR_GUILD_ID) return;
|
||||
if (message.author?.bot) return;
|
||||
|
||||
try {
|
||||
await captureMessage(db, message, "text");
|
||||
await captureMessage(message, "text");
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
{
|
||||
@@ -121,12 +121,15 @@ export function registerMessageCapture(
|
||||
|
||||
try {
|
||||
const { updateMessageAsEdited } = await import("./messageStore");
|
||||
const db = getDatabase() as any;
|
||||
|
||||
const existing = db
|
||||
.prepare("SELECT id FROM messages WHERE id = ?")
|
||||
.get(newMessage.id) as { id: string } | undefined;
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(messagesTable)
|
||||
.where(eq(messagesTable.id, newMessage.id))
|
||||
.limit(1);
|
||||
|
||||
if (existing) {
|
||||
if (existing.length > 0) {
|
||||
const editedAt = Date.now();
|
||||
await updateMessageAsEdited(
|
||||
newMessage.id,
|
||||
@@ -144,7 +147,7 @@ export function registerMessageCapture(
|
||||
});
|
||||
}
|
||||
} else if (newMessage.author) {
|
||||
await captureMessage(db, newMessage as Message, "text");
|
||||
await captureMessage(newMessage as Message, "text");
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
|
||||
+2
-3
@@ -14,10 +14,11 @@ import {
|
||||
getMessagesByChannel,
|
||||
} from "./moderation/messageStore";
|
||||
import {
|
||||
getDatabase,
|
||||
getDatabase as getMuxerDatabase,
|
||||
getPersistedValue,
|
||||
setPersistedValue,
|
||||
} from "./muxer-queue";
|
||||
import { getDatabase } from "./database/drizzle";
|
||||
import { discordPlayer } from "./player";
|
||||
import type { VoiceController } from "./voiceController";
|
||||
|
||||
@@ -260,7 +261,6 @@ export async function startWebserver(
|
||||
// Moderation API endpoints
|
||||
app.get("/api/messages", async (req, res, next) => {
|
||||
try {
|
||||
const db = await getDatabase();
|
||||
const {
|
||||
channel,
|
||||
type,
|
||||
@@ -325,7 +325,6 @@ export async function startWebserver(
|
||||
|
||||
const count = await syncSelectedChannelBacklog(
|
||||
_client,
|
||||
await getDatabase(),
|
||||
guildId,
|
||||
channelId,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user