-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
70 lines (57 loc) · 2.41 KB
/
database.js
File metadata and controls
70 lines (57 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// config/database.js
// Initialises a SQLite database that persists welcome-channel settings
// across bot restarts. Each guild gets its own row keyed by guild ID.
import Database from 'better-sqlite3';
import { fileURLToPath } from 'url';
import path from 'path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const DB_PATH = path.join(__dirname, '..', 'data', 'bot.db');
// Ensure the data directory exists before opening the database
import fs from 'fs';
const dataDir = path.join(__dirname, '..', 'data');
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
const db = new Database(DB_PATH);
// Enable WAL mode for better concurrent read performance
db.pragma('journal_mode = WAL');
// Create the settings table if it does not already exist
db.exec(`
CREATE TABLE IF NOT EXISTS guild_settings (
guild_id TEXT PRIMARY KEY,
channel_id TEXT NOT NULL,
webhook_url TEXT NOT NULL,
created_at INTEGER DEFAULT (strftime('%s', 'now')),
updated_at INTEGER DEFAULT (strftime('%s', 'now'))
);
`);
// ── Prepared statements ──────────────────────────────────────────────────────
/** Upsert a guild's welcome channel and webhook URL. */
const upsertSettings = db.prepare(`
INSERT INTO guild_settings (guild_id, channel_id, webhook_url, updated_at)
VALUES (@guildId, @channelId, @webhookUrl, strftime('%s', 'now'))
ON CONFLICT(guild_id) DO UPDATE SET
channel_id = excluded.channel_id,
webhook_url = excluded.webhook_url,
updated_at = strftime('%s', 'now')
`);
/** Retrieve settings for a guild. Returns undefined when not configured. */
const getSettings = db.prepare(`
SELECT * FROM guild_settings WHERE guild_id = ?
`);
/** Remove a guild's settings (e.g. when resetting the welcome channel). */
const deleteSettings = db.prepare(`
DELETE FROM guild_settings WHERE guild_id = ?
`);
export default {
/** Save or update the welcome channel & webhook for a guild. */
setWelcomeChannel(guildId, channelId, webhookUrl) {
upsertSettings.run({ guildId, channelId, webhookUrl });
},
/** Get the stored settings for a guild, or null if not set. */
getWelcomeSettings(guildId) {
return getSettings.get(guildId) ?? null;
},
/** Remove all welcome settings for a guild. */
resetWelcomeChannel(guildId) {
deleteSettings.run(guildId);
},
};