1 |
import { systemPrefix } from "@sudobot/utils/utils"; |
2 |
import { existsSync } from "fs"; |
3 |
import ArchiveServer from "src/server/ArchiveServer"; |
4 |
import { ArchiveMessagePayload } from "../types/ArchiveMessagePayload"; |
5 |
|
6 |
class ArchiveService implements Disposable { |
7 |
private _db?: Awaited<ReturnType<typeof ArchiveService.createDatabase>>; |
8 |
private _server?: ArchiveServer; |
9 |
|
10 |
private constructor() {} |
11 |
|
12 |
public get db() { |
13 |
if (!this._db) { |
14 |
throw new Error("Database not initialized"); |
15 |
} |
16 |
|
17 |
return this._db; |
18 |
} |
19 |
|
20 |
private prepare(query: string, params: string[], results = true) { |
21 |
const db = this.db as unknown as import("bun:sqlite").Database; |
22 |
const compiled = db.prepare(query, params); |
23 |
|
24 |
if (results) { |
25 |
return compiled.all(); |
26 |
} |
27 |
|
28 |
compiled.run(); |
29 |
} |
30 |
|
31 |
private static createTables(_db: Awaited<ReturnType<typeof ArchiveService.createDatabase>>) { |
32 |
const db = _db as unknown as import("bun:sqlite").Database; |
33 |
|
34 |
db.transaction(() => { |
35 |
db.run( |
36 |
`CREATE TABLE IF NOT EXISTS messages ( |
37 |
id TEXT PRIMARY KEY, |
38 |
userId TEXT, |
39 |
guildId TEXT, |
40 |
channelId TEXT, |
41 |
content TEXT, |
42 |
timestamp INTEGER |
43 |
); |
44 |
` |
45 |
); |
46 |
|
47 |
db.run(` |
48 |
CREATE TABLE IF NOT EXISTS users ( |
49 |
id TEXT PRIMARY KEY, |
50 |
username TEXT, |
51 |
isBot INTEGER DEFAULT 0 |
52 |
);`); |
53 |
})(); |
54 |
} |
55 |
|
56 |
public async archive({ author, guild, message, channel }: ArchiveMessagePayload) { |
57 |
const { id: userId } = author; |
58 |
const { id: guildId } = guild; |
59 |
const { id: channelId } = channel; |
60 |
const { id, content, createdTimestamp } = message; |
61 |
|
62 |
this.prepare( |
63 |
`INSERT INTO messages (id, userId, guildId, channelId, content, timestamp) VALUES (?, ?, ?, ?, ?, ?);`, |
64 |
[id, userId, guildId, channelId, content, createdTimestamp.toString()], |
65 |
false |
66 |
); |
67 |
|
68 |
this.prepare( |
69 |
`INSERT OR IGNORE INTO users (id, username, isBot) VALUES (?, ?, ?);`, |
70 |
[userId, author.username, author.bot ? "1" : "0"], |
71 |
false |
72 |
); |
73 |
} |
74 |
|
75 |
public close() { |
76 |
this._db?.close(); |
77 |
} |
78 |
|
79 |
public static async create() { |
80 |
const service = new ArchiveService(); |
81 |
service._db = await ArchiveService.createDatabase(); |
82 |
service._server = new ArchiveServer(service._db); |
83 |
return service; |
84 |
} |
85 |
|
86 |
private static async createDatabase() { |
87 |
let db; |
88 |
const dbPath = systemPrefix("archive.db"); |
89 |
const createTables = !existsSync(dbPath); |
90 |
|
91 |
if (process.isBun) { |
92 |
const { Database } = await import("bun:sqlite"); |
93 |
db = new Database(dbPath, { |
94 |
create: true, |
95 |
strict: true, |
96 |
readwrite: true |
97 |
}); |
98 |
} else { |
99 |
const { default: Database } = await import("better-sqlite3"); |
100 |
db = new Database(dbPath); |
101 |
} |
102 |
|
103 |
if (createTables) { |
104 |
this.createTables(db); |
105 |
} |
106 |
|
107 |
return db; |
108 |
} |
109 |
|
110 |
public [Symbol.dispose]() { |
111 |
this.close(); |
112 |
} |
113 |
|
114 |
public startServer() { |
115 |
this._server?.start(); |
116 |
} |
117 |
} |
118 |
|
119 |
export default ArchiveService; |