discord-prefix
Version:
A simple Node.js module that lets you easily manage custom prefixes for your discord bot
90 lines (79 loc) • 3.83 kB
JavaScript
const Database = require('better-sqlite3');
let db = new Database('./prefixes.db');
let methods = {
fetch: require('./lib/fetch.js'),
set: require('./lib/set.js'),
delete: require('./lib/delete.js'),
all: require('./lib/all.js')
};
module.exports = {
fetch: function(key, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('fetch', {id: key, ops: ops || {}});
},
get: function(key, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('fetch', {id: key, ops: ops || {}});
},
set: function(key, value, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
if (!value && value != 0) throw new TypeError('No value specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('set', {stringify: true, id: key, data: value, ops: ops || {}});
},
delete: function(key, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('delete', {id: key, ops: ops || {}});
},
all: function(ops) {
return arbitrate('all', {ops: ops || {}});
},
fetchAll: function(ops) {
return arbitrate('all', {ops: ops || {}});
},
table: function(tableName, options = {}) {
if (typeof tableName !== 'string') throw new TypeError('Table name has to be a string. Need Help? Check out: discord.gg/plexidev');
else if (tableName.includes(' ')) throw new TypeError('Table name cannot include spaces. Need Help? Check out: discord.gg/plexidev');
this.tableName = tableName;
this.fetch = function(key, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('fetch', {id: key, ops: ops || {}}, this.tableName);
}
this.get = function(key, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('fetch', {id: key, ops: ops || {}}, this.tableName);
}
this.set = function(key, value, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
if (!value && value != 0) throw new TypeError('No value specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('set', {stringify: true, id: key, data: value, ops: ops || {}}, this.tableName);
}
this.delete = function(key, ops) {
if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
return arbitrate('delete', {id: key, ops: ops || {}}, this.tableName);
}
this.fetchAll = function(ops) {
return arbitrate('all', {ops: ops || {}}, this.tableName);
}
this.all = function(ops) {
return arbitrate('all', {ops: ops || {}}, this.tableName);
}
}
}
function arbitrate(method, params, tableName) {
let options = {
table: tableName || 'json'
}
db.prepare(`CREATE TABLE IF NOT EXISTS ${options.table} (ID TEXT, json TEXT)`).run();
if (params.ops.target && params.ops.target[0] === '.') params.ops.target = params.ops.target.slice(1); // Remove prefix if necessary
if (params.data && params.data === Infinity) throw new TypeError(`You cannot set Infinity into the database @ ID: ${params.id}`)
if (params.stringify) {
try { params.data = JSON.stringify(params.data); } catch (e)
{ throw new TypeError(`Please supply a valid input @ ID: ${params.id}\nError: ${e.message}`); }
}
if (params.id && params.id.includes('.')) {
let unparsed = params.id.split('.');
params.id = unparsed.shift();
params.ops.target = unparsed.join('.');
}
return methods[method](db, params, options);
};