Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
"description": "Reddit Eclipse custom Discord bot",
"main": "index.js",
"dependencies": {
"clash-of-clans-api": "^0.6.3",
"discord.js": "^12.0.2",
"enmap": "^5.2.4",
"enmap-level": "^2.1.0",
"express": "^4.17.1",
"moment-timezone": "^0.5.28",
"outdent": "^0.7.1",
"twemoji": "^12.1.5"
"better-sqlite3": "^12.5.0",
"clash-of-clans-api": "^0.6.4",
"discord.js": "^14.25.1",
"express": "^5.2.1",
"moment-timezone": "^0.6.0",
"outdent": "^0.8.0",
"twemoji": "^14.0.2"
},
"devDependencies": {
"better-sqlite3": "^5.4.3",
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-plugin-import": "^2.20.1",
"markdownlint-cli": "^0.22.0"
"dotenv": "^17.2.3",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.32.0",
"markdownlint-cli": "^0.46.0"
},
"scripts": {
"start": "node ./src/index.js",
Expand All @@ -32,6 +30,9 @@
"url": "git+https://github.com/Luis729/eclipse-bot.git"
},
"author": "Paul Matthew Barrameda",
"maintainers": [
"Kanishk Gupta"
],
"license": "ISC",
"bugs": {
"url": "https://github.com/Luis729/reddit-eclipse-bot/issues"
Expand Down
92 changes: 92 additions & 0 deletions src/helper/SimpleStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const Database = require('better-sqlite3');
const path = require('path');

/**
* Simple key-value storage using SQLite that mimics Enmap's interface
*/
class SimpleStorage {
constructor(options = {}) {
const dbName = options.name || 'storage';
const dbPath = path.join(process.cwd(), `${dbName}.sqlite`);

this.db = new Database(dbPath);

// Create table if it doesn't exist
this.db.exec(`
CREATE TABLE IF NOT EXISTS storage (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
`);

this.getStmt = this.db.prepare('SELECT value FROM storage WHERE key = ?');
this.setStmt = this.db.prepare('INSERT OR REPLACE INTO storage (key, value) VALUES (?, ?)');
this.deleteStmt = this.db.prepare('DELETE FROM storage WHERE key = ?');
this.hasStmt = this.db.prepare('SELECT 1 FROM storage WHERE key = ?');
this.keysStmt = this.db.prepare('SELECT key FROM storage');
}

/**
* Get a value by key
* @param {string} key
* @returns {*} The stored value or undefined
*/
get(key) {
const row = this.getStmt.get(key);
return row ? JSON.parse(row.value) : undefined;
}

/**
* Set a value for a key
* @param {string} key
* @param {*} value
* @returns {SimpleStorage} this (for chaining)
*/
set(key, value) {
this.setStmt.run(key, JSON.stringify(value));
return this;
}

/**
* Check if a key exists
* @param {string} key
* @returns {boolean}
*/
has(key) {
return this.hasStmt.get(key) !== undefined;
}

/**
* Delete a key
* @param {string} key
* @returns {boolean} true if deleted, false if didn't exist
*/
delete(key) {
const result = this.deleteStmt.run(key);
return result.changes > 0;
}

/**
* Get all keys as an array
* @returns {string[]}
*/
keyArray() {
return this.keysStmt.all().map(row => row.key);
}

/**
* Clear all data
*/
clear() {
this.db.exec('DELETE FROM storage');
}

/**
* Close the database connection
*/
close() {
this.db.close();
}
}

module.exports = SimpleStorage;
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ if (devMode) {
}

const Discord = require('discord.js');
const Enmap = require('enmap');
const EnmapLevel = require('enmap-level');
const SimpleStorage = require('./helper/SimpleStorage.js');
const fs = require('fs');
const HandleMessage = require('./helper/HandleMessage.js');
const {
Expand Down Expand Up @@ -38,7 +37,7 @@ function trackDonations() {

require('moment-timezone').tz.setDefault(defaultTimeZone);

client.points = new Enmap({ provider: new EnmapLevel({ name: 'players' }) });
client.points = new SimpleStorage({ name: 'players' });
client.commands = new Discord.Collection();
fs.readdirSync('./src/command').forEach((file) => {
const Command = require(`./commands/${file}`);
Expand Down