DB: Switch to better-sqlite3
This commit is contained in:
25
core/db.js
25
core/db.js
@@ -1,20 +1,21 @@
|
||||
const fs = require("fs");
|
||||
const sqlite3 = require("sqlite3").verbose();
|
||||
const Database = require("better-sqlite3");
|
||||
const path = require("path");
|
||||
const { open } = require('sqlite');
|
||||
const filepath = "./data/minzbot.db";
|
||||
|
||||
async function createDbConnection() {
|
||||
if (fs.existsSync(filepath)) {
|
||||
//new sqlite3.Database(filepath);
|
||||
const db = await open({filename: filepath, driver: sqlite3.Database});
|
||||
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 dir = path.dirname(filepath);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
const db = new Database(filepath, { verbose: console.log });
|
||||
|
||||
initDB(db);
|
||||
|
||||
console.log("[DATABASE] Connection with better-sqlite3 established");
|
||||
return db;
|
||||
}
|
||||
|
||||
async function initDB(db) {
|
||||
@@ -36,4 +37,4 @@ async function initDB(db) {
|
||||
console.log('[DATABASE] Created new DB table');
|
||||
}
|
||||
|
||||
module.exports = createDbConnection();
|
||||
module.exports = createDbConnection;
|
||||
38
main.js
38
main.js
@@ -2,24 +2,27 @@ const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { Client, Collection, Events, GatewayIntentBits, Options, Partials } = require('discord.js');
|
||||
const { token } = require('./config.json');
|
||||
const createDbConnection = require('./core/db.js');
|
||||
|
||||
const localDB = require('./core/db');
|
||||
|
||||
const client = new Client({
|
||||
async function entrypoint() {
|
||||
const localDB = await createDbConnection();
|
||||
|
||||
const client = new Client({
|
||||
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions],
|
||||
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
|
||||
sweepers: {
|
||||
...Options.DefaultSweeperSettings,
|
||||
reactions: { interval: 300, filter: () => () => { return true } }
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
client.localDB = localDB;
|
||||
client.commands = new Collection();
|
||||
const foldersPath = path.join(__dirname, 'commands');
|
||||
const commandFolders = fs.readdirSync(foldersPath);
|
||||
client.localDB = localDB;
|
||||
client.commands = new Collection();
|
||||
const foldersPath = path.join(__dirname, 'commands');
|
||||
const commandFolders = fs.readdirSync(foldersPath);
|
||||
|
||||
for (const folder of commandFolders) {
|
||||
for (const folder of commandFolders) {
|
||||
const commandsPath = path.join(foldersPath, folder);
|
||||
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
||||
for (const file of commandFiles) {
|
||||
@@ -31,12 +34,12 @@ for (const folder of commandFolders) {
|
||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const eventsPath = path.join(__dirname, 'events');
|
||||
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
|
||||
const eventsPath = path.join(__dirname, 'events');
|
||||
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 event = require(filePath);
|
||||
if (event.once) {
|
||||
@@ -45,9 +48,9 @@ for (const file of eventFiles) {
|
||||
console.log(`Registered event ${event.name} ${file}`);
|
||||
client.on(event.name, (...args) => event.execute(...args));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client.on(Events.ClientReady, () => {
|
||||
client.on(Events.ClientReady, () => {
|
||||
//setup timers
|
||||
const timersPath = path.join(__dirname, 'timers');
|
||||
const timerFiles = fs.readdirSync(timersPath).filter(file => file.endsWith('.js'));
|
||||
@@ -61,6 +64,9 @@ client.on(Events.ClientReady, () => {
|
||||
timer.instance = timerInstance;
|
||||
if (timer.immediate) { timer.tick(client, timer) }
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
client.login(token);
|
||||
client.login(token);
|
||||
};
|
||||
|
||||
entrypoint();
|
||||
|
||||
1007
package-lock.json
generated
1007
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"axios": "^1.6.8",
|
||||
"better-sqlite3": "^12.6.2",
|
||||
"discord.js": "^14.13.0",
|
||||
"dotenv": "^16.3.1",
|
||||
"spotify-web-api-node": "^5.0.2",
|
||||
"sqlite": "^5.1.1",
|
||||
"sqlite3": "^5.1.7"
|
||||
"sqlite": "^5.1.1"
|
||||
},
|
||||
"name": "minzbot",
|
||||
"description": "Successor of the Discord.py implementation",
|
||||
|
||||
Reference in New Issue
Block a user