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 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
108
main.js
@@ -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: {
|
|
||||||
...Options.DefaultSweeperSettings,
|
|
||||||
reactions: { interval: 300, filter: () => () => { return true } }
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
client.localDB = localDB;
|
const client = new Client({
|
||||||
client.commands = new Collection();
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessageReactions],
|
||||||
const foldersPath = path.join(__dirname, 'commands');
|
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
|
||||||
const commandFolders = fs.readdirSync(foldersPath);
|
sweepers: {
|
||||||
|
...Options.DefaultSweeperSettings,
|
||||||
|
reactions: { interval: 300, filter: () => () => { return true } }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
for (const folder of commandFolders) {
|
client.localDB = localDB;
|
||||||
const commandsPath = path.join(foldersPath, folder);
|
client.commands = new Collection();
|
||||||
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
const foldersPath = path.join(__dirname, 'commands');
|
||||||
for (const file of commandFiles) {
|
const commandFolders = fs.readdirSync(foldersPath);
|
||||||
const filePath = path.join(commandsPath, file);
|
|
||||||
const command = require(filePath);
|
for (const folder of commandFolders) {
|
||||||
if ('data' in command && 'execute' in command) {
|
const commandsPath = path.join(foldersPath, folder);
|
||||||
client.commands.set(command.data.name, command);
|
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
||||||
} else {
|
for (const file of commandFiles) {
|
||||||
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
|
const filePath = path.join(commandsPath, file);
|
||||||
|
const command = require(filePath);
|
||||||
|
if ('data' in command && 'execute' in command) {
|
||||||
|
client.commands.set(command.data.name, command);
|
||||||
|
} else {
|
||||||
|
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
1007
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user