· 5 years ago · Feb 13, 2020, 05:42 PM
1const sql = require('sqlite');
2const path = require('path');
3sql.open(path.join(__dirname, 'credits.sql')) // read sql file
4.then(() => { // then ?
5 console.log('Opened') // if the sql opened
6 sql.run(`CREATE TABLE IF NOT EXISTS creditSysteme (id VARCHAR(30), credits BIGINT, timeDaily BIGINT)`) // create new table if the table does'nt exosts
7})
8.catch(err => console.error(err)) // if the sql file does'nt exists
9
10client.on("message", async msg => { // event message
11 if(!msg.channel.guild) return; // channel guild
12 let men = msg.mentions.users.first() || msg.author; // the mention or the author
13 let prize = msg.content.split(" ").slice(2).join(" ") // prize
14
15 if(msg.content.startsWith(prefix+"credits")) { // if the message content credits do
16 if(!men || !men === undefined) return msg.channel.send("** :interrobang: | "+men.username+", I can't find "+men.username+"!**"); // undefind user
17 if(!prize) {
18 sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`).then(res => { // select user from table
19 if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if the user does'nt exisit in table
20 if(res) { // if user exsist
21 msg.channel.send("**"+men.username+" :credit_card: balance is ``"+res.credits+"$``.**") // reply
22 }
23 })
24 }else{ // else ?
25 if(isNaN(prize)) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // is nan :)
26 if(parseFloat(prize) === NaN) return msg.channel.send(" :interrobang: | "+msg.author.username+", type the credit you need to transfer!"); // if nan :))
27 if(men === msg.author) return; // if the men = author
28 let authorRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
29 let userRes = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${men.id}'`) // select from sql
30 if(!authorRes) sql.run(`INSERT INTO creditSysteme VALUES ('${msg.author.id}', 0, 0)`) // if !user create new col
31 if(!userRes) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
32 let authorCredits = authorRes.credits; // credits before transfer
33 let userCredits = userRes.credits; // credits before transfer
34 if(parseFloat(prize) > authorCredits) return msg.channel.send("** :thinking: | "+msg.author.username+", Your balance is not enough for that!**"); // if the balance hight then prize
35 sql.run(`UPDATE creditSysteme SET credits = ${authorCredits - parseInt(prize)} WHERE id = '${msg.author.id}'`); // uptade credits for the author
36 sql.run(`UPDATE creditSysteme SET credits = ${userCredits + parseInt(prize)} WHERE id = '${men.id}'`); // update credits for the mentions user
37 msg.channel.send("**:moneybag: | "+msg.author.username+", has transferred ``$"+prize+"`` to "+men.toString()+"**") // the message :)
38 }
39 } else if(msg.content.startsWith(prefix+"daily")) { // if the message content daily do
40 let daily = 250; // 1d
41 let amount = Math.floor((Math.random() * 500) + 1) // Money
42 let res = await sql.get(`SELECT * FROM creditSysteme WHERE id = '${msg.author.id}'`) // select from sql
43 if(!res) sql.run(`INSERT INTO creditSysteme VALUES ('${men.id}', 0, 0)`) // if !user create new col
44 let time = res.timeDaily; // select last daily
45 let credits = res.credits; // credits before daily
46 if(time != null && daily - (Date.now() - time) > 0) { // if already climed the daily in same day
47
48 let fr8 = ms(daily - (Date.now() - time)); // the remining time
49 msg.channel.send("**:stopwatch: | "+msg.author.username+", your daily :yen: credits refreshes in "+fr8.hours+" hours and "+fr8.seconds+" seconds. **") //reply
50
51 }else{ // if does'nt clim her daily in 2h
52 msg.channel.send("**:atm: | "+msg.author.username+", you received your :yen: "+amount+" daily credits!**"); // reply
53 sql.run(`UPDATE creditSysteme SET credits = ${credits + amount}, timeDaily = ${Date.now()} WHERE id = '${msg.author.id}'`); // add amount to the credits before daily
54 }
55 }
56});