1import { Client, Events, GatewayIntentBits } from 'discord.js';
2import 'dotenv/config';
3import { saveDMMessages } from './database.js';
4
5/**
89 console.log(`Fetched ${messages.length} messages`);
90
91 // Save messages to database
92 await saveDMMessages(messages);
93
5## Features
6
7- **Message History Storage**: Securely stores your DM history in a SQLite database
8- **Link Detection & Categorization**: Automatically detects and categorizes links shared in conversations
9- **AI-Powered Querying**: Ask natural language questions about your conversation history
53 - `OPENAI_API_KEY`: Your OpenAI API key
54
555. Initialize the database
56 ```
57 node index.js
124- `/analyze [query]`: Analyze your conversation with AI
125
126## Database Schema
127
128The application uses a SQLite database with the following tables:
129
130- `messages`: Stores all DM messages
148- OpenAI for AI-powered querying
149- Val.town for hosting and scheduling
150- Better-SQLite3 for database management
198```
199โโโ backend/
200โ โโโ database/
201โ โ โโโ migrations.ts # Schema definitions
202โ โ โโโ queries.ts # DB query functions
257 ```
258
259### Database Patterns
260- Run migrations on startup or comment out for performance
261- Change table names when modifying schemas rather than altering
1import { initializeDatabase } from './database.js';
2import { fetchAndStoreDMs } from './discord-client.js';
3import { batchProcessLinks } from './link-analyzer.js';
10app.use(express.json());
11
12// Initialize database
13initializeDatabase();
14
15// API routes
134 console.log('Initializing Discord DM Analyzer...');
135
136 // Initialize database
137 const db = initializeDatabase();
138 console.log('Database initialized');
139
140 // Additional setup if needed
1import linkify from 'linkify-it';
2import { getMessages, updateLinkCategory } from './database.js';
3import { categorizeContent } from './anthropic-service.js';
4import 'dotenv/config';
174
175/**
176 * Batch process all links in the database
177 * - Categorizes uncategorized links
178 * - Updates link metadata
194 const metadata = await extractLinkMetadata(link.url);
195
196 // Update link in database
197 updateLinkCategory(link.id, categorization.category);
198
12The project is structured to work with Val.town and has the following components:
13
14- **Database Layer**: Uses Val.town's built-in SQLite database
15- **AI Integration**: Leverages Anthropic's Claude API for analysis
16- **Discord Integration**:
90## Val.town Specific Considerations
91
92- The database uses Val.town's built-in SQLite database
93- The code uses Deno and ESM modules compatible with Val.town
94- HTTP endpoints are designed to work with Val.town's serverless model
55}
56
57// Database-related types
58export interface MessageRecord {
59 id: string;
1// Link analyzer for Val.town
2import { getMessages, updateLinkCategory } from "./database.ts";
3import { categorizeContent } from "./anthropic-service.ts";
4import { extractUrls, log } from "../shared/utils.ts";
204
205/**
206 * Batch process all links in the database
207 * - Categorizes uncategorized links
208 * - Updates link metadata
230 const metadata = await extractLinkMetadata(link.url);
231
232 // Update link in database
233 await updateLinkCategory(link.id, categorization.category);
234
2import { Hono } from "https://esm.sh/hono@3.12.5";
3import { cors } from "https://esm.sh/@hono/cors@0.0.8";
4import { initializeDatabase } from "./database.ts";
5import { fetchAndStoreDMs } from "./discord-client.ts";
6import { handleDiscordInteractionRequest } from "./discord-interactions.ts";
20async function initializeServices() {
21 try {
22 // Initialize the database
23 await initializeDatabase();
24 log("Database initialized successfully", "info");
25
26 // Initialize Anthropic client
1// Database adapter for Val.town SQLite
2import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
3import { extractUrls, log } from "../shared/utils.ts";
19
20/**
21 * Initialize the database schema
22 */
23export async function initializeDatabase() {
24 try {
25 // Create messages table
102 }
103
104 log("Database initialized successfully", "info");
105 return true;
106 } catch (error) {
107 log(`Error initializing database: ${error.message}`, "error");
108 throw error;
109 }
111
112/**
113 * Save messages to the database
114 * @param messages - Array of message objects to save
115 */
168 }
169
170 log(`Saved ${savedCount} of ${messages.length} messages to database`, "info");
171 return savedCount;
172 } catch (error) {
177
178/**
179 * Get messages from the database, with optional filtering
180 * @param filters - Optional filters for messages
181 * @returns Array of message objects
239
240/**
241 * Get links from the database, with optional filtering
242 * @param filters - Optional filters for links
243 * @returns Array of link objects