-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.js
377 lines (377 loc) · 11.4 KB
/
functions.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* **Function that encodes utf-8 to base64
*
* example:
* ```js
* encode("Hello!") //SGVsbG8=
* ```
* @param {String} string
* @returns {String}
*/
function encode(string){
return Buffer.from(string,"utf-8").toString("base64")
}
/**
* **Function that decodes base64 to utf-8**
*
* example:
* ```js
* decode("SGVsbG8=") //Hello
* ```
* @param {String} string
* @returns {String}
*/
function decode(string){
return Buffer.from(string,"base64").toString("utf-8")
}
/**
* @typedef User
* @property {Snowflake} id
* @property {Number} credits
* @typedef Command
* @property {String} name
* @property {String} value
*/
const {Database} = require("sqlite3").verbose();
const fs = require("fs")
const {Snowflake} = require("discord.js");
const Client = require("./index.js");
const db = new Database("database.db");
if(!fs.existsSync("./database.db")){
db.run(`CREATE TABLE users (id varchar(20), credits int)`);
db.run(`CREATE TABLE cmd (name varchar(20), value text)`);
}
class CommandDatabase{
/**
* **Function that returns all commands in database**
*
* example:
* ```js
* await CommandDatabase.getCommands()
* ```
* @returns {Promise.<Command[]>} Array of commands in database (async)
*/
static async getCommands(){
return new Promise(r=>{
db.all(`SELECT * FROM cmd`,(err,rows)=>{
if(err)throw err;
r(rows)
})
})
}
/**
* **Function that returns a command with the provided name from database**
*
* example:
* ```js
* await CommandDatabase.getCommand("admin")
* ```
* @param {String} name Command name
* @returns {Promise.<Command|undefined>} Command from database (async)
*/
static async getCommand(name){
return new Promise(r=>{
db.all(`SELECT * FROM cmd WHERE name=?`,[name],(err,rows)=>{
if(err)throw err;
r(rows[0])
})
})
}
/**
* **Function that adds a command to database**
*
* example:
* ```js
* await CommandDatabase.addCommand("command",new SlashCommandBuilder().setName("command").setDescription("A slash command").toJSON())
* ```
* @param {String} name Command name
* @param {String} data Command data
* @returns {Promise.<Command>} Command from database (async)
*/
static async addCommand(name, data) {
return new Promise((resolve, reject) => {
db.run('INSERT INTO cmd (name, value) VALUES (?, ?)',name, data, async function (err) {
if (err) {
reject(err);
} else {
resolve(await CommandDatabase.getCommand(name));
}
});
});
}
/**
* **Function that removes a provided command from database**
*
* example:
* ```js
* await CommandDatabase.removeCommand("command")
* ```
* @param {String} name Command name
* @returns {Promise.<void>} Nothing (async)
*/
static async removeCommand(name){
return new Promise((resolve,reject)=>{
db.run(`DELETE FROM cmd WHERE name=?`,[name],(err)=>{
if(err){
reject(err);
}else{
resolve();
}
})
})
}
/**
* **Function that edits a provided command from database**
*
* example:
* ```js
* await CommandDatabase.editCommand("command",new SlashCommandBuilder().setName("command").setDescription("A slash command").toJSON())
* ```
* @param {String} name Command name
* @param {String} data Command data
* @returns {Promise.<Command>} Command from database (async)
*/
static async editCommand(name,data){
return new Promise(r=>{
db.run(`UPDATE cmd SET value=? WHERE name=?`,[data,name],async(err)=>{
if(err)throw err;
r(await CommandDatabase.getCommand(name))
})
})
}
}
class Bank{
/**
* **Function that gives data about all users in the database**
*
* example:
* ```js
* await Bank.getUsers()
* ```
* @returns {Promise.<User[]>} Array of users (async)
*/
static async getUsers(){
return new Promise(r=>{
db.all(`SELECT * FROM users`,(err,rows)=>{
if(err)throw err;
r(rows);
})
})
}
/**
* **Function that returns list of users sorted by credits (descending)**
*
* example:
* ```js
* await Bank.getLeaderboard(100)
* ```
* @param {Number} limit Limit of users in leaderboard
* @returns {Promise.<User[]>} Users sorted by credits (async)
*/
static async getLeaderboard(limit=10){
return new Promise(r=>{
db.all(`SELECT * FROM users ORDER BY credits DESC LIMIT ?`,[limit],(err,rows)=>{
if(err)throw err;
r(rows);
})
})
}
/**
* **Function that gives user data from database**
*
* example:
* ```js
* await Bank.getUser("525791610794147842")
* ```
* @param {Snowflake} id User id
* @returns {Promise.<User|undefined>} User from database (async)
*/
static async getUser(id){
return new Promise(r=>{
db.all(`SELECT * FROM users WHERE id=?`,[id],(err,rows)=>{
if(err)throw err;
r(rows[0])
})
})
}
/**
* **Function that adds a user to database**
*
* example:
* ```js
* await Bank.addUser("525791610794147842",100)
* ```
* @param {String} id User id
* @param {Number} credits Amount of credits you want to create the account with
* @returns {Promise.<User>} User from database (async)
*/
static async addUser(id,credits){
return new Promise(r=>{
db.run(`INSERT INTO users (id,credits) values (?,?)`,[id,credits],async(err)=>{
if(err)throw err;
r(await Bank.getUser(id));
})
})
}
/**
* **Function that deletes the provided user from database**
*
* example:
* ```js
* await Bank.deleteUser("525791610794147842")
* ```
* @param {String} id User id
* @returns {Promise.<void>} Nothing (async)
*/
static async deleteUser(id){
return new Promise(r=>{
db.run(`DELETE FROM users WHERE id=?`,[id],async(err)=>{
if(err)throw err;
r();
})
})
}
/**
* **Function that gives credits to a provided user**
*
* example:
* ```js
* await Bank.addMoney("525791610794147842",100)
* ```
* @param {Snowflake} id User id
* @param {Number} amount Amound of credits to add to a user
* @returns {Promise.<User>} User from database
*/
static async addMoney(id,amount){
return new Promise(async r=>{
if(!await Bank.getUser(id)){
r(await Bank.addUser(id,amount))
}
db.run(`UPDATE users SET credits=credits+? WHERE id=?`,[amount,id],async(err)=>{
if(err)throw err;
r(await Bank.getUser(id));
})
})
}
/**
* **Function that transfers credits from one user to another
* @param {Snowflake} from
* @param {Snowflake} to
* @param {Number} amount
* @returns {Promise.<[User,User]>}
*/
static async transfer(from,to,amount){
return new Promise(async(resolve,reject)=>{
if(amount<0){
reject(new Error("Transfer amount cannot be lower than 0"))
}
if((await Bank.getUser(from)).credits<amount){
reject(new Error("Insufficient funds"))
}
resolve([await Bank.removeMoney(from,amount),await Bank.addMoney(to,amount)])
})
}
/**
* **Function that resets the balance of all acounts to 0**
*
* example:
* ```js
* await Bank.resetMoneyAll()
* ```
* @returns {Promise.<User[]>} Array of users (async)
*/
static async resetMoneyAll(){
return new Promise(r=>{
db.run(`UPDATE users set credits=0`,async(err)=>{
if(err)throw err;
r(await Bank.getUsers())
})
})
}
/**
* **Function that removes credits from a provided user**
*
* example:
* ```js
* await Bank.removeMoney("525791610794147842",100)
* ```
* @param {Snowflake} id User id
* @param {Number} amount Amount of credits that you want removed from the user
* @returns {Promise.<User>} User from database (async)
*/
static async removeMoney(id,amount){
return new Promise(async r=>{
const user = await Bank.getUser(id)
if(!user){
r(await Bank.addUser(id,0));
}
if(user.credits<amount){
amount = user.credits;
}
db.run(`UPDATE users SET credits=credits-? WHERE id=?`,[amount,id],async(err)=>{
if(err)throw err;
r(await Bank.getUser(id));
})
})
}
/**
* **Function that sets the credits of a provided user**
*
* example:
* ```js
* await Bank.setMoney("525791610794147842",100)
* ```
* @param {String} id
* @param {Number} amount
* @returns {Promise.<User>}
*/
static async setMoney(id,amount){
return new Promise(async r=>{
if(!await Bank.getUser(id)){
await Bank.addUser(id,amount)
}
db.run(`UPDATE users SET credits=? WHERE id=?`,[amount,id],async(err)=>{
if(err)throw err;
r(await Bank.getUser(id));
})
})
}
}
/**
* **Function that checks whether any slash commands were update/deleted/added**
* @param {Client} client Bot client
* @returns {Promise.<{name:String,value:String,action:"insert"|"update"|"delete"}[]>} Array of commands to update/delete/add to database
*/
async function checkSlashCommandUpdates(client) {
const dbCommands = await new Promise((resolve, reject) => {
db.all('SELECT * FROM cmd', (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
const changes = [];
const clientCommandArray = [...client.commands.values()];
for (const command of clientCommandArray) {
if (!dbCommands.some((row) => row.name == command.data.name)) {
const encodedData = encode(JSON.stringify(command.data.toJSON()));
changes.push({ name: command.data.name, data: encodedData, action: 'insert' });
}
}
for (const dbCommand of dbCommands) {
if(!changes.some(r=>r.name==dbCommand.name)){
const commandData = JSON.parse(decode(`${dbCommand.value}`));
const clientCommand = clientCommandArray.find((command) => command.data.name == dbCommand.name);
if (!clientCommand) {
changes.push({ name: dbCommand.name, action: 'delete' });
} else if (JSON.stringify(commandData) != JSON.stringify(clientCommand)) {
const encodedData = encode(JSON.stringify(clientCommand.data.toJSON()));
changes.push({ name: dbCommand.name, data: encodedData, action: 'update' });
}
}
}
return changes;
}
module.exports={Bank,checkSlashCommandUpdates,db,CommandDatabase,encode,decode}