DB: Switch to better-sqlite3

This commit is contained in:
2026-01-17 18:14:22 +01:00
parent 2e4147abec
commit 00210a03cf
4 changed files with 153 additions and 991 deletions

View File

@@ -1,20 +1,21 @@
const fs = require("fs"); const fs = require("fs");
const sqlite3 = require("sqlite3").verbose(); const Database = require("better-sqlite3");
const path = require("path");
const { open } = require('sqlite'); const { open } = require('sqlite');
const filepath = "./data/minzbot.db"; const filepath = "./data/minzbot.db";
async function createDbConnection() { async function createDbConnection() {
if (fs.existsSync(filepath)) { const dir = path.dirname(filepath);
//new sqlite3.Database(filepath); if (!fs.existsSync(dir)) {
const db = await open({filename: filepath, driver: sqlite3.Database}); fs.mkdirSync(dir, { recursive: true });
await initDB(db);
return db;
} else {
const db = await open({filename: filepath, driver: sqlite3.Database});
await initDB(db);
console.log("[DATABASE] Connection with SQLite has been established");
return db;
} }
const db = new Database(filepath, { verbose: console.log });
initDB(db);
console.log("[DATABASE] Connection with better-sqlite3 established");
return db;
} }
async function initDB(db) { async function initDB(db) {
@@ -36,4 +37,4 @@ async function initDB(db) {
console.log('[DATABASE] Created new DB table'); console.log('[DATABASE] Created new DB table');
} }
module.exports = createDbConnection(); module.exports = createDbConnection;

108
main.js
View File

@@ -2,65 +2,71 @@ const fs = require('node:fs');
const path = require('node:path'); const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits, Options, Partials } = require('discord.js'); const { Client, Collection, Events, GatewayIntentBits, Options, Partials } = require('discord.js');
const { token } = require('./config.json'); const { token } = require('./config.json');
const createDbConnection = require('./core/db.js');
const localDB = require('./core/db');
const client = new Client({ async function entrypoint() {
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions], const localDB = await createDbConnection();
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
sweepers: { const client = new Client({
...Options.DefaultSweeperSettings, intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions],
reactions: { interval: 300, filter: () => () => { return true } } partials: [Partials.Message, Partials.Channel, Partials.Reaction],
} sweepers: {
}); ...Options.DefaultSweeperSettings,
reactions: { interval: 300, filter: () => () => { return true } }
}
});
client.localDB = localDB; client.localDB = localDB;
client.commands = new Collection(); client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands'); const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath); const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) { for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder); const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) { for (const file of commandFiles) {
const filePath = path.join(commandsPath, file); const filePath = path.join(commandsPath, file);
const command = require(filePath); const command = require(filePath);
if ('data' in command && 'execute' in command) { if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command); client.commands.set(command.data.name, command);
} else { } else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
} }
} }
}
const eventsPath = path.join(__dirname, 'events'); const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) { for (const file of eventFiles) {
const filePath = path.join(eventsPath, file); const filePath = path.join(eventsPath, file);
const event = require(filePath); const event = require(filePath);
if (event.once) { if (event.once) {
client.once(event.name, (...args) => event.execute(...args)); client.once(event.name, (...args) => event.execute(...args));
} else { } else {
console.log(`Registered event ${event.name} ${file}`); console.log(`Registered event ${event.name} ${file}`);
client.on(event.name, (...args) => event.execute(...args)); client.on(event.name, (...args) => event.execute(...args));
}
} }
}
client.on(Events.ClientReady, () => { client.on(Events.ClientReady, () => {
//setup timers //setup timers
const timersPath = path.join(__dirname, 'timers'); const timersPath = path.join(__dirname, 'timers');
const timerFiles = fs.readdirSync(timersPath).filter(file => file.endsWith('.js')); const timerFiles = fs.readdirSync(timersPath).filter(file => file.endsWith('.js'));
for (const file of timerFiles) { for (const file of timerFiles) {
const filePath = path.join(timersPath, file); const filePath = path.join(timersPath, file);
const timer = require(filePath); const timer = require(filePath);
console.log(`Started Timer ${timer.name}`); console.log(`Started Timer ${timer.name}`);
let timerInstance = setInterval(() => { let timerInstance = setInterval(() => {
timer.tick(client, timer); timer.tick(client, timer);
}, timer.timeout); }, timer.timeout);
timer.instance = timerInstance; timer.instance = timerInstance;
if (timer.immediate) { timer.tick(client, timer) } if (timer.immediate) { timer.tick(client, timer) }
} }
}); });
client.login(token); client.login(token);
};
entrypoint();

1007
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,11 @@
{ {
"dependencies": { "dependencies": {
"axios": "^1.6.8", "axios": "^1.6.8",
"better-sqlite3": "^12.6.2",
"discord.js": "^14.13.0", "discord.js": "^14.13.0",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"spotify-web-api-node": "^5.0.2", "spotify-web-api-node": "^5.0.2",
"sqlite": "^5.1.1", "sqlite": "^5.1.1"
"sqlite3": "^5.1.7"
}, },
"name": "minzbot", "name": "minzbot",
"description": "Successor of the Discord.py implementation", "description": "Successor of the Discord.py implementation",