-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
82 lines (68 loc) · 2.7 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const { QuickDB } = require("quick.db")
const { MongoDriver } = require("quickmongo")
const methods = {
add: require('./methods/add'),
set: require('./methods/set'),
get: require('./methods/get'),
all: require('./methods/all'),
daily: require('./methods/daily'),
subtract: require('./methods/subtract'),
lb: require('./methods/lb')
}
let database;
/**
* Allows user select a DB driver
* @param {"sqlite"|"mongodb"} driver Driver name
* @param {object} options The options object
* @returns {void}
*/
async function selectDriver(driver, options){
if(!["mongodb", "sqlite"].includes(driver)) throw new TypeError("[discord-simple-economy] Unknown driver. Select either 'mongodb' or 'sqlite'!")
if(driver == "mongodb" && !options.mongoUri) throw new SyntaxError("[discord-simple-economy] Missing mongo uri on options object.")
if(driver == "sqlite") database = new QuickDB()
if(driver == "mongodb"){
const d = new MongoDriver(options.mongoUri)
await d.connect()
database = new QuickDB({ driver: d })
}
}
class guildUser {
/**
* @param {user} string Discord user ID or member object with ID.
* @param {guild} string Discord guild ID or guild object with ID.
*/
constructor(user, guild) {
if(!database) throw new SyntaxError("[discord-simple-economy] Missing database. Did you used selectDriver()?")
user.id ? this.user = user.id : this.user = user;
guild.id ? this.guild = guild.id : this.guild = guild;
}
async add(amount, type) {
return (await methods.add({ db: database, amount: amount, type: type, user: this.user, guild: this.guild }));
}
async subtract(amount, type) {
return (await methods.subtract({ db: database, amount: amount, type: type, user: this.user, guild: this.guild }));
}
async set(amount, type) {
return (await methods.set({ db: database, amount: amount, type: type, user: this.user, guild: this.guild }));
}
async get(type) {
return (await methods.get({ db: database, type: type, user: this.user, guild: this.guild }));
}
async all() {
return (await methods.all({ db: database, guild: this.guild }));
}
async daily(amount, type) {
return (await methods.daily({ db: database, amount: amount, type: type, user: this.user, guild: this.guild }));
}
async lb(type=null){
return (await methods.lb({ db: database, type: type, user: this.user, guild: this.guild }));
}
}
module.exports = {
guildUser: guildUser,
version: require("./package.json").version,
selectDriver,
randomNumber: (min, max) => {
return (require('./methods/randomNumber')({min:min,max:max}));
}
};